解題思路
每個任務(Si,Ei,Pi),在Si的位置加入Pi,在Ei+1的位置刪掉Pi,這樣取前綴和就能表示每個點包含的所有數。
將每個任務拆成兩個操作:在Si加Pi和在Ei減Pi。將所有操作排序後按操作建主席樹。
然後還需要求個to數組,存詢問中的每個時間點要在哪棵樹中查詢。
可能會有重複的數,所以在查詢到最底層的時候需要判一下。因爲這個東西wa了好久。。。mdzz
代碼:
#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#define ll long long
using namespace std;
struct ldx{
ll wi,xi;
ll opt;
bool operator < (const ldx p) const{
return wi<p.wi;
}
}s[200005];
struct node{
ll x,y,num;
ll sum;
node* ch[2];
node(ll,ll,ll);
}*null=new node(0,0,0),*root[200005];
node:: node(ll _,ll __,ll ___){
x=_;y=__;num=___;sum=0;
ch[0]=ch[1]=null;
}
ll to[100005];
ll n,m,tot,xx,u;
ll ans=1;
void build(node* p,node* &t,ll lx,ll ly){
if(t==null) t=new node(lx,ly,0);
t->num=p->num+u;t->sum=p->sum+1ll*xx*u;
if(lx==ly) return ;
ll mid=(lx+ly)>>1;
if(mid>=xx) {t->ch[1]=p->ch[1];build(p->ch[0],t->ch[0],lx,mid);}
else {t->ch[0]=p->ch[0];build(p->ch[1],t->ch[1],mid+1,ly);}
}
ll srch(node* p,ll k){
if(p->num<=k) return p->sum;
else if(p->x==p->y && p->num>k) return p->x*k;
else if(p->ch[0]->num>=k) return srch(p->ch[0],k);
else return p->ch[0]->sum+srch(p->ch[1],k-p->ch[0]->num);
}
void sett(){
null->ch[0]=null->ch[1]=null;
for(ll i=0;i<=tot;i++) root[i]=null;
}
int main(){
ll ai,bi,ci;
scanf("%lld%lld",&m,&n);
for(ll i=1;i<=m;i++){
scanf("%lld%lld%lld",&ai,&bi,&ci);
tot++;s[tot].wi=ai;s[tot].xi=ci;s[tot].opt=1;
tot++;s[tot].wi=bi+1;s[tot].xi=ci;s[tot].opt=-1;
}
sort(s+1,s+tot+1);sett();
for(ll i=1;i<=tot;i++){
xx=s[i].xi;u=s[i].opt;
build(root[i-1],root[i],1,1e9);
}
ll tp=0;
for(ll i=1;i<=n;i++){
while(s[tp+1].wi<=i && tp<tot) tp++;
to[i]=tp;
}
for(ll i=1;i<=n;i++){
scanf("%lld%lld%lld%lld",&xx,&ai,&bi,&ci);
ll ki=(1ll*ai*ans+bi)%ci+1;
ans=srch(root[to[xx]],ki);
printf("%lld\n",ans);
}
return 0;
}