Test instructions: Give n number, M inquire. Each query is an interval in which the absolute value of the interval difference is 1 of the number logarithm.
The puzzle: First discretization, then the MO team algorithm. The team is an offline algorithm, sorted by the left end of the query, sorted by the right endpoint.
PS: First write MO team, said is quite simple, but this problem before the mess has been tle, MO team is still very strong.
Code:
#include <algorithm>#include<iostream>#include<cstdio>#include<cmath>#include<cstring>using namespaceStd;typedefLong Longll;structNode {intVal; intPOS; BOOL operator< (ConstNode x)Const { returnVal <X.val; }} a[10005];intb[10005], c[10005];inttmp[30000];ll so[100005];structQuery {intL, R, id; BOOL operator< (ConstQuery x)Const { if(L = = x.l)returnR <X.R; returnL <X.L; }} q[100005];intUpdateintXintd) { intAns = d * ((tmp[x+1] + tmp[x-1])); if(D <0) tmp[x]--;Elsetmp[x]++; returnans;}//I'm a good cook.intMain () {//freopen ("In.txt", "R", stdin); intN, M; while(~SCANF ("%d%d", &n, &m)) { for(inti =1; I <= N; ++i) {scanf ("%d", &a[i].val); A[i].pos=i; } sort (A+1, A +1+N); b[1] =1; for(inti =2; I <= N; ++i) {if(A[i].val = = a[i-1].val) B[i] = b[i-1]; Else if(A[i].val = = a[i-1].val +1) B[i] = b[i-1]+1; ElseB[i] = b[i-1] +2; } for(inti =1; I <= N; ++i) {c[A[i].pos]=B[i];} memset (tmp,0,sizeoftmp); for(inti =0; I < m; ++i) {scanf ("%d%d", &q[i].l,&Q[I].R); Q[i].id=i; } sort (q, q+m); intPL =1, PR =0; ll ans=0; for(inti =0; I < m; ++i) {intID =q[i].id; intL =Q[I].L; intR =Q[I].R; if(PR < R) for(inti = pr+1; I <= R; ++i) ans + = Update (C[i],1); Else for(inti = PR; i > R; -i) ans + = Update (C[i],-1); if(PL < L) for(inti = pl; I < L; ++i) ans + = Update (C[i],-1); PR= r, pl =l; So[id]=ans; } for(inti =0; I < m; ++i) printf ("%lld\n", So[i]); } return 0;}
CSU 1515 Sequence (Mo team algorithm)