HDU 4456 Crowd

Crowd

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

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.

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

解题:隔壁老王对我说,这个曼哈顿距离啊覆盖的区域旋转45度后就是一个矩形区域,既然是矩形区域,范围还那么大,其实可以更大的,只是内存真抠门。。。

上吧,CDQ

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 typedef long long LL;
 4 const int maxn = 200010;
 5 struct QU {
 6     int x1,x2,y,id,f;
 7     QU(int a = 0,int b = 0,int c = 0,int d = 0,int e = 0) {
 8         x1 = a;
 9         x2 = b;
10         y = c;
11         id = d;
12         f = e;
13     }
14     bool operator<(const QU &t)const {
15         return y < t.y;
16     }
17 } Q[maxn],A[maxn],B[maxn];
18 LL C[maxn],ans[maxn];
19 void add(int i,int val) {
20     while(i < maxn) {
21         C[i] += val;
22         i += i&-i;
23     }
24 }
25 LL sum(int i,LL ret = 0) {
26     while(i > 0) {
27         ret += C[i];
28         i -= i&-i;
29     }
30     return ret;
31 }
32 void cdq(int L,int R) {
33     if(R <= L) return;
34     int mid = (L + R)>>1;
35     cdq(L,mid);
36     cdq(mid+1,R);
37     int a = 0,b = 0,j = 0;
38     for(int i = L; i <= mid; ++i)
39         if(Q[i].id == -1) A[a++] = Q[i];
40     for(int i = mid + 1; i <= R; ++i)
41         if(Q[i].id != -1) B[b++] = Q[i];
42     sort(A,A + a);
43     sort(B,B + b);
44     for(int i = 0; i < b; ++i) {
45         for(; j < a && A[j].y <= B[i].y; ++j) add(A[j].x1,A[j].f);
46         ans[B[i].id] += B[i].f*sum(B[i].x2);
47         ans[B[i].id] -= B[i].f*sum(B[i].x1);
48     }
49     for(int i = 0; i < j; ++i) add(A[i].x1,-A[i].f);
50 }
51 int main() {
52     int n,m,op,x,y,z,tot,ask;
53     while(scanf("%d",&n),n) {
54         scanf("%d",&m);
55         ask = tot = 0;
56         memset(ans,0,sizeof ans);
57         while(m--) {
58             scanf("%d%d%d%d",&op,&x,&y,&z);
59             if(op == 1) Q[tot++] = QU(x + y,0,y - x,-1,z);
60             else {
61                 int cx = x + y;
62                 int cy = y - x;
63                 int x1 = cx - z;
64                 int x2 = cx + z;
65                 int y1 = cy - z;
66                 int y2 = cy + z;
67                 Q[tot++] = QU(x1-1,x2,y2,ask,1);
68                 Q[tot++] = QU(x1-1,x2,y1-1,ask++,-1);
69             }
70         }
71         cdq(0,tot-1);
72         for(int i = 0; i < ask; ++i)
73             printf("%I64d\n",ans[i]);
74     }
75     return 0;
76 }

时间: 2024-10-25 19:09:35

HDU 4456 Crowd的相关文章

hdu 4456 Crowd(二维树状数组)

题目链接:hdu 4456 Crowd 题目大意:给定N,然后M次操作 1 x y z:在x,y的位置加z 2 x y z:询问与x,y曼哈顿距离小于z的点值和. 解题思路:将矩阵旋转45度,然后询问就等于是询问一个矩形,可以用容斥定理搞,维护用二维树状数组,但是空间开 不下,直接用离散化,将有用到的点处理出来. #include <cstdio> #include <cstring> #include <algorithm> using namespace std;

HDOJ 4456 Crowd 离散化+二维树状数组

将坐标旋转45度就可以得到正方形,可以用二维树状数组求解... 为了节省内存,提前将树状数组中会被更新的点全都存下来,并离散化 Crowd Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1199    Accepted Submission(s): 282 Problem Description City F in the south

HDU 4456(二维树状数组+坐标转换)

题目链接:Problem - 4456 看别人叙述看的心烦,于是我自己画了一张图. 上图. 上代码 1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 #include <cstring> 5 #include <cmath> 6 using namespace std; 7 const int maxn = 81111; 8 const int maxm = 3e

hdu 4815 Little Tiger vs. Deep Monkey(01背包)

http://acm.hdu.edu.cn/showproblem.php?pid=4815 Description A crowd of little animals is visiting a mysterious laboratory ? The Deep Lab of SYSU. “Are you surprised by the STS (speech to speech) technology of Microsoft Research and the cat face recogn

HDU 4815 2013长春现场赛C题

C - Little Tiger vs. Deep Monkey Time Limit:1000MS     Memory Limit:65535KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 4815 Description A crowd of little animals is visiting a mysterious laboratory ? The Deep Lab of SYSU. "Are y

HDU 6203 ping ping ping [LCA,贪心,DFS序,BIT(树状数组)]

题目链接:[http://acm.hdu.edu.cn/showproblem.php?pid=6203] 题意 :给出一棵树,如果(a,b)路径上有坏点,那么(a,b)之间不联通,给出一些不联通的点对,然后判断最少有多少个坏点. 题解 :求每个点对的LCA,然后根据LCA的深度排序.从LCA最深的点对开始,如果a或者b点已经有点被标记了,那么continue,否者标记(a,b)LCA的子树每个顶点加1. #include<Bits/stdc++.h> using namespace std;

HDU 5542 The Battle of Chibi dp+树状数组

题目:http://acm.hdu.edu.cn/showproblem.php?pid=5542 题意:给你n个数,求其中上升子序列长度为m的个数 可以考虑用dp[i][j]表示以a[i]结尾的长度为j的上升子序列有多少 裸的dp是o(n2m) 所以需要优化 我们可以发现dp的第3维是找比它小的数,那么就可以用树状数组来找 这样就可以降低复杂度 #include<iostream> #include<cstdio> #include<cstring> #include

hdu 1207 汉诺塔II (DP+递推)

汉诺塔II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4529    Accepted Submission(s): 2231 Problem Description 经典的汉诺塔问题经常作为一个递归的经典例题存在.可能有人并不知道汉诺塔问题的典故.汉诺塔来源于印度传说的一个故事,上帝创造世界时作了三根金刚石柱子,在一根柱子上从下往

[hdu 2102]bfs+注意INF

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2102 感觉这个题非常水,结果一直WA,最后发现居然是0x3f3f3f3f不够大导致的--把INF改成INF+INF就过了. #include<bits/stdc++.h> using namespace std; bool vis[2][15][15]; char s[2][15][15]; const int INF=0x3f3f3f3f; const int fx[]={0,0,1,-1};