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
 
 #include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn = ;
struct QU {
int x1,x2,y,id,f;
QU(int a = ,int b = ,int c = ,int d = ,int e = ) {
x1 = a;
x2 = b;
y = c;
id = d;
f = e;
}
bool operator<(const QU &t)const {
return y < t.y;
}
} Q[maxn],A[maxn],B[maxn];
LL C[maxn],ans[maxn];
void add(int i,int val) {
while(i < maxn) {
C[i] += val;
i += i&-i;
}
}
LL sum(int i,LL ret = ) {
while(i > ) {
ret += C[i];
i -= i&-i;
}
return ret;
}
void cdq(int L,int R) {
if(R <= L) return;
int mid = (L + R)>>;
cdq(L,mid);
cdq(mid+,R);
int a = ,b = ,j = ;
for(int i = L; i <= mid; ++i)
if(Q[i].id == -) A[a++] = Q[i];
for(int i = mid + ; i <= R; ++i)
if(Q[i].id != -) B[b++] = Q[i];
sort(A,A + a);
sort(B,B + b);
for(int i = ; i < b; ++i) {
for(; j < a && A[j].y <= B[i].y; ++j) add(A[j].x1,A[j].f);
ans[B[i].id] += B[i].f*sum(B[i].x2);
ans[B[i].id] -= B[i].f*sum(B[i].x1);
}
for(int i = ; i < j; ++i) add(A[i].x1,-A[i].f);
}
int main() {
int n,m,op,x,y,z,tot,ask;
while(scanf("%d",&n),n) {
scanf("%d",&m);
ask = tot = ;
memset(ans,,sizeof ans);
while(m--) {
scanf("%d%d%d%d",&op,&x,&y,&z);
if(op == ) Q[tot++] = QU(x + y,,y - x,-,z);
else {
int cx = x + y;
int cy = y - x;
int x1 = cx - z;
int x2 = cx + z;
int y1 = cy - z;
int y2 = cy + z;
Q[tot++] = QU(x1-,x2,y2,ask,);
Q[tot++] = QU(x1-,x2,y1-,ask++,-);
}
}
cdq(,tot-);
for(int i = ; i < ask; ++i)
printf("%I64d\n",ans[i]);
}
return ;
}

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. luogu1063 能量项链

    题目大意 有一串项链,项链上的每个珠子有首尾两个数字,首尾相连的两个珠子的尾数字和头数字相同.每次选择相连的一对珠子,得到第一个项链的首数字*第一个项链的尾数字(第二个项链的首数字)*第二个项链的尾数 ...

  2. Coursera Algorithms week3 归并排序 练习测验: Counting inversions

    题目原文: An inversion in an array a[] is a pair of entries a[i] and a[j] such that i<j but a[i]>a ...

  3. nodejs安装express

    最近在看<Node.js开发指南>,看到使用nodejs进行web开发的时候,准备创建ejs项目遇到问题了, 书上命令为: 1 express -t ejs microblog 可是执行后 ...

  4. json用法

    什么是JSON? JavaScript 对象表示法(JavaScript Object Notation). JSON是一种轻量级的数据交换格式,某个JSON格式的文件内部譬如可以长成这样: 1 2 ...

  5. [Apple开发者帐户帮助]六、配置应用服务(3)创建地图标识符和私钥

    要与MapKit JS通信,您将使用Maps私钥对一个或多个开发人员令牌进行签名. 首先注册地图标识符以识别您的应用.为使用MapKit JS的每个应用注册地图标识符.接下来创建并下载启用了MapKi ...

  6. 显示程序输出并复制到文件(tee 命令)

    Linux tee命令用于读取标准输入的数据,并将其内容输出成文件. tee指令会从标准输入设备读取数据,将其内容输出到标准输出设备,同时保存成文件. 语法 tee [-ai][--help][--v ...

  7. C#方法的练习

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Demo ...

  8. NoSQL概念

    NoSQL是非关系型数据库,即not only sql,key/value键值对存储. 现有Nosql DB产品:Redis/MongoDB/Memcached等等. SQL Sever是关系型数据库 ...

  9. 时序分析:串匹配—Brute-Force算法

    在使用KMP算法之前,使用了BF算法用于串匹配:原文链接已无法查找.....        设有主串s和子串t,子串t的定位就是要在主串s中找到一个与子串t相等的子串.通常把主串s称为目标串,把子串t ...

  10. windows 下安装mysql 成功版

    mysql 下载地址 http://dev.mysql.com/downloads/ zip版下载 解压到本地 假设文件保存在C:\mysql-5.7.17-winx64 1.以管理员身份运行cmd. ...