http://acm.fzu.edu.cn/problem.php?pid=2226
Confidence question, also say is the confidence question, the topic gives really good. But it's not like a confidence question.
is a new algorithm, MO team algorithm
MO Team algorithm is an array can be easily implemented by the magic data structure, can deal with a class of non-modified offline interval query problem (PS: temporarily have not met Mo team to solve the problem of update interval query)
The MO team algorithm can be in O (1), the implementation of [L, R] to [L, r±1]/[l±1, R] Transfer, and then we can be divided into all queries sqrt (n) blocks, each query where the block number as the first keyword, the right end point as the second keyword in ascending order.
Then the state transfer can be done.
Time Complexity O (n*sqrt (n)): When I and i+1 are within the same block, L moves up to sqrt (n) and R moves N at most, so the complexity is O (n*sqrt (n)).
When I is not in the same block as i+1, L moves up to 2*SQRT (n), R moves N at most, and the complexity is O (n*sqrt (n)).
------Rosie.
#include <stdio.h>#include<string.h>#include<stdlib.h>#include<math.h>#include<algorithm>#include<iostream>#include<vector>using namespacestd;#defineN 1001#defineMemset (A, B) memset (A,b,sizeof (a))Vector<int>G[n];structnode{intL,r,id;} Q[n*Ten];intn,q,l[n],r[n],ans[n*Ten];inta[n* -];intCMP (node C,node d) {if(c.l!=d.l)returnc.l<D.l; Else returnc.r<D.R;}voidsolve () {intL=0, r=0; for(intI=1; i<=q;i++) { while(q[i].l<l) {L--; L[A[L]]--; } while(q[i].l>L) {L[a[l]]++; L++; } while(q[i].r<R) {R[a[r]]--; R--; } while(q[i].r>R) {R++; R[A[R]]++; } intmax=0; for(intj=1; j<= +; j + +) { if(r[j]<=0) Continue; intu=G[j][r[j]]; intv=g[j][l[j]+1]; Max=max (max,g[j][r[j]]-g[j][l[j]+1]); } Ans[q[i].id]=Max; }}intMain () { while(SCANF ("%d", &n)! =EOF) { for(intI=1; i<= +; i++) g[i].clear (); for(intI=1; i<=n;i++) {scanf ("%d",&A[i]); G[a[i]].push_back (i); } memset (L,-1); Memset (R,-1); scanf ("%d",&q); for(intI=1; i<=q;i++) {scanf ("%d%d",&q[i].l,&Q[I].R); Q[i].id=i; } sort (Q+1, q+1+q,cmp); Solve (); for(intI=1; i<=q;i++) {printf ("%d\n", Ans[i]); } } return 0;}/*Wuyi 1 1 1 131 5*/
Confidence Problem--fuoj2226 (Mo team algorithm)