HDU4456(Crowd) 二维树状组+坐标旋转
来源:互联网 发布:刺客列传网络剧 编辑:程序博客网 时间:2024/05/24 01:23
Crowd
Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2663 Accepted Submission(s): 640
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.
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.
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 51 8 8 11 1 1 -22 5 5 61 5 5 32 2 3 93 21 3 2 -92 3 2 00
Sample Output
11-9
Source
2012 Asia Hangzhou Regional Contest
大意就是给定一个矩阵,有两种操作,一种是更改某个点的值,一种是查询该点曼哈顿距离内的点值。
曼哈顿距离表示的是一个菱形,所以每个点旋转45°。
新点的坐标表示为整数的话就是:(x-y,x+y)
为了防止x-y<0,将横坐标加n即可,所以处理点就是(x-y+n,x-y)
再加上这个题数据量大,用离散化处理一下,将二维的坐标映射成为一个数字,用树状数组维护更新和查询即可。
代码如下:
#include <stdio.h>#include <iostream>#include <stdlib.h>#include <algorithm>#include <string.h>#include <math.h>#define N 3001003using namespace std;int x[N],y[N];int sym[N],val[N];int num[N],tree[N];int cnt;int n,m;int H;int lowbit(int i){ return i&(-i);}void get(int x,int y){ for(int i=x;i<=H;i+=lowbit(i)) { for(int j=y;j<=H;j+=lowbit(j)) { num[cnt++]=i*(H+1)+j; //这里将二维的坐标映射成为一个数字,i乘的数字最小为H,当小于H时加j有可能使得两个坐标投影为同一个数字 } }}void add(int x,int y,int val){ for(int i=x;i<=H;i+=lowbit(i)) { for(int j=y;j<=H;j+=lowbit(j)) { int pos=lower_bound(num,num+cnt,i*(H+1)+j)-num; tree[pos]+=val; } }}int query(int x,int y){ int sum=0; for(int i=x;i>0;i-=lowbit(i)) { for(int j=y;j>0;j-=lowbit(j)) { int pos=lower_bound(num,num+cnt,i*(H+1)+j)-num; if(num[pos]==i*(H+1)+j) sum+=tree[pos]; } } return sum;}int main(){ int newx,newy; while(scanf("%d",&n)!=EOF&&n) { scanf("%d",&m); H=n*2;//坐标扩大为与来的2倍 cnt=0; memset(tree,0,sizeof(tree)); memset(num,0,sizeof(num)); for(int i=0;i<m;i++) { scanf("%d%d%d%d",&sym[i],&x[i],&y[i],&val[i]); newx=x[i]-y[i]+n; newy=x[i]+y[i]; if(sym[i]==1) get(newx,newy); } //printf("%d\n",cnt); sort(num,num+cnt); cnt=unique(num,num+cnt)-num; //unique将重复的元素去掉,加在数组的最后 for(int i=0;i<m;i++) { newx=x[i]-y[i]+n; newy=x[i]+y[i]; if(sym[i]==1) add(newx,newy,val[i]); else { int x1=max(1,newx-val[i]);//防止越界 int x2=min(H,newx+val[i]); int y1=max(1,newy-val[i]); int y2=min(H,newy+val[i]); printf("%d\n",query(x2,y2)-query(x1-1,y2)-query(x2,y1-1)+query(x1-1,y1-1)); } } } return 0;}
阅读全文
0 0
- HDU4456(Crowd) 二维树状组+坐标旋转
- hdu4456 Crowd(二维树状数组)
- HDU4456————Crowd(离散化,坐标转化,二维树状数组)
- HDU 4456 Crowd 坐标旋转 二维树状数组
- HDU4456-Crowd
- hdu 4456 Crowd(二维树状数组)
- HDU 4456 Crowd (二维树状数组 + HASH)
- HDU 4456 Crowd (坐标变换+树状数组+离散化)
- HDOJ 4456 Crowd 离散化+二维树状数组
- 【VC / MFC】二维坐标旋转
- HDU4456 坐标转换+线段树+离散化
- OpenCv: 二维坐标的旋转方程
- 坐标轴旋转+二维树状数组离散化
- hdu2642 二维树状组
- POJ4449(三维凸包+空间坐标旋转+二维凸包)
- 坐标旋转
- 计算机图形学 学习笔记(七):二维图形变换:平移,比例,旋转,坐标变换等
- PV3D获得二维坐标
- backbone+requirejs+bootstrap登录注册(maven项目,后台springmvc)
- 反射机制在学习时候的总结
- 神经网络网络参数的取值问题
- zepto框架学习日记
- poj 2540 求半平面交
- HDU4456(Crowd) 二维树状组+坐标旋转
- 数据结构——队列
- 封装继承多态概念复习
- 【51 nod 1090】 3个数和为0
- 朴素贝叶斯实现的文本分类
- html日期选择框
- 51Job 工作爬虫
- 前端页面性能优化
- hdu1005找规律