Hdu 4456 Crowd (two-dimensional tree array)

Source: Internet
Author: User

Hdu 4456 Crowd (two-dimensional tree array)

Link: hdu 4456 Crowd

Given N, then M operations

  • 1 x y z: Add z at the position of x and y
  • 2 x y z: Ask about the sum of points whose distance from Manhattan x and y is less than z.

    Solution: rotate the matrix 45 degrees, and then ask about a rectangle. You can use the refresh theorem to maintain a two-dimensional tree array, but the space is open.

    No. Use discretization to process useful points.

    #include 
        
         #include 
         
          #include using namespace std;const int maxn = 4000005;const int maxm = 80005;#define lowbit(x) ((x)&(-x))int N, M, W, E, H[maxn+5], fenw[maxn + 5];int O[maxm], X[maxm], Y[maxm], Z[maxm];inline int find (int x) {    return lower_bound(H + 1, H + E, x) - H;}void hashPoint (int x, int y) {    for (int i = x; i <= W; i += lowbit(i)) {        for (int j = y; j <= W; j += lowbit(j))            H[E++] = i * W + j;    }}void add(int x, int y, int d) {    for (int i = x; i <= W; i += lowbit(i)) {        for (int j = y; j <= W; j += lowbit(j)) {            int pos = find(i * W + j);            fenw[pos] += d;        }    }}int sum (int x, int y) {    int ret = 0;    for (int i = x; i; i -= lowbit(i)) {        for (int j = y; j; j -= lowbit(j)) {            int pos = find(i * W + j);            if (H[pos] == i * W + j)                ret += fenw[pos];        }    }    return ret;}void init () {    E = 1;    W = 2 * N;    scanf("%d", &M);    memset(fenw, 0, sizeof(fenw));    for (int i = 1; i <= M; i++) {        scanf("%d%d%d%d", &O[i], &X[i], &Y[i], &Z[i]);        int x = X[i] - Y[i] + N;        int y = X[i] + Y[i];        if (O[i] == 1)            hashPoint(x, y);    }    sort(H + 1, H + E);    E = unique(H + 1, H + E) - H;}void solve() {    for (int i = 1; i <= M; i++) {        int x = X[i] - Y[i] + N;        int y = X[i] + Y[i];        if (O[i] == 1)            add(x, y, Z[i]);        else {            int a = max(1, x - Z[i]);            int b = max(1, y - Z[i]);            int c = min(W, x + Z[i]);            int d = min(W, y + Z[i]);            printf("%d\n", sum(c, d) - sum(c, b-1) - sum(a-1, d) + sum(a-1, b-1));        }    }}int main () {    while (scanf("%d", &N) == 1 && N) {        init();        solve();    }    return 0;}
         
        

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.