码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 4456 Crowd

时间:2015-09-02 23:26:06      阅读:467      评论:0      收藏:0      [点我收藏+]

标签:

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
 
解题:隔壁老王对我说,这个曼哈顿距离啊覆盖的区域旋转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 }
View Code

 

HDU 4456 Crowd

标签:

原文地址:http://www.cnblogs.com/crackpotisback/p/4779635.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!