博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU 4456 Crowd
阅读量:4562 次
发布时间:2019-06-08

本文共 4768 字,大约阅读时间需要 15 分钟。

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 
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

 

转载于:https://www.cnblogs.com/crackpotisback/p/4779635.html

你可能感兴趣的文章
luogu 1314 聪明的质检员
查看>>
[转载]求职者防骗必读!楼主亲身经历告诉你岗前培训多么不靠谱而且违法!
查看>>
Hibernate内存溢出分析一例
查看>>
基于Axis1.4的webservice接口开发(接口调用)
查看>>
Hive内置函数详解
查看>>
【转】MyEclipse快捷键大全
查看>>
IT职业技能图谱10--Hadoop家族技能图谱
查看>>
Java - 反射(1)
查看>>
控制台中显示执行的Sql语句
查看>>
Linux(Centos7)下搭建SVN服务器
查看>>
安卓开发的Tasks and Back Stack
查看>>
Ansi,UTF8,Unicode编码
查看>>
原子变量的性能问题
查看>>
Sybase PowerDesigner 15.0 完美版+特别文件
查看>>
快速傅立叶之二
查看>>
cetos 6.3 安装 apache+mysql+php
查看>>
js编写简单的贪吃蛇游戏
查看>>
2018/12/01 一个64位操作系统的实现 第四章 导入kernel.bin(4)
查看>>
如何在windows xp professional安装iis的解决方法
查看>>
抽象类和接口
查看>>