MO Team Algorithm
Today in order to do this problem first to learn the MO team algorithm, then a dropped the MO Team Algorithm introduction topic-Small Z socks.
In the examination room above I was a fool to hit a high-precision, gorgeous lost violence points. However I found that only need to take a mold on it, the examination room silly.
After learning the MO team algorithm, found that the problem is actually a naked question.
The first is still the MO team algorithm, according to the left end of the block is the number of a keyword, the right end number of the second keyword to sort,
Consider the first set of queries for the violent handling of each block before we can find that we only need to fine-tune the left and right end points of the interval. For example: I last processed 1 5, then 1 6 can just add 6 into it, if this time is 2 6, then I need to remove the 1.
Consider how to transfer: first discretization of the remainder, record [L, R] This number appears several times, interval length ± 1 o'clock obviously the answer to change the value of the remainder of the point of the original occurrence number (assuming the remainder of the first number is 2, with cnt[2] represents the number of the remainder of 2 in the current interval occurrence, Then remove 1 after the answer to change the value of cnt[2], their own yy should be able to make sense of it).
And then it seems to make sense, ac?! Well, actually it's kind of a question for people who don't have a card for us, in fact P = 2 or P = 5 can be stuck out of the case
That's a good one. Two arrays representing the number of cases in multiples of 2 or 5 in [1, I], and how many are at the end of a multiple of 2 or 5, prefixed and maintained.
This will make the AC perfect, and you can get rid of the new data on the Bzoj.
#include <iostream>#include<cstdio>#include<cstring>#include<cmath>#include<cstdlib>#include<algorithm>#include<queue>#include<map>#include<vector>using namespaceStd;typedefLong LongLL;Const intMAXN =100011;Const intMAXM =100011;intp,m;intN;CharCH[MAXN]; LL ANS[MAXN]; LL MI[MAXN];intsize;intBELONG[MAXN]; LL HOU[MAXN]; LL Cnt[maxn]ll C[MAXN];intTot//È¥öøºóµä³¤¶èLL now; LL JISUAN1[MAXN],JISUAN2[MAXN];structwen{intL,r,jilu; intK;} Q[maxm];inlineintGetint () {CharC=getchar ();intw=0, q=0; while((c<'0'|| C>'9') && c!='-') c=GetChar (); if(c=='-') C=getchar (), q=1; while(c<='9'&& c>='0') w=w*Ten+c-'0', c=GetChar (); returnq?-w:w;}BOOLCMP (Wen Q,wen qq) {if(Q.K==QQ.K)returnq.r<qq.r;returnq.k<QQ.K;} Inlinevoidinit () {Mi[n]=1; for(inti=n-1; i>=1; i--) mi[i]=mi[i+1]*Ten%p; Size=SQRT (n);if(n!=size*size) size++; Hou[n+1]=0; for(inti=n;i>=1; i--) {Belong[i]= (I-1)/size+1; Hou[i]=hou[i+1]+ (ch[i]-'0') *mi[i]; hou[i]%=p; } memcpy (C,hou,sizeof(HOU)); Sort (c+1, c+n+1+1); Tot=unique (c+1, c+n+1+1)-c-1; for(intI=1; i<=n+1; i++) Hou[i]=lower_bound (c+1, c+n+1+1, Hou[i])-C; M=Getint (); for(intI=1; i<=m;i++) {Q[I].L=getint (), Q[i].r=getint (), q[i].k=belong[q[i].l],q[i].jilu=i; Q[I].R++;//x¢òâ¼óò»}}inlinevoidUpdateintXinttype) { Now-=cnt[hou[x]]* (cnt[hou[x]]-1)/2; CNT[HOU[X]]+=type; now+=cnt[hou[x]]* (cnt[hou[x]]-1)/2;} InlinevoidWork () {intL=1, r=0; Sort (Q+1, q+m+1, CMP); for(intI=1; i<=m;i++) { for(; r<q[i].r;r++) Update (r+1,1); for(; r>q[i].r;r--) Update (r,-1); for(; l<q[i].l;l++) Update (l,-1); for(; l>q[i].l;l--) Update (L-1,1); Ans[q[i].jilu]=Now ; } for(intI=1; i<=m;i++) printf ("%lld\n", Ans[i]);} InlinevoidSolve () {//¼çò»ïâç°xººím=Getint (); for(intI=1; i<=n;i++) {Jisuan1[i]=jisuan1[i-1]; jisuan2[i]=jisuan2[i-1]; if((ch[i]-'0')%p==0) {Jisuan1[i]++; Jisuan2[i]+=i; } //jisuan1[i]=jisuan1[i-1]+ ((ch[i]-' 0 ')%p==0), jisuan2[i]=jisuan2[i-1]+ (((ch[i]-' 0 ')%p==0)? i:0); } for(intI=1; i<=m;i++) { intX=getint (), y=Getint (); printf ("%lld\n", jisuan2[y]-jisuan2[x-1]-(X1) * (jisuan1[y]-jisuan1[x-1])); }}intMain () {//freopen ("number.in", "R", stdin); //freopen ("Number.out", "w", stdout);p=Getint (); scanf ("%s", ch+1); N=strlen (ch+1); //scanf ("%s", ch); N=strlen (CH); //for (int i=n;i>=1;i--) ch[i]=ch[i-1]; if(p!=2&& p!=5) {init (); Work (); } Elsesolve (); return 0;}
HNOI2016 Day2 T3 large number (BZOJ4542)