Topics 3236: [Ahoi2013] Job time limit:100 Sec Memory limit:512 MB
submit:732 solved:271
Description
Input
Output
Sample Input3 4
1 2 2
1 2 1 3
1 2 1 1
1 3 1 3
2 3 2 3
Sample Output2 2
1 1
3 2
2 1HINT
n=100000,m=1000000
Source
by wangyisong1996 strengthening data
Solving
A good problem of card evaluation! Using the legendary MO team algorithm can water over, that is, we regard each inquiry x0,y0 as a point on the plane, then the price of violent transfer is the distance between two points, we divide the x-axis, so the total cost of transfer is O (sqrt (n) *n). Since this problem requires a tree-like array to maintain, the complexity of this problem is O (nsqrt (n) logn).
Code
1 /*Author:wnjxyk*/2#include <cstdio>3#include <algorithm>4 using namespacestd;5 6 Const intmaxm=1000000;7 structquestionnode{8 intL,r;9 intb;Ten intindex; One }; AQuestionnode quest[maxm+Ten]; - structansnode{ - intAns1,ans2; the }; -Ansnode ans[maxm+Ten]; - - Const intmaxn=100000; + intcnt[maxn+Ten]; - + intsiz; A intn,m; at intnum[maxn+Ten]; - - intsum1[maxn+Ten],sum2[maxn+Ten],cot[maxn+Ten]; - -InlineintRemin (intAintb) { - if(A<B)returnA; in returnb; - } to +InlineBOOLCMP (Questionnode A,questionnode b) { - if(CNT[A.L]<CNT[B.L])return true; the if(CNT[A.L]==CNT[B.L] && A.R<B.R)return true; * return false; $ }Panax Notoginseng -InlineintLowbit (intx) { the returnx&-x; + } A theInlinevoidAddintC[],intXintnum) { + for(intI=x;i<=maxn;i+=lowbit (i)) c[i]+=num; - } $ $Inlineint Get(intC[],intx) { - intres=0; - for(intI=x;i;i-=lowbit (i)) res+=C[i]; the returnRes; - }Wuyi the intMain () { -scanf"%d%d",&n,&m); Wu //getcnt - while(siz*siz<n) siz++; About for(intI=1; i<=siz;i++){ $ intst=siz* (I-1)+1, Ed= (Remin (siz*i,n)); - for(intj=st;j<=ed;j++){ -cnt[j]=i; - } A } + //Getnum the for(intI=1; i<=n;i++) scanf ("%d",&num[i]); - //getquest $ for(intI=1; i<=m;i++) scanf ("%d%d%d%d",&quest[i].l,&quest[i].r,&quest[i].a,&quest[i].b); the for(intI=1; i<=m;i++) quest[i].index=i; the //sortquest theSort (quest+1, quest+m+1, CMP); the //Getans - intleft=1, right=0; in for(intI=1; i<=m;i++){ the intL=quest[i].l,r=QUEST[I].R; the inta=quest[i].a,b=quest[i].b; About intindex=Quest[i].index; the while(left<l) { thecot[num[left]]--; theAdd (sum1,num[left],-1); + if(cot[num[left]]==0) Add (sum2,num[left],-1); -left++; the }Bayi while(l<Left ) { theleft--; thecot[num[left]]++; -Add (Sum1,num[left],1); - if(cot[num[left]]==1) Add (Sum2,num[left],1); the } the while(right<S) { theright++; thecot[num[right]]++; -Add (Sum1,num[right],1); the if(cot[num[right]]==1) Add (Sum2,num[right],1); the } the while(r<Right ) {94cot[num[right]]--; theAdd (sum1,num[right],-1); the if(cot[num[right]]==0) Add (sum2,num[right],-1); theright--;98 } AboutAns[index]. ans1=Get(SUM1,B)-Get(sum1,a-1); -Ans[index]. Ans2=Get(SUM2,B)-Get(sum2,a-1);101 }102 //Print103 for(intI=1; i<=m;i++) printf ("%d%d\n", Ans[i]. Ans1,ans[i]. ANS2);104 return 0; the}
View Code
Bzoj 3236: [Ahoi2013] Jobs