浅谈主席树:https://www.cnblogs.com/AKMer/p/9956734.html
题目传送门:https://www.lydsy.com/JudgeOnline/problem.php?id=3932
用主席树维护任务差分值,类似于树状数组的区间修改单点询问。
时间复杂度:(O(nlogm))
空间复杂度:(O(mlogm))
代码入戏:
#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
const int maxn=1e5+5;
int n,m,cnt;
ll lstans=1;
int rt[maxn<<1],tmp1[maxn<<1],tmp2[maxn];
int read() {
int x=0,f=1;char ch=getchar();
for(;ch<'0'||ch>'9';ch=getchar())if(ch=='-')f=-1;
for(;ch>='0'&&ch<='9';ch=getchar())x=x*10+ch-'0';
return x*f;
}
struct Task {
int pos,val,sign;
Task() {}
Task(int _pos,int _val,int _sign) {
pos=_pos,val=_val,sign=_sign;
}
bool operator<(const Task &a)const {
return pos<a.pos;
}
}p[maxn<<1];
struct tree_node {
ll sum;
int cnt,ls,rs;
};
struct Chairman_tree {
int tot;
tree_node tree[maxn*40];
void ins(int lst,int &now,int l,int r,int pos,int v) {
now=++tot;tree[now]=tree[lst];
tree[now].cnt+=v;tree[now].sum+=v*tmp2[pos];
if(l==r)return; int mid=(l+r)>>1;
if(pos<=mid)ins(tree[lst].ls,tree[now].ls,l,mid,pos,v);
else ins(tree[lst].rs,tree[now].rs,mid+1,r,pos,v);
}
ll query(int now,int l,int r,int rk) {
if(tree[now].cnt<=rk)return tree[now].sum;
if(l==r)return 1ll*rk*tmp2[l];//记得是返回rk*tmp2而不是直接返回tree[now].sum
int mid=(l+r)>>1,tmp=tree[tree[now].ls].cnt;
if(tmp>=rk)return query(tree[now].ls,l,mid,rk);
return tree[tree[now].ls].sum+query(tree[now].rs,mid+1,r,rk-tmp);
}
}T;
int main() {
m=read(),n=read();
for(int i=1;i<=m;i++) {
int st=read(),ed=read()+1;tmp2[i]=read();
p[i<<1]=Task(st,tmp2[i],1);
p[(i<<1)-1]=Task(ed,tmp2[i],-1);
}sort(p+1,p+2*m+1);sort(tmp2+1,tmp2+m+1);
for(int i=1;i<=2*m;i++)
tmp1[i]=p[i].pos;
cnt=unique(tmp2+1,tmp2+m+1)-tmp2-1;
for(int i=1;i<=2*m;i++) {
int x=lower_bound(tmp2+1,tmp2+cnt+1,p[i].val)-tmp2;
T.ins(rt[i-1],rt[i],1,cnt,x,p[i].sign);
}
for(int i=1;i<=n;i++) {
int x=read(),a=read(),b=read(),c=read();
int k=1+(lstans%c*a%c+b)%c;
x=upper_bound(tmp1+1,tmp1+2*m+1,x)-tmp1-1;
lstans=T.query(rt[x],1,cnt,k);
printf("%lld
",lstans);
}
return 0;
}