COGS 577 蝗灾

DESCRIPTION
C国国土辽阔,地大物博......但是最近却在闹蝗灾.....
我们可以把C国国土当成一个W×W的矩阵,你会收到一些诸如(X,Y,Z)的信息,代表(X,Y)这个点增多了
Z只蝗虫,而由于C国政府机关比较臃肿,为了批复消灭蝗虫的请求需要询问一大堆的问题......每个询问形如
(X1,Y1,X2,Y2),询问在(X1,Y1,X2,Y2)范围内有多少蝗虫(请注意询问不会改变区域内的蝗虫数),
你作为一个C国的程序员,需要编一个程序快速的回答所有的询问。

NOTICE
C国一开始没有蝗虫。

INPUT
输入文件的第一行包括一个整数W,代表C国国土的大小。第二行有一个整数N,表示事件数。接下来有N行表示N个事件,以(1 X Y Z)的形式或(2,X1,Y1,X2,Y2)的形式给出,分别代表蝗虫的增加和询问。

OUTPUT
对于每个询问输出一个整数表示需要的结果。

SAMPLE INPUT
locust.in
5
8
2 4 1 4 2
1 3 1 8
1 4 4 4
2 1 3 4 4
1 1 5 1
1 4 4 5
2 2 2 5 4
2 3 2 4 4

SAMPLE OUTPUT
locust.out
0
4
9
9

数据范围:
10%的数据满足W<=100,N<=100;
30%的数据满足W<=2000,N<=5000;
50%的数据满足W<=100000,N<=50000;
100%的数据满足W<=500000,N<=200000,每次蝗虫增加数不超过1000;

时间限制:
2s

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

CDQ分治+树状数组~

x坐标排序,y坐标树状数组,时间CDQ分治。


#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;

int n,m,k,x,y,x1,y1,x2,y2,val,c[500001],cnt,tot,ans[200001];

struct node{
	bool flag;
	int x,y,val,pos,id;
}a[800001],b[800001];

int read()
{
	int x=0,f=1;char ch=getchar();
	while(ch<'0' || ch>'9') {if(ch=='-') f=-1;ch=getchar();}
	while(ch>='0' && ch<='9') {x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}
	return x*f;
}

bool operator < (node u,node v)
{
	return u.x==v.x ? u.pos<v.pos:u.x<v.x;
}

void add(int u,int v)
{
	for(;u<=n;u+=u&(-u)) c[u]+=v;
}

int cal(int u)
{
	int now=0;
	for(;u;u-=u&(-u)) now+=c[u];
	return now;
}

void sol(int l,int r)
{
	if(l>=r) return;
	int mid=l+r>>1,l1=l,l2=mid+1;
	for(int i=l;i<=r;i++)
	  if(!a[i].flag && a[i].pos<=mid) add(a[i].y,a[i].val);
	  else if(a[i].flag && a[i].pos>mid) ans[a[i].id]+=cal(a[i].y)*a[i].val;
	for(int i=l;i<=r;i++) if(!a[i].flag && a[i].pos<=mid) add(a[i].y,-a[i].val);
	for(int i=l;i<=r;i++)
	  if(a[i].pos<=mid) b[l1++]=a[i];
	  else b[l2++]=a[i];
	for(int i=l;i<=r;i++) a[i]=b[i];
	sol(l,mid);sol(mid+1,r);
}

int main()
{
	freopen("locust.in","r",stdin);
	freopen("locust.out","w",stdout);
	n=read();m=read();
	for(int i=1;i<=m;i++)
	{
		k=read();
		if(k==1) x=read(),y=read(),val=read(),a[++tot]=(node){0,x,y,val,tot,0};
		else
		{
			x1=read()-1;y1=read()-1;x2=read();y2=read();cnt++;
			a[++tot]=(node){1,x1,y1,1,tot,cnt};
			a[++tot]=(node){1,x2,y1,-1,tot,cnt};
			a[++tot]=(node){1,x1,y2,-1,tot,cnt};
			a[++tot]=(node){1,x2,y2,1,tot,cnt};
		}
	}
	sort(a+1,a+tot+1);
	sol(1,tot);
	for(int i=1;i<=cnt;i++) printf("%d\n",ans[i]);
	return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值