HDU4456 坐标转换+线段树+离散化


Crowd

Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2190    Accepted Submission(s): 505


Problem Description
City F in the southern China is preparing lanterns festival celebration along the streets to celebrate the festival. 
Since frequent accidents had happened last year when the citizens went out to admire the colorful lanterns, City F is planning to develop a system to calculate the degree of congestion of the intersection of two streets. 
The map of City F is organized in an N×N grid (N north-south streets and N west-east street). For each intersection of streets, we define a density value for the crowd on the intersection. 
Initially, the density value of every intersection is zero. As time goes by, the density values may change frequently. A set of cameras with new graphical recognition technology can calculate the density value of the intersection easily in a short time.
But the administrator of the police office is planning to develop a system to calculate the degree of congestion. For some consideration, they come up with a conception called "k-dimension congestion degree". The "k-dimension congestion degree" of intersection (x0,y0) is represented as "c(x0,y0,k)", and it can be calculated by the formula below:

Here, d(x,y) stands for the density value on intersection (x,y) and (x,y) must be in the N×N grid. The formula means that all the intersections in the range of manhattan distance k from (x0,y0) effect the k-dimension congestion degree of (x0,y0) equally, so we just simply sum them up to get the k-dimension congestion degree of (x0,y0). 
The figure below shows a 7×7 grid, and it shows that if you want to get the 2-dimension congestion degree of intersection (4,2),you should sum up the density values of all marked intersections.
HDU4456 坐标转换+线段树+离散化_第1张图片

 

Input
These are multiple test cases. 
Each test case begins with a line with two integers N, M, meaning that the city is an N×N grid and there will be M queries or events as time goes by. (1 ≤ N ≤10 000, 1 ≤ M ≤ 80 000) Then M lines follow. Each line indicates a query or an event which is given in form of (p, x, y, z), here p = 1 or 2, 1 ≤ x ≤ N, 1 ≤ y ≤ N. 
The meaning of different p is shown below.
1. p = 1 the value of d(x,y) is increased by z, here -100 ≤ z ≤ 100.
2. p = 2 query the value of c(x,y,z), here 0 ≤ z ≤ 2N-1.
Input is terminated by N=0.
 

Output
For each query, output the value for c(x,y,z) in a line.
 

Sample Input
 
       
8 5 1 8 8 1 1 1 1 -2 2 5 5 6 1 5 5 3 2 2 3 9 3 2 1 3 2 -9 2 3 2 0 0
 

Sample Output
 
       
1 1 -9
 

Source
2012 Asia Hangzhou Regional Contest
 


题目大意:给你若干个点,每个点有个权值。两种操作:1.修改某个点的权值2.求 

解题思路:将每个点逆时针旋转45度,然后将坐标扩大sqrt(2)倍,目的是为了将点坐标变成整数。旋转之后有个好处,直接询问以这个点为中心2*k为长度的矩形内权值的和就可以了。

第二个难点就是离散化了原来的树状数组的bit[i][j]开不下怎么办?对于每个i,j我只需要知道他们对应的值就可以了,所以存在二维的数组里面是最简单的一种方式。二维数组存不下那我就存在一维数组里面。另开一个数组存i*w+j,排序后i*w+j的位置pos和i,j是一一对应的。那我就用pos去存i,j对应的值。

#include 
#include 
#include 
#include 
using namespace std;
const int maxm = 4000003;   ///此处的maxm需要开的很大,再重复加入了元素之后用uniqe去重
const int maxn = 8e4 + 2;
int pp[maxn], xx[maxn], yy[maxn], zz[maxn], re[maxm], bit[maxm];
int w, cnt, n, m;

void read(int x, int y)
{
    for (int i = x; i <= w; i += i & -i) {
        for (int j = y; j <= w; j += j & -j) {
            re[cnt++] = i * w + j;
        }
    }
}

void add(int x, int y, int t) {
    for(int i = x; i <= w; i += -i & i) {
        for(int j = y; j <= w; j += j & -j) {
            int pos = lower_bound(re, re + cnt, i * w + j) - re;
            bit[pos] += t;
        }
    }
}

int sum(int x, int y) {
    int s = 0;
    for(int i = x; i > 0; i -= i & -i) {
        for(int j = y; j > 0; j -= j & -j) {
            int pos = lower_bound(re, re + cnt, i * w + j) - re;
            if(re[pos] == i * w + j) {
                s += bit[pos];
            }
        }
    }
    return s;
}

int main()
{
    int tempx, tempy, x1, x2, y1, y2;
    while(scanf("%d", &n) != EOF && n) {
        scanf("%d", &m);
        memset(bit, 0, sizeof(bit));
        w = 2 * n;
        cnt = 0;
        for(int i = 0; i < m; ++i) {
            scanf("%d%d%d%d", &pp[i], &xx[i], &yy[i], &zz[i]);
            if(pp[i] == 1) read(xx[i] - yy[i] + n, xx[i] + yy[i]);
        }
        sort(re, re + cnt);
        cnt = unique(re, re + cnt) - re;
        for(int i = 0; i < m; ++i) {
            tempx = xx[i] - yy[i] + n;
            tempy = xx[i] + yy[i];
            if(pp[i] == 1) {
                add(tempx, tempy, zz[i]);
                //cout << sum(tempx, tempy) << "***" << endl;
            } else {
                x1 = min(tempx + zz[i], w);
                y1 = min(tempy + zz[i], w);
                x2 = max(tempx - zz[i], 1) - 1;
                y2 = max(tempy - zz[i], 1) - 1;
                printf("%d\n", sum(x1, y1) - sum(x1, y2) - sum(x2, y1) + sum(x2, y2));
            }
        }
    }
    return 0;
}





你可能感兴趣的:(线段树,ACM-线段树)