BZOJ.3932.[CQOI2015]任务查询系统(主席树 差分)
对于这一区间的操作,我们可以想到差分+前缀和(感觉也没什么别的了。。)。
同时对于本题我们能想到主席树,而主席树正是利用前一个节点建树的。
所以离散化、按时间排序,把操作拆成单点加和减即可。
另外优先级会有重,权值线段树是去重后的,所以要记录sz[](即有sz[]个该值 是sum[])并根据这个算出k个。
但是对于同一根节点每次修改必须新建logn个节点?是的,因为每改一个节点都不能和之前的路径共用,需要新建。
注意每个询问拆成了两个,空间是2nlogn!
longlong!
//82080kb 3672ms
#include <cstdio>
#include <cctype>
#include <algorithm>
#define gc() getchar()
typedef long long LL;
const int N=1e5+5,S=N*40;//size!
int n,m,cnt,ref[N],root[N];
struct Operation
{
int p,val,tag;
Operation() {}
Operation(int _p,int _v,int _t):p(_p),val(_v),tag(_t) {}
bool operator <(const Operation &a)const{
return p<a.p;
}
}q[N<<1];
namespace T
{
#define lson son[rt][0]
#define rson son[rt][1]
int tot,sz[S],son[S][2]; LL sum[S];
inline void Update(int rt){
sum[rt]=sum[lson]+sum[rson];
}
void Insert(int rt,int &y,int l,int r,int p,int v)
{
sz[y=++tot]=sz[rt]+v, sum[y]=sum[rt];
if(l==r) sum[y]+=ref[p]*v;//别直接用p!
else{
int m=l+r>>1;
if(p<=m) son[y][1]=rson, Insert(lson,son[y][0],l,m,p,v);
else son[y][0]=lson, Insert(rson,son[y][1],m+1,r,p,v);
Update(y);
}
}
LL Query(int rt,int l,int r,int k)
{
if(l==r) return sz[rt]?sum[rt]/sz[rt]*k:0;
if(sz[lson]==k) return sum[lson];
else if(sz[lson]<k) return sum[lson]+Query(rson,(l+r>>1)+1,r,k-sz[lson]);
else return Query(lson,l,l+r>>1,k);
}
}
inline int read()
{
int now=0;register char c=gc();
for(;!isdigit(c);c=gc());
for(;isdigit(c);now=now*10+c-'0',c=gc());
return now;
}
int Find(int x)
{
int l=1,r=cnt,mid;
while(l<r)
if(ref[mid=l+r>>1]<x) l=mid+1;
else r=mid;
return l;
}
int main()
{
n=read(),m=read();
int tot=n<<1;
for(int s,e,i=0; i<n; ++i)
s=read(),e=read(),ref[i+1]=read(),q[i<<1]=Operation(s,ref[i+1],1),q[i<<1|1]=Operation(e+1,ref[i+1],-1);
std::sort(ref+1,ref+1+n), cnt=1;
for(int i=2; i<=n; ++i)
if(ref[i]!=ref[i-1]) ref[++cnt]=ref[i];
std::sort(q,q+tot), q[tot].p=m+1;
for(int i=0; i<tot; ++i) q[i].val=Find(q[i].val);
for(int i=1,now=0; i<=m; root[i+1]=root[i],++i)//m还是时间范围!
while(q[now].p<=i/*&& now<tot*/)
T::Insert(root[i]/*(赋值后)在root[i]的基础上建*/,root[i],1,cnt,q[now].val/*不在这写Find(q[].val)好像更快?*/,q[now].tag), ++now;
int x,a,b,c; LL res=1;
while(m--)
x=read(),a=read(),b=read(),c=read(),printf("%lld\n",res=T::Query(root[x],1,cnt,1+(res*a%c+b)%c));
return 0;
}
BZOJ.3932.[CQOI2015]任务查询系统(主席树 差分)的更多相关文章
- bzoj 3932: [CQOI2015]任务查询系统 -- 主席树 / 暴力
3932: [CQOI2015]任务查询系统 Time Limit: 20 Sec Memory Limit: 512 MB Description 最近实验室正在为其管理的超级计算机编制一套任务管 ...
- BZOJ 3932: [CQOI2015]任务查询系统 (主席树板题)
就是裸的主席树,差分之后排序插入主席树就行了. 注意主席树查询的时候叶子节点要特判,因为本身是有size的 还有要开longlong CODE #include <cctype> #inc ...
- BZOJ 3932: [CQOI2015]任务查询系统 [主席树]
传送门 题意: 任务用三元组(Si,Ei,Pi)描述,(Si,Ei,Pi)表示任务从第Si秒开始,在第Ei秒后结束(第Si秒和Ei秒任务也在运行),其优先级为Pi 调度系统会经常向查询系统询问,第Xi ...
- BZOJ 3932: [CQOI2015]任务查询系统 | 主席树练习题
题目: 洛谷也能评测 题解: De了好长时间BUG发现是自己sort前面有一行for没删,气死. 题目询问第x秒时候前k小的P值之和. 朴素想法: 我们可以把P值离散化,然后对于每个时刻建一棵定义域是 ...
- 2018.06.30 BZOJ 3932: [CQOI2015]任务查询系统(主席树)
3932: [CQOI2015]任务查询系统 Time Limit: 20 Sec Memory Limit: 512 MB Description 最近实验室正在为其管理的超级计算机编制一套任务管理 ...
- BZOJ3932: [CQOI2015]任务查询系统 主席树
3932: [CQOI2015]任务查询系统 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 4869 Solved: 1652[Submit][St ...
- [CQOI2015]任务查询系统 主席树
[CQOI2015]任务查询系统 LG传送门 以前还没见过主席树的这种写法. 考虑使用差分的思想处理每一个任务,然后所有的东西就都能顺理成章地用主席树维护了,查询的时候和平时的主席树有一点不同,详见代 ...
- 【BZOJ3932】[CQOI2015]任务查询系统 主席树
[BZOJ3932][CQOI2015]任务查询系统 Description 最近实验室正在为其管理的超级计算机编制一套任务管理系统,而你被安排完成其中的查询部分.超级计算机中的 任务用三元组(Si, ...
- bzoj 3932 [CQOI2015]任务查询系统(主席树)
Description 最近实验室正在为其管理的超级计算机编制一套任务管理系统,而你被安排完成其中的查询部分. 超级计算机中的任务用三元组(Si,Ei,Pi)描述,(Si,Ei,Pi)表示任务从第Si ...
随机推荐
- Sql语句里面调用变量
sql语句里面调用变量的话有两种情况,一种是字符类型,一种是整型.浮点型之类的数字 db1.Execute("insert DataInformation values('" + ...
- log4j常见配置
依赖jar <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId ...
- CString的头文件
CString的头文件:#include <atlstr.h>
- bzoj千题计划294:bzoj3139: [Hnoi2013]比赛
http://www.lydsy.com/JudgeOnline/problem.php?id=3139 队伍的顺序不会影响结果 将队伍的得分情况作为状态,记忆化搜索 就是先搜索第一只队伍的得分情况, ...
- ListUtil(差集、交集、并集)
package cn.fraudmetrix.octopus.horai.biz.utils; import java.util.ArrayList; import java.util.Arrays; ...
- Manacher's Algorithm 马拉车算法
作用:求一个字符串中的最长子串,同时还可以求所有子串的长度. 题目链接: https://vjudge.net/contest/254692#problem/B 代码: #include<bit ...
- 使用Docx4j创建word文档
原文标题:Creating Word documents with Docx4j 原文链接:http://blog.iprofs.nl/2012/09/06/creating-word-documen ...
- mongodb导入json文件
mongoimport --db test --collection item --jsonArray item.json
- 源码安装svn 1.8.9
2014年5月25日 12:26:14 需要文件: svn apr apr-util sqlite3 serf svn : http://subversion.apache.org/download/ ...
- export,import ,export default是什么
首先要知道export,import ,export default是什么 ES6模块主要有两个功能:export和importexport用于对外输出本模块(一个文件可以理解为一个模块)变量的接口i ...