51nod 1290 counting Diff Pairs | Mo Team Tree-like array
Surface
A positive integer of length n array A, given a number K and q query, each query contains 2 number L and r, for each query output from a[i] to a[j], the number of logarithm, ABS (A[i]-a[j]) <= K (ABS represents absolute value).
Exercises
MO Team! In fact, I was searching for "51nod + MO Team" to find this problem ...
Level Seven algorithm problem!
320 points!
You deserve it!
The puzzle is ... It would be nice to use a normal team, plus a tree-like array to count the number of matches.
When adding/deleting a number, the statistic can make up the number of valid number pairs and then increment/subtract the answer accordingly.
#include <cstdio>#include <cmath>#include <cstring>#include <algorithm>using namespaceStdtypedef Long LongllTemplate<classT>voidRead (T &x) {CharCBOOLOP =0; while(c = GetChar (), C <' 0 '|| C >' 9 ')if(c = ='-') op =1; x = C-' 0 '; while(c = GetChar (), C >=' 0 '&& C <=' 9 ') x = x *Ten+ C-' 0 ';if(OP) x =-X.;}Template<classT>voidWrite (T x) {if(X <0) Putchar ('-'), x =-X;if(x >=Ten) Write (X/Ten); Putchar (' 0 '+ x%Ten);}#define Space Putchar (")#define ENTER Putchar ('\n ')Const intN =50005, B =233;intN, D, M, A[n], lst[n], IDX, tol[n], Tor[n],TR[N], pl =1, Pr;ll Res, ans[n];#define BEL (x) (((x)-1)/B + 1)structQuery {intID, L, R;BOOL operator< (ConstQuery &b)Const{returnBel (l) = = Bel (B.L)? R < B.r:l < B.L; }} Q[n];voidInit () {sort (lst +1, LST + n +1); IDX = unique (LST +1, LST + n +1)-LST-1; for(inti =1; I <= N; i++) A[i] = Lower_bound (LST +1, LST + idx +1, A[i])-lst;intL =1, r =1; for(inti =1; I <= idx; i++) { while(L < i && Lst[i]-lst[l] > D) l++; while(R < idx && Lst[r +1]-lst[i] <= D) r++; Tol[i] = l, tor[i] = R; }}voidAddintPintx) { while(P <= idx)TR[P] + = x, p + = P & p;}intAskintP) {intres =0; while(p) Res + =TR[P], P-= p & p;returnRes;}intGetres (intx) {returnAsk (Tor[x])-Ask (Tol[x]-1);}intMain () {read (n), read (d), read (m); for(inti =1; I <= N; i++) read (A[i]), lst[i] = a[i]; Init (); for(inti =1; I <= m; i++) Q[i].id = i, read (Q[I].L), q[i].l++, read (Q[I].R), q[i].r++; Sort (q +1, q + M +1); for(inti =1; I <= m; i++) { while(PL > Q[i].l) res + = Getres (A[--PL]), add (A[PL],1); while(PR < Q[I].R) Res + = Getres (A[++PR]), add (A[PR],1); while(PL < Q[I].L) Add (A[PL],-1), Res-= Getres (a[pl++]); while(pr > Q[I].R) Add (A[PR],-1), Res-= Getres (a[pr--]); Ans[q[i].id] = res; } for(inti =1; I <= m; i++) write (Ans[i]), enter;return 0;}
51nod 1290 counting Diff Pairs | Mo Team Tree-like array