problem_id=1551 "> Topic links
Main topic:
The logarithm of the number in the interval less than or equal to 2 is calculated.
Thinking Analysis:
MO team algorithm.
Then analyze it.
Suppose you add a number. Then add it next to the number of the difference of 2.
Conversely lower one. It is necessary to reduce the number of the difference between 2 and. Subtract yourself again this 1.
#include <cstdio> #include <iostream> #include <cstring> #include <algorithm> #include < Cmath> #define MAXN 100005using namespace Std;int app[maxn];int save[maxn];int pos[maxn];struct foo{int l,r,index; int ans; BOOL operator < (const foo &CMP) const {if (pos[l] = = POS[CMP.L]) return r<cmp.r; Return pos[l]<pos[cmp.l]; }}q[maxn];bool cmp_id (const foo &a, const foo &b) {return a.index < B.index;} void Debug () {for (int i=0;i<=7;i++) printf ("%d", app[i]); Puts ("");} void Modify (int p,int &ans,int add) {int tot=0; for (int i=max (save[p]-2,0); i<=save[p]+2;i++) {tot+=app[i]; } if (add>0) Ans+=tot; else ans-=tot-1; App[save[p]]+=add;} int main () {int n,m; int Cas=1; while (scanf ("%d%d", &n,&m)!=eof) {int size= (int) sqrt (1.0*n); memset (app,0,sizeof app); for (int i=1;i<=n;i++) {scanf ("%d", &savE[i]); pos[i]= (i-1)/size+1; } for (int i=0;i<m;i++) {scanf ("%d%d", &Q[I].L,&Q[I].R); Q[i].index=i; } sort (q,q+m); int ans=0; for (int i=0,l=1,r=0;i<m;i++) {if (R<Q[I].R) {for (r=r+1;r<=q[i].r;r++ ) modify (r,ans,1); r--; } if (R>Q[I].R) {for (; r>q[i].r;r--) modify (r,ans,-1); } if (L<Q[I].L) {for (; l<q[i].l;l++) modify (l,ans,-1); } if (L>Q[I].L) {for (l=l-1;l>=q[i].l;l--) modify ( l,ans,1); l++; } Q[i].ans=ans; } sort (q,q+m,cmp_id); printf ("Case%d:\n", cas++); for (int i=0;i<m;i++) printf ("%d\n", Q[i].ans); } return 0;}
WHU OJ 1551 Pairs (Mo team algorithm)