【bzoj1176】[Balkan2007]Mokia
题目描述:
维护一个W*W的矩阵,初始值均为S.每次操作可以增加某格子的权值,或询问某子矩阵的总权值.修改操作数M<=160000,询问数Q<=10000,W<=2000000.
输入:
第一行两个整数,S,W;其中S为矩阵初始值;W为矩阵大小
接下来每行为一下三种输入之一(不包含引号):
"1 x y a"
"2 x1 y1 x2 y2"
"3"
输入1:你需要把(x,y)(第x行第y列)的格子权值增加a
输入2:你需要求出以左上角为(x1,y1),右下角为(x2,y2)的矩阵内所有格子的权值和,并输出
输入3:表示输入结束
输出:
对于每个输入2,输出一行,即输入2的答案
题解:
本蒟蒻的第一道cdq分治题。。。“cdq分治不就是归并排序吗?”写完这道题以后我对这句话有了更深的理解。
代码:
#include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #ifdef WIN32 #define LL "%I64d" #else #define LL "%lld" #endif #ifdef CT #define debug(...) printf(__VA_ARGS__) #define setfile() #else #define debug(...) #define filename "" #define setfile() freopen(filename".in", "r", stdin); freopen(filename".out", "w", stdout); #endif #define R register #define getc() (S == T && (T = (S = B) + fread(B, 1, 1 << 15, stdin), S == T) ? EOF : *S++) #define dmax(_a, _b) ((_a) > (_b) ? (_a) : (_b)) #define dmin(_a, _b) ((_a) < (_b) ? (_a) : (_b)) #define cmax(_a, _b) (_a < (_b) ? _a = (_b) : 0) #define cmin(_a, _b) (_a > (_b) ? _a = (_b) : 0) char B[1 << 15], *S = B, *T = B; inline int FastIn() { R char ch; R int cnt = 0; R bool minus = 0; while (ch = getc(), (ch < '0' || ch > '9') && ch != '-') ; ch == '-' ? minus = 1 : cnt = ch - '0'; while (ch = getc(), ch >= '0' && ch <= '9') cnt = cnt * 10 + ch - '0'; return minus ? -cnt : cnt; } #define maxn 200010 #define maxm 2000010 struct event { int x, y, pos, opet, ans; inline bool operator < (const event &that) const {return pos < that.pos ;} }t[maxn], q[maxn]; #define lowbit(_x) ((_x) & -(_x)) int bit[maxm], last[maxm], s, w, cnt, now; inline void add(R int x, R int val) { for (; x <= w; x += lowbit(x)) { if (last[x] != now) bit[x] = 0; bit[x] += val; last[x] = now; } } inline int query(R int x) { R int ans = 0; for (; x ; x -= lowbit(x)) { if (last[x] == now) ans += bit[x]; } return ans; } void cdq(R int left, R int right) { if (left == right) return ; R int mid = left + right >> 1; cdq(left, mid); cdq(mid + 1, right); ++now; for (R int i = left, j = mid + 1; j <= right; ++j) { for (; i <= mid && q[i].x <= q[j].x; ++i) if (!q[i].opet) add(q[i].y, q[i].ans); if (q[j].opet) q[j].ans += query(q[j].y); } R int i, j, k = 0; for (i = left, j = mid + 1; i <= mid && j <= right; ) { if (q[i].x <= q[j].x) t[k++] = q[i++]; else t[k++] = q[j++]; } for (; i <= mid; ) t[k++] = q[i++]; for (; j <= right; ) t[k++] = q[j++]; for (R int i = 0; i < k; ++i) q[left + i] = t[i]; } int main() { // setfile(); s = FastIn(); w = FastIn(); while (1) { R int opt = FastIn(); if (opt == 1) { R int x = FastIn(), y = FastIn(), a = FastIn(); q[++cnt] = (event){x, y, cnt, 0, a}; } if (opt == 2) { R int x = FastIn() - 1, y = FastIn() - 1, a = FastIn(), b = FastIn(); q[++cnt] = (event) {x, y, cnt, 1, x * y * s}; q[++cnt] = (event) {a, b, cnt, 2, a * b * s}; q[++cnt] = (event) {x, b, cnt, 2, x * b * s}; q[++cnt] = (event) {a, y, cnt, 2, a * y * s}; } if (opt == 3) break; } cdq(1, cnt); std::sort(q + 1, q + cnt + 1); for (R int i = 1; i <= cnt; ++i) if (q[i].opet == 1) printf("%d\n",q[i].ans + q[i + 1].ans - q[i + 2].ans - q[i + 3].ans ), i += 3; return 0; }