Magnificent Tree 牛客競賽 三維偏序問題 CDQ分治+樹狀數組

鏈接:https://ac.nowcoder.com/acm/contest/106/H?&headNav=www
來源:牛客網
 

It’s universally acknowledged that there’re innumerable trees in the campus of HUST.

 

There is a new leisure ground at the gate of HUST. The president of HUST wants to plant a group of trees on the ground and he wants the trees to be planted exactly like a square matrix. And then the gardens start their work. In the next N days, the gardens can water a tree in coordinate: (x, y) and it makes the height of the tree grows H. Sometimes, the president wants to know how magnificent his trees are. The president will give the gardens two coordinates: (x1, y1), (x2, y2). The gardens should report the sum of the heights of the trees in the sub matrix represents by the two coordinates. There will be A days that the gardens will water a tree. And there will be Q days that the president will ask how magnificent his trees are. Apparently, N=A +Q. Furthermore, all the trees’ heights are 0 in the very beginning.

 

All the absolute values of the inputs and the results are all nonnegative and won't exceed 231-1.

輸入描述:


 

The first line consists of two integers N and W(0≤A≤105,0≤Q≤105,1≤W≤105)(0 \leq A \leq 10^{5},0 \leq Q \leq 10^{5}, 1\leq W\leq 10^{5})(0≤A≤105,0≤Q≤105,1≤W≤105). N represents the number of days and W represents the size of the matrix. Followed by N lines in two forms:

1 x y H

2 x1 y1 x2 y2

The first one represents the gardeners will water the tree in coordinate (x, y) and it will grow H(1≤x,y≤W)(1\leq x,y\leq W)(1≤x,y≤W).

The second one represents that you need to output the sum of the heights of the trees in the sub matrix represents by the two coordinates.

輸出描述:

Your output should consist of Q lines. Each line contains an integer, the result of a query.

示例1

輸入

4 5
1 1 3 2
2 1 1 2 2
1 2 2 3
2 1 1 3 3

輸出

0
5

題意:給你一個矩陣,n個操作:

1 x y h  將(x,y)中的數字加上h

2 x1 y1 x2 y2   求(x1,y1)到(x2,y2)這個子矩陣內所有數字的和。

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=1e5+100;
const int maxm=5e5+100;
int cnt,n,w,tree[maxn],ans[maxn];
int lowbit(int x)
{
	return x&-x;
}
void update(int tar,int val)
{
	for(int i=tar;i<=w;i+=lowbit(i))
		tree[i]+=val;
}
int query(int tar)
{
	int res=0;
	for(int i=tar;i;i-=lowbit(i)) res+=tree[i];
	return res;
}
struct node{
	int x;
	int y;
	int h;
	int flag;
	bool operator < (const node &t)const
	{
		if(x<t.x) return 1; 
		if(x>t.x) return 0;
		if(y<t.y) return 1;
		if(y>t.y) return 0;
		return flag<t.flag;
	}
}a[maxm],tmp[maxm];
void CDQ(int l,int r)
{
	if(l==r) return ;
	int mid=(l+r)>>1,p=l,q=mid+1,cont=l-1;
	CDQ(l,mid);CDQ(mid+1,r);
	while(p<=mid&&q<=r)
	{
		if(a[p]<a[q])
		{
			
			if(a[p].flag==1) update(a[p].y,a[p].h);
			tmp[++cont]=a[p++];
		}
		else
		{
			if(a[q].flag==2) ans[a[q].h]+=query(a[q].y);
			if(a[q].flag==3) ans[a[q].h]-=query(a[q].y);
			tmp[++cont]=a[q++];
		}
	}
	while(q<=r)
	{
		if(a[q].flag==2) ans[a[q].h]+=query(a[q].y);
		if(a[q].flag==3) ans[a[q].h]-=query(a[q].y);
		tmp[++cont]=a[q++];
	}
	for(int i=l;i<p;i++) if(a[i].flag==1) update(a[i].y,-a[i].h);
	while(p<=mid) tmp[++cont]=a[p++];
	for(int i=l;i<=r;i++) a[i]=tmp[i];
}
int main()
{
	int op,x1,x2,y1,y2,ct;
	cnt=ct=0;
	scanf("%d%d",&n,&w);
	while(n--)
	{
		scanf("%d",&op);
		if(op==1)
		{
			scanf("%d%d%d",&x1,&y1,&y2);
			a[++cnt].x=x1;a[cnt].y=y1;a[cnt].h=y2;
			a[cnt].flag=1;
		}
		else
		{
			scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
			a[++cnt].x=x2;a[cnt].y=y2;a[cnt].h=++ct;a[cnt].flag=2;
			a[++cnt].x=x1-1;a[cnt].y=y1-1;a[cnt].h=ct;a[cnt].flag=2;
			a[++cnt].x=x1-1;a[cnt].y=y2;a[cnt].h=ct;a[cnt].flag=3;
			a[++cnt].x=x2;a[cnt].y=y1-1;a[cnt].h=ct;a[cnt].flag=3;
		}
	}
	CDQ(1,cnt);
	for(int i=1;i<=ct;i++) printf("%d\n",ans[i]);
	return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章