洛谷P3168 [CQOI2015]任務查詢系統
阿新 • • 發佈:2018-01-02
log pda body turn blog pos lower post int
#include<cstdio> #include<cstdlib> #include<algorithm> #include<cstring> #include<vector> #define MAXN 100000+10 #define Nd Node* #define pii pair<int,int> #define mp make_pair #define ft first #define sc second #define pb push_back #define ll long long using namespacestd; struct Node{ Nd l;Nd r; int L,R,cnt; ll sum; }; Nd rt[MAXN]; ll b[MAXN]; Nd NewNode(Nd l,Nd r,int L,int R,ll sum,int cnt){ Nd ret=new Node; ret->l=l,ret->r=r,ret->L=L,ret->R=R,ret->sum=sum,ret->cnt=cnt; return ret; } Nd Build(int L,int R){ Nd ret=NewNode(NULL,NULL,L,R,0LL,0); if(L+1!=R)ret->l=Build(L,(L+R)>>1),ret->r=Build((L+R)>>1,R); return ret; } void update(Nd &A,Nd B,int p,ll x,int f){ if(!B)return; A=NewNode(B->l,B->r,B->L,B->R,B->sum+f*x,B->cnt+f); if(p<(B->L+B->R)>>1)update(A->l,B->l,p,x,f);else update(A->r,B->r,p,x,f); } ll query(Nd A,int k){ if(!A->l)return k*b[A->L]; if(A->l->cnt>=k)return query(A->l,k); else return query(A->r,k-A->l->cnt)+A->l->sum; } struct Opt{ int L,R,p; ll x; }s[MAXN]; int n,m,sz; vector<pii> vs[MAXN]; void init(){ scanf("%d%d",&m,&n); for(int i=1;i<=m;i++){ scanf("%d%d%lld",&s[i].L,&s[i].R,&s[i].x); b[i]=s[i].x; } sort(b+1,b+m+1); sz=unique(b+1,b+m+1)-(b+1); rt[0]=Build(1,sz+1); for(int i=1;i<=m;i++){ s[i].p=lower_bound(b+1,b+m+1,s[i].x)-b; vs[s[i].L].pb(mp(s[i].p,1)); vs[s[i].R+1].pb(mp(s[i].p,-1)); } for(int i=1;i<=n;i++){ rt[i]=rt[i-1]; for(int j=0;j<vs[i].size();j++){ pii &t=vs[i][j]; update(rt[i],rt[i],t.ft,b[t.ft],t.sc); } } } void solve(){ ll pre=1,X,A,B,C; int k; for(int i=1;i<=n;i++){ scanf("%lld%lld%lld%lld",&X,&A,&B,&C); k=(A*pre+B)%C+1; pre=(k>=rt[X]->cnt?rt[X]->sum:query(rt[X],k)); printf("%lld\n",pre); } } int main() { //freopen("data.in","r",stdin); init(); solve(); return 0; }
洛谷P3168 [CQOI2015]任務查詢系統