Topic Link: Click to open the link
CDQ Getting Started: Click the Open link
Idea: First of all, according to the above PPT know CDQ divided treatment:
Solve (L, mid);
Calculates the effect of [L,mid] on [mid+1, R] Intervals
Solve (mid+1, R);
Calculate the impact section, split the inquiry into 2, the X-sort after the can be engaged.
#include <stdio.h> #include <iostream> #include <algorithm> #include <sstream> #include < stdlib.h> #include <string.h> #include <limits.h> #include <vector> #include <string># Include <time.h> #include <math.h> #include <queue> #include <stack> #include <set># Include <map>const double eps = 1e-8;const double pi = ACOs ( -1.0); template <class t>inline BOOL Rd (T &ret) {char c; int sgn;if (c = GetChar (), c = = EOF) return 0;while (c! = '-' && (c< ' 0 ' | | c> ' 9 ')) C = GetChar (); s GN = (c = = '-')? -1:1;ret = (c = = '-')? 0: (C-' 0 '); while (c = GetChar (), C >= ' 0 ' &&c <= ' 9 ') ret = ret * + (C-' 0 '); ret *= Sgn;return 1;} Template <class t>inline void pt (T x) {if (x <0) {Putchar ('-'); x = x;} if (x>9) pt (X/10);p Utchar (x% 10 + ' 0 ');} Using namespace Std;typedef long Long ll;int c[500005], maxn;inline int lowbit (int x) {return x& (-X);} void Add (int i, int x) {WhilE (i <= MAXN) c[i] + = x,i + lowbit (i);} int sum (int x) {//Interval sum [1,x]int ans = 0;for (int i = x; i; i-= Lowbit (i)) ans + = C[i];return ans; const int N = 200005;struct q{int op, x1, y1, x2, y2, z, num, ans;} Q[n], Cc[n<<1];bool CMP (const q&x, const q&y) {if (x.x1 = = y.x1) return X.op < Y.op;return x.x1 < y.x1;} int W, n;int top;void solve (int l, int r) {if (L = = r) Return;int mid = (L + r) >> 1;top = 0;for (int i = l; I <= m Id i++) if (Q[i].op = = 1) cc[top++] = q[i];for (int i = mid + 1; I <= R; i++) {if (Q[i].op = = 2) {cc[top++] = q[i];cc[top++] = q[i];cc[top-2].x1--;cc[top-1].x1 = Q[i].x2;cc[top-1].op = 3;}} Sort (cc, CC + TOP, CMP), for (int i = 0; i < top; i++) {if (Cc[i].op = = 1) Add (cc[i].y1, cc[i].z); else if (Cc[i].op = = 2) q [Cc[i].num].ans-= SUM (cc[i].y2)-sum (cc[i].y1-1), Elseq[cc[i].num].ans + = SUM (cc[i].y2)-sum (cc[i].y1-1);} for (int i = 0; i < top; i++) if (Cc[i].op = = 1) Add (cc[i].y1,-cc[i].z); Solve (L, mid); Solve (mid + 1, r);} int main () {Freopen ("Locust.in", "R", stdin); Freopen ("Locust.out", "w", stdout); Rd (W); RD (n); maxn = w;for (int i = 1, x1, y1, x2, y2; i <= n; i++) {q[i].num = I;rd (Q[I].OP); if (Q[i].op = = 1) {rd (q[i].x1); Rd ( Q[I].Y1); RD (Q[I].Z);} else {rd (x1); Rd (Y1); Rd (x2); rd (y2); q[i].x1 = min (x1, x2); q[i].y1 = min (y1, y2); q[i].x2 = max (x1, x2); q[i].y2 = max (Y1, y2);}} Solve (1, n); for (int i = 1; I <= n; i++) if (Q[i].op = = 2) pt (Q[i].ans), Putchar (' \ n '); return 0;}
COGS 577 Locust CDQ Division + tree-like array