HDU 4456 Crowd (two-dimensional tree-like array)

Source: Internet
Author: User

Title Link: Hdu 4456 Crowd

Topic: Given n, then m operations

    • 1 x y z: Add z at x, y position
    • 2 x y z: Ask for a point value with X, y Manhattan distance less than Z.

Problem-solving ideas: Rotate the Matrix 45 degrees, then ask is equal to ask a rectangle, you can use the tolerance theorem to maintain a two-dimensional tree array, but the space open

, directly with the discretization, will be useful to the point processing out.

#include <cstdio>#include <cstring>#include <algorithm>using namespace STD;Const intMAXN =4000005;Const intMAXM =80005;#Define lowbit (x) ((x) & ( ×))intN, M, W, E, h[maxn+5], FENW[MAXN +5];intO[MAXM], X[MAXM], Y[MAXM], Z[MAXM];inline intFind (intx) {returnLower_bound (H +1, H + E, x)-H;}voidHashpoint (intXintY) { for(inti = x; I <= W; i + = Lowbit (i)) { for(intj = y; J <= W;    J + = Lowbit (j)) h[e++] = i * W + j; }}voidAddintXintYintD) { for(inti = x; I <= W; i + = Lowbit (i)) { for(intj = y; J <= W; J + = Lowbit (j)) {intpos = Find (i * W + j);        Fenw[pos] + = D; }    }}intSUM (intXintY) {intRET =0; for(inti = x; I I-= Lowbit (i)) { for(intj = y; J J-= Lowbit (j)) {intpos = Find (i * W + j);if(H[pos] = = i * W + j) ret + = Fenw[pos]; }    }returnRET;}voidInit () {E =1; W =2Nscanf("%d", &m);memset(Fenw,0,sizeof(FENW)); for(inti =1; I <= M; i++) {scanf("%d%d%d%d", &o[i], &x[i], &y[i], &z[i]);intx = X[i]-y[i] + N;inty = X[i] + y[i];if(O[i] = =1) Hashpoint (x, y); } sort (H +1, H + E); E = unique (H +1, H + E)-H;}voidSolve () { for(inti =1; I <= M; i++) {intx = X[i]-y[i] + N;inty = X[i] + y[i];if(O[i] = =1) Add (x, Y, z[i]);Else{intA = max (1, X-z[i]);intb = Max (1, Y-z[i]);intc = min (W, x + z[i]);intd = min (W, y + z[i]);printf("%d\n", sum (c, D)-sum (c, B-1)-SUM (A-1, d) + sum (A-1, B-1)); }    }}intMain () { while(scanf("%d", &n) = =1&& N) {init ();    Solve (); }return 0;}

HDU 4456 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.