hdu4456 Crowd (two-dimensional tree-like array)

Source: Internet
Author: User

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. When a query is found, the effective point is actually a matrix.

Then it becomes a single point of change. Ask for the matrix and the problem.

We consider the practice of a bare two-dimensional tree-like array. It will be found that the matrix is too large, but notice that the initial time, all the values in the matrix are 0, then this two-dimensional tree array. The effective point is the point at which the change is made, and the interval in which these points are controlled. The total number of States here is only M*logn*logn. So we take the operation out and map the changed 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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.