HDOJ 4456 Crowd 离散化+二维树状数组


将坐标旋转45度就可以得到正方形,可以用二维树状数组求解...

为了节省内存,提前将树状数组中会被更新的点全都存下来,并离散化


Crowd

Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1199    Accepted Submission(s): 282


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
 



/* ***********************************************
Author        :CKboss
Created Time  :2015年08月31日 星期一 10时17分01秒
File Name     :HDOJ4456.cpp
************************************************ */

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <queue>
#include <set>
#include <map>

using namespace std;

const int maxn=88888;
const int maxm=4004000;

int tree[maxm];
int hs[maxm],hn;
int n,m;
int w;
int p[maxn],x[maxn],y[maxn],d[maxn];

inline int lowbit(int x) { return x&(-x); }

void ready(int x,int y)
{
	for(int i=x;i<=w;i+=lowbit(i))
	{
		for(int j=y;j<=w;j+=lowbit(j))
		{
			hs[hn++]=i*w+j;
		}
	}
}

void Add(int x,int y,int v)
{
	for(int i=x;i<=w;i+=lowbit(i))
	{
		for(int j=y;j<=w;j+=lowbit(j))
		{
			int id=lower_bound(hs,hs+hn,i*w+j)-hs;
			tree[id]+=v;
		}
	}
}

int Sum(int x,int y)
{
	x=max(x,0); x=min(x,w);
	y=max(y,0); y=min(y,w);

	int ret=0;
	for(int i=x;i>0;i-=lowbit(i))
	{
		for(int j=y;j>0;j-=lowbit(j))
		{
			int id=lower_bound(hs,hs+hn,i*w+j)-hs;
			if(hs[id]==i*w+j) ret+=tree[id];
		}
	}
	return ret;
}

int main()
{
	//freopen("in.txt","r",stdin);
	//freopen("out.txt","w",stdout);

	while(scanf("%d",&n)!=EOF&&n)
	{
		scanf("%d",&m);
		w=2*n; hn=0;
		memset(tree,0,sizeof(tree));

		for(int i=0;i<m;i++)
		{
			scanf("%d%d%d%d",p+i,x+i,y+i,d+i);
			if(p[i]==1)
			{
				int nx=x[i]+y[i];
				int ny=y[i]-x[i]+n;
				ready(nx,ny);
			}
		}

		sort(hs,hs+hn);
		hn=unique(hs,hs+hn)-hs;

		for(int i=0;i<m;i++)
		{
			int nx=x[i]+y[i];
			int ny=y[i]-x[i]+n;
			if(p[i]==1)
			{
				Add(nx,ny,d[i]);
			}
			else if(p[i]==2)
			{
				int x1,y1,x2,y2,x3,y3,x4,y4;

				/// X=x+y  Y=y-x+n;
				x1=nx+d[i]; y1=ny+d[i];
				x2=nx-d[i]; y2=ny+d[i]; x2--;
				x3=nx+d[i]; y3=ny-d[i]; y3--;
				x4=nx-d[i]; y4=ny-d[i]; x4--; y4--;

				printf("%d\n",Sum(x1,y1)-Sum(x2,y2)-Sum(x3,y3)+Sum(x4,y4));
			}
		}
	}

    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值