http://www.tsinsen.com/A1333
Test instructions: ...
Idea: The same as the previous K-small, but the one-dimensional bit is replaced by a two-dimensional bit. Note the two-dimensional bit notation Qaq
1#include <cstdio>2#include <algorithm>3#include <iostream>4#include <cstring>5#include <string>6#include <cmath>7#include <queue>8#include <vector>9#include <map>Ten#include <Set> One#include <stack> A using namespacestd; - #defineINF 0x3f3f3f3f - #defineN 350000 thetypedefLong LongLL; - structP { - intx1, x2, y1, Y2, Val, id; - P () {} +PintX1,intY1,intX2,intY2,intValintID): X1 (x1), y1 (y1), X2 (x2), y2 (y2), Val (val), id (ID) {} - } Q[n], lq[n], rq[n]; + intbit[505][505], ans[n], N; A at intLowbit (intx) {returnX & (-x); } - - voidUpdateintXintYintW) { - for(inti = x; I <= N; i + =lowbit (i)) - for(intj = y; J <= N; J + = Lowbit (j)) Bit[i][j] + =W; - } in - intQueryintXinty) { to intAns =0; + for(inti = x; I I-=lowbit (i)) - for(intj = y; J J-= Lowbit (j)) ans + =Bit[i][j]; the returnans; * } $ Panax Notoginseng voidSolve (intLaskintRaskintLintr) { - if(Lask > Rask | | l > R)return ; the if(L = =r) { + for(inti = Lask; I <= Rask; i++)if(q[i].id) Ans[q[i].id] =l; A return ; the } + intMid = (L + r) >>1, lcnt =0, rcnt =0; - for(inti = Lask; I <= Rask; i++) { $ if(!q[i].id) { $ if(Q[i].val <=mid) { -Update (Q[I].X1, Q[i].y1,1); -LQ[++LCNT] =Q[i]; the}ElseRQ[++RCNT] =Q[i]; -}Else {Wuyi intnum = Query (q[i].x2, Q[i].y2)-Query (Q[I].X1-1, q[i].y2)-Query (Q[I].X2, Q[i].y1-1) + Query (Q[I].X1-1, Q[i].y1-1); the if(Num >= q[i].val) lq[++lcnt] =Q[i]; - Else { WuQ[i].val-=num; -RQ[++RCNT] =Q[i]; About } $ } - } - for(inti =1; I <= lcnt; i++)if(!lq[i].id) Update (LQ[I].X1, Lq[i].y1,-1); - for(inti =1; I <= lcnt; i++) q[lask+i-1] =Lq[i]; A for(inti =1; I <= rcnt; i++) q[lask+lcnt+i-1] =Rq[i]; +Solve (Lask, Lask + lcnt-1, L, mid); theSolve (Lask + lcnt, Rask, Mid +1, R); - } $ the intMain () { the intM, cnt =0, A; thescanf"%d%d", &n, &m); thememset (bit,0,sizeof(bit)); - for(inti =1; I <= N; i++) in for(intj =1; J <= N; J + +) { thescanf"%d", &a); Q[++CNT] = P (i, J,0,0A0); the } About for(inti =1; I <= m; i++) { the++cnt; Q[cnt].id =i; thescanf"%d%d%d%d%d", &q[cnt].x1, &q[cnt].y1, &q[cnt].x2, &q[cnt].y2, &q[cnt].val); the } +Solve (1Cnt1, INF); - for(inti =1; I <= m; i++) printf ("%d\n", Ans[i]); the}
Tsinsen A1333: Matrix multiplication (whole dichotomy)