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 = ;
  5. struct QU {
  6. int x1,x2,y,id,f;
  7. QU(int a = ,int b = ,int c = ,int d = ,int e = ) {
  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 = ) {
  26. while(i > ) {
  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)>>;
  35. cdq(L,mid);
  36. cdq(mid+,R);
  37. int a = ,b = ,j = ;
  38. for(int i = L; i <= mid; ++i)
  39. if(Q[i].id == -) A[a++] = Q[i];
  40. for(int i = mid + ; i <= R; ++i)
  41. if(Q[i].id != -) B[b++] = Q[i];
  42. sort(A,A + a);
  43. sort(B,B + b);
  44. for(int i = ; 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 = ; 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 = ;
  56. memset(ans,,sizeof ans);
  57. while(m--) {
  58. scanf("%d%d%d%d",&op,&x,&y,&z);
  59. if(op == ) Q[tot++] = QU(x + y,,y - x,-,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-,x2,y2,ask,);
  68. Q[tot++] = QU(x1-,x2,y1-,ask++,-);
  69. }
  70. }
  71. cdq(,tot-);
  72. for(int i = ; i < ask; ++i)
  73. printf("%I64d\n",ans[i]);
  74. }
  75. return ;
  76. }

HDU 4456 Crowd的更多相关文章

  1. cdq分治(hdu 5618 Jam's problem again[陌上花开]、CQOI 2011 动态逆序对、hdu 4742 Pinball Game、hdu 4456 Crowd、[HEOI2016/TJOI2016]序列、[NOI2007]货币兑换 )

    hdu 5618 Jam's problem again #include <bits/stdc++.h> #define MAXN 100010 using namespace std; ...

  2. 【 HDU - 4456 】Crowd (二维树状数组、cdq分治)

    BUPT2017 wintertraining(15) #5A HDU 4456 题意 给你一个n行n列的格子,一开始每个格子值都是0.有M个操作,p=1为第一种操作,给格子(x,y)增加z.p=2为 ...

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

    题目链接:Problem - 4456 看别人叙述看的心烦,于是我自己画了一张图. 上图. 上代码 #include <iostream> #include <cstdio> ...

  4. HDU - 4456 cdq

    题意:给一个矩阵,两种操作1:修改单点的权值,2:查询和某个点曼哈顿距离小于r点的权值和 题解:先旋转坐标轴,(x,y)->(x-y,x+y)然后就变成了cdq分治裸题,子矩阵和和单点修改一维时 ...

  5. 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 m ...

  6. HDOJ 2111. Saving HDU 贪心 结构体排序

    Saving HDU Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  7. 【HDU 3037】Saving Beans Lucas定理模板

    http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...

  8. hdu 4859 海岸线 Bestcoder Round 1

    http://acm.hdu.edu.cn/showproblem.php?pid=4859 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格 ...

  9. HDU 4569 Special equations(取模)

    Special equations Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u S ...

随机推荐

  1. 禁止root用户直接远程telnet/ssh登陆

    AIX 封闭root,只能使用su登录root用户,禁止root用户直接远程登陆. 1.  禁止telnet登录 smit chuser   ->root       ->User can ...

  2. matlab中s函数编写心得-转自水木

    S函数是system Function的简称,用它来写自己的simulink模块.(够简单吧,^_^, 详细的概念介绍大伙看帮助吧)可以用matlab.C.C++.Fortran.Ada等语言来写, ...

  3. Spring:延迟初始化

    ApplicationContext实现的默认行为就是在启动时将所有singleton bean提前进行实例化.提前实例化意味着作为初始化过程的一部分,ApplicationContext实例会创建并 ...

  4. bzoj4078

    二分+2-sat 枚举第一个权值,二分第二个权值,然后2-sat检查,当第一个权值已经不能形成二分图时,再往下没意义,因为没法分成两个点集.(双指针好像跑得慢) #include<bits/st ...

  5. Django day17 博客项目(一)

    一: 博客项目需求分析 首页(显示文章) 文章详情 点赞, 点踩 文章评论 字评论 评论的展示 登录功能(图片验证码) 注册功能(基于form验证,ajax) 个人站点(不同人不同样式,文章过滤) 后 ...

  6. 【STM32H7教程】第23章 STM32H7的MPU内存保护单元(重要)

    完整教程下载地址:http://forum.armfly.com/forum.php?mod=viewthread&tid=86980 第23章       STM32H7的MPU内存保护单元 ...

  7. spring定时任务Cron时间设定

    直接举例: 0 10 0 * *?分别对应的是 秒 分 时 日 月 周 年 秒(0–59)分钟(0–59)小时(0–23)日(1–31)月(1–12或JAN–DEC)周(1–7或SUN–SAT)年(1 ...

  8. PL/SQL实现JAVA中的split()方法的小例子

    众所周知,java中为String类提供了split()字符串分割的方法,所以很容易将字符串以指定的符号分割为一个字符串数组.但是在pl/sql中并没有提供像java中的split()方法,所以要想在 ...

  9. Android 应用安装成功之后删除apk文件

    问题: 在应用开发中遇到需要这样的需求:在用户下载我们的应用安装之后删除安装包. 解决: android会在每个外界操作APK的动作之后发出系统级别的广播,过滤器名称: android.intent. ...

  10. Android 清空缓存

    APP开发中常有计算缓存大小和清空缓存的功能,此功能很常见,几乎每个应用都能看到,下面就用代码来实现此功能: 步骤为: 1.获取缓存路径 获取长时间保存的文件,Context.getExternalF ...