BZOJ 2683 简单题

题面
你有一个N*N的棋盘,每个格子内有一个整数,初始时的时候全部为0,现在需要维护两种操作:

 

命令

参数限制

内容

1 x y A

1<=x,y<=NA是正整数

将格子x,y里的数字加上A

2 x1 y1 x2 y2

1<=x1<= x2<=N

1<=y1<= y2<=N

输出x1 y1 x2 y2这个矩形内的数字和

3

终止程序


这题是 bzoj 4066(kdtree)的弱化版,没有强制在线,所以离线乱搞
二维cdq分治(学习cdq分治后的第一题)
 //By Richard
#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <ctime>
#define rep(x,y,z) for (int x=(y);(x)<=(z);(x)++)
#define per(x,y,z) for (int x=(y);(x)>=(z);(x)--)
#define log2(x) (31-__builtin_clz(x))
#define XX 100001
#define mod 1e9+7
#define inf 0x3f3f3f3f
#define cls(x) memset(x,0,sizeof(x))
#ifdef DEBUG
#define debugdo(X) X
#define debugndo(X)
#define debugout(X) cout<<(#X)<<"="<<(X)<<endl
#else
#define debugdo(X)
#define debugndo(X) X
#define debugout(X)
#endif // debug
#define putarray(x,n) rep(iiii,1,n) printf("%d ",x[iiii])
#define mp make_pair
using namespace std;
typedef pair<int,int> pairs;
typedef long long LL;
/read3.0
template <typename T>
inline void read(T &x){char ch;x=0;bool flag=false;ch=getchar();while (ch>'9'||ch<'0') {ch=getchar();if (ch=='-') flag=true;}while ((ch<='9'&&ch>='0')){x=x*10+ch-'0';ch=getchar();}if (flag) x*=-1;}
template <typename T>
inline void read(T &x,T &y){read(x);read(y);}
/variables&functions
#define lowbit(x) (x&(-x))

int n,tot,T,c[800001],ans[800001],opt;
struct AAA
{
	int x,y,id,a,belong;
	bool operator<(const AAA &other)const
	{
		return x==other.x?(y==other.y?id<other.id:y<other.y):x<other.x;
	}
}q[800001],temp[800001];
void change(int pos,int x)
{
	while (pos<=n)
	{
		c[pos]+=x;
		pos+=lowbit(pos);
	}
}
int query(int x)
{
	int ret=0;
	while (x)
	{
		ret+=c[x];
		x-=lowbit(x);
	}
	return ret;
}
void solve(int l,int r)
{
	if (l==r) return;
	int mid=((l+r)>>1);
	rep(i,l,r)
	{
		if ((!q[i].belong)&&q[i].id<=mid) change(q[i].y,q[i].a);
		else if ((q[i].belong)&&q[i].id>mid) ans[q[i].belong]+=query(q[i].y)*q[i].a;
	}
	rep(i,l,r) if ((!q[i].belong)&&q[i].id<=mid) change(q[i].y,-q[i].a);
	int l1=l,l2=mid+1;
	rep(i,l,r) 
	{
		if (q[i].id<=mid) temp[l1++]=q[i];
		else temp[l2++]=q[i];
	}
	rep(i,l,r) q[i]=temp[i];
	solve(l,mid);
	solve(mid+1,r);
}
int main()
{
	read(n);
	while (scanf("%d",&opt)!=EOF&&opt^3)
	{
		if (opt==1)
		{
			++tot;
			q[tot].id=tot;
			read(q[tot].x,q[tot].y);
			read(q[tot].a);
		}
		else 
		{
			int x,y,xx,yy;
			read(x,y);
			read(xx,yy);
			q[++tot].belong=++T,q[tot].id=tot,q[tot].x=x-1,q[tot].y=y-1,q[tot].a=1;
			q[++tot].belong=T,q[tot].id=tot,q[tot].x=x-1,q[tot].y=yy,q[tot].a=-1;
			q[++tot].belong=T,q[tot].id=tot,q[tot].x=xx,q[tot].y=y-1,q[tot].a=-1;
			q[++tot].belong=T,q[tot].id=tot,q[tot].x=xx,q[tot].y=yy,q[tot].a=1;
		}
	}
	sort(q+1,q+tot+1);
	solve(1,tot);
	rep(i,1,T) printf("%d\n",ans[i]);
	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值