BZOJ3932[CQOI2015] Task query system
Test instructions
The M task, task (SI,EI,PI) indicates that the task starts from Si seconds, ends in the first Ei seconds, and has a priority of Pi. n queries, the sum of the priority of the KI task with the smallest priority is the number of tasks that are being run each time you ask for the first XI second. If Ki is greater than the total number of tasks that are running for the first XI second, the sum of the priority of the task is output. m,n≤100000, Force online.
Exercises
The first time I wrote the Chairman tree ... (Because I didn't completely understand the yyl. D: You don't understand the Chairman tree at all) The Chairman tree is essentially a weight segment tree + Reuse node.
Reflected in the subject, is to give each time point to build a tree of weight segment, but this will be mle, so we first split all the tasks into "Si to n time point of weight +pi" and "Ei+1 to n time point of weight + (-PI), and then by the point of insertion of the order of time, before each insert operation, Copy the root node pointer of the weight segment tree from the previous operation, and then insert only the newly opened node to record the modified node, because one insertion only affects log2n nodes, so the total space complexity is O (nlog2n), At the same time, the time complexity is O (nlog2n) because the point at which a pointer is inserted is LOG2N, and the complexity of each insertion time is as long as it is two points apart. The subject can not be discretized, but I am more counseling so it is still a bit of separation of space.
Code:
1#include <cstdio>2#include <cstring>3#include <algorithm>4#include <queue>5 #defineInc (I,J,K) for (int i=j;i<=k;i++)6 #defineMAXN 4000007 #definell Long Long8 using namespacestd;9 Ten structopt{intA,b,c,id;}; Opt opts[maxn*2]; One BOOLCMP1 (opt a,opt b) {returna.b<b.b;} A BOOLCMP2 (opt a,opt b) {returna.a<B.A;} - intlc[ -*maxn],rc[ -*maxn],rt[maxn],sz[ -*MAXN],N,M,VALN,TOT,OPTN; -ll sm[ -*MAXN]; the voidBuildint&x,intLintR) { -X=++tot; lc[x]=rc[x]=sz[x]=sm[x]=0;if(L==R)return; - intMid= (l+r) >>1; Build (Lc[x],l,mid); Build (rc[x],mid+1, R); - } + voidInsint&x,intLintRintYintZ1,intZ2) { -tot++; sm[tot]=sm[x]+ (LL) (Z1*Z2); sz[tot]=sz[x]+Z2; +LC[TOT]=LC[X]; RC[TOT]=RC[X]; X=tot;if(L==R)return;intMid= (l+r) >>1; A if(y<=mid) ins (LC[X],L,MID,Y,Z1,Z2);ElseINS (rc[x],mid+1, R,Y,Z1,Z2); at } -ll query (intx,ll k) { -ll q=0;inty=x; - while(1){ - if(K>=sz[y]) {Q+=sm[y];returnQ;}if(!lc[y]&&!rc[y]) {q+=sm[y]/sz[y]*k;returnq;} - if(K==sz[lc[y]]) {Q+=sm[lc[y]];returnq;} in if(K<sz[lc[y]]) y=lc[y];Elsek-=sz[lc[y]],q+=sm[lc[y]],y=Rc[y]; - } to } + intMain () { -scanf"%d%d", &m,&n); optn=0; theInc (I,1, M) { * intA,b,c; scanf"%d%d%d",&a,&b,&c); $Opts[++optn]= (opt) {a,c,1,0};if(b!=n) opts[++optn]= (opt) {B +1, c,-1,0};Panax Notoginseng } -Sort (opts+1, opts+1+OPTN,CMP1); valn=1; opts[1].id=1; theInc (I,2, OPTN) {if(opts[i].b!=opts[i-1].B) Opts[i].id=++valn;ElseOpts[i].id=Valn;} +tot=0; Build (rt[0],1, VALN); Sort (opts+1, opts+1+OPTN,CMP2); opts[optn+1].a=N; A for(intI=1; i<=opts[1].a&&i<=n;i++) rt[i]=rt[i-1]; theInc (I,1, OPTN) { +Ins (rt[opts[i].a],1, valn,opts[i].id,opts[i].b,opts[i].c); - for(intj=opts[i].a+1; j<=opts[i+1].a&&j<=n;j++) rt[j]=rt[j-1]; $ } $ll last=1; -Inc (I,1, N) { - intA;ll b,c,d; scanf"%d%lld%lld%lld",&a,&b,&c,&d); theLast=query (Rt[a],1+ (B*LAST+C)%d); -printf"%lld\n", last);Wuyi } the return 0; -}
20160516
BZOJ3932[CQOI2015] Task query system