Test instructions: Gives a n*n matrix, and then M operation,1 represents the coordinates (x, y) value plus z,2 represents the weights and the points with coordinates (x, y) of Manhattan that are not more than Z.
Problem-solving ideas: The matrix side over 45 degrees, found that when asked, the effective point of the composition is actually a matrix. Then it becomes a single point of modification, the problem of finding matrices. We consider the practice of a bare two-dimensional tree array, and we find that the matrix is too large, but notice that, initially, all of the values in the matrix are 0, so the valid points in this two-dimensional tree array are the ones that are modified, and the intervals that govern those points. The total number of States here is only m*logn*logn, so we take the operation out and map the modified state to the sequence.
Code:
#include <stdio.h> #include <string.h> #include <algorithm> #include <vector> #define LOWBIT (x) (x& (-X)) using namespace std; struct Operation {int op, x, Y, Z;} p[88888]; int base = 100000; const int N = 22 22222;vector<int> MP; int val[n], tot, N, m; const int MAXN = 22222; inline int get (int x, int y) {x = x * base + y; y = Lower_bound (Mp.begin (), Mp.end (), X)-Mp.begin (); if (y = = Tot | | mp[y]! = x) return-1; return y;} void update (int x, int y, int z) {while (X < n*2+100) {int pos = y; while (Pos < n*2+100) {Int. now = Get (X,pos); Val[now] + = Z; pos + = Lowbit (POS); } x + = Lowbit (x); }}int sum (int x, int y) {int ret = 0; while (x) {int pos = y; while (POS) {Int. now = Get (x, POS); if (now! =-1) ret + = Val[now]; pos-= Lowbit (POS); } X-= Lowbit (x); } return ret;} int query (int x1, int y1, int x2, int y2) {int ret1 = SUM (x1-1,y1-1), Ret2 = SUM (x2,y2), Ret3 = SUM (x1-1,y2), Ret4 = SUM (x2,y1-1);//printf ("Ret2 =%d\n", Ret2); return RET1+RET2-RET3-RET4;} int Get_num () {int n = 0, flag = 1; char c; while ((c = GetChar ()) && c! = '-' && (c< ' 0 ' | | C> ' 9 ')); if (c = = '-') flag =-1; else n = C-' 0 '; while ((c = GetChar ()) && C >= ' 0 ' && C <= ' 9 ') n = n * + C-' 0 '; return n * FLAG; void Add (int t, int x, int y) {while (X < n*2+100) {int pos = y; while (Pos < n*2+100) {//if (tot = = N-1) while (1); Mp.push_back (x * base + POS); pos + = Lowbit (POS); } x + = Lowbit (x); }}void read (int i) {p[i].op = Get_num (); p[i].x = Get_num (); P[I].Y = Get_num (); P[i].z = Get_num (); Intx = p[i].x + p[i].y-1, y = n-p[i].x + p[i].y; if (P[i].op = = 1) Add (1, x, y);} int main () {while (scanf ("%d", &n) && N) {scanf ("%d", &m); Mp.clear (); Memset (val, 0, sizeof (Val)); for (int i = 1; I <= m; i + +) read (i); Sort (Mp.begin (), Mp.end ()); Vector<int>::iterator it = unique (Mp.begin (), Mp.end ()); Mp.erase (It,mp.end ()); Tot = Unique (MP + 1, MP + tot + 1)-mp-1; tot = Mp.size (); for (int i = 1; I <= m; i + +) {int op, x, Y, Z; op = p[i].op; x = p[i].x; y = p[i].y; z = p[i].z; printf ("Op =%d, x =%d, y =%d, z =%d\n", op, x, Y, p[i].z); int xx = x + y-1, yy = N-x + y; if (op = = 1) {update (xx, yy, z); } else {int x1 = Std::max (1, xx-z); int y1 = Std::max (1, yy-z); int x2 = std::min (2*n-1, XX + z); int y2 = std::min (2*n-1, yy + z); printf ("%d\n", Query (x1, y1, x2, y2)); }}} return 0;}
hdu4456 Crowd (two-dimensional tree-like array)