Topic Link: Portal
Question: static interval ask K small
Topic ideas: The whole two points, and poj2104 exactly the same explanation link: Portal
1#include <iostream>2#include <cstdio>3#include <cstdlib>4#include <cmath>5#include <algorithm>6#include <cstring>7#include <stack>8#include <cctype>9#include <queue>Ten#include <string> One#include <vector> A#include <Set> -#include <map> -#include <climits> the #defineLson Root<<1,l,mid - #defineRson Root<<1|1,mid+1,r - #defineFi first - #defineSe Second + #definePing (x, y) ((x-y) * (x-y)) - #defineMST (x, y) memset (x,y,sizeof (x)) + #defineMCP (x, y) memcpy (x,y,sizeof (y)) A using namespacestd; at #defineGamma 0.5772156649015328606065120 - #defineMOD 1000000007 - #defineINF 0x3f3f3f3f - #defineN 200005 - #defineMAXN 50005 -typedef pair<int,int>PII; intypedefLong LongLL; - to intn,m; + structnode{ - intid,l,r,v,f; the Node () {} *Node (intAintBintCintDinte) { $id=a;l=b;r=c;v=d;f=e;Panax Notoginseng } - }node[n],t1[n],t2[n]; the intTree[n],hcnt,ans[n]; + voidAddintIintV) { for(; i<=n;i+= (i&-i)) tree[i]+=v;} A intQueryinti) {intres=0; for(; i;i-= (i&-i)) res+=tree[i];returnRes;} the voidSolveintQlintQrintLintR) { + if(QL>QR)return; - if(l==R) { $ for(intI=ql;i<=qr;++i)if(node[i].f==2) $ans[node[i].id]=l; - return; - } the intMid=l+r>>1; - intlen1=0, len2=0;Wuyi for(inti=ql;i<=qr;++i) { the if(node[i].f==1){ - if(node[i].v<=mid) { WuAdd (Node[i].id,1); -t1[len1++]=Node[i]; About } $ Elset2[len2++]=Node[i]; - } - Else{ - intTemp=query (NODE[I].R)-query (node[i].l-1); A if(temp<node[i].v) { +node[i].v-=temp; thet2[len2++]=Node[i]; - } $ Elset1[len1++]=Node[i]; the } the } the for(intI=0; i<len1;++i)if(t1[i].f==1) Add (t1[i].id,-1); the for(intI=0; i<len1;++i) node[i+ql]=T1[i]; - for(intI=0; i<len2;++i) node[i+ql+len1]=T2[i]; inSolve (ql,ql+len1-1, l,mid); theSolve (ql+len1,qr,mid+1, R); the } About intMain () { the inti,j,group,x,y,v; the while(SCANF ("%d%d", &n,&m)! =EOF) { theHcnt=0; + int_min=inf,_max=-inf; - for(i=1; i<=n;++i) { thescanf"%d",&x);Bayi_max=max (_max,x); _min=min (_min,x); theNode[++hcnt]=node (I,1,1X1); the } - for(i=1; i<=m;++i) { -scanf"%d%d%d",&x,&y,&v); theNode[++hcnt]=node (I,x,y,v,2); the } theSolve1, Hcnt,_min,_max); the for(i=1; i<=m;++i) printf ("%d\n", Ans[i]); - } the return 0; the}
POJ2761 (Feed the Dogs)