HDU1166 敌兵布阵

  题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1166

  线段树裸题,注意数组大小为nlogn

  贴代码

#include<cstdio>
#include<cstring>
using namespace std;
const int N=50005;
int a[N];//addflag[N],
int addflag[N*20],tree[N*20],left[20*N],right[20*N];
int n,Q;
void build(int root,int L,int R){
	addflag[root]=0;
	left[root]=L;right[root]=R;
	if (L==R) tree[root]=a[L];
	else{
		int mid=(L+R)/2;
		build(root*2,L,mid);
		build(root*2+1,mid+1,R);
		tree[root]=tree[root*2]+tree[root*2+1];
	}
}
void pushdown(int root){
	 if (addflag[root]!=0){
	 	addflag[root*2]+=addflag[root];
	 	addflag[root*2+1]+=addflag[root];
	 	tree[root*2]+=addflag[root];
	 	tree[root*2+1]+=addflag[root];
	 	addflag[root]=0; 
	 }
}
int query(int root,int l,int r){
	int L,R;
	L=left[root];
	R=right[root];
	if ((r<L)||(l>R))return 0;
	if ((l<=L)&&(R<=r))return tree[root];
	pushdown(root);
	int mid=(L+R)/2;
	int x=query(root*2,l,r)+query(root*2+1,l,r);
	return x;
}
void add(int root,int x,int delta){	
	int L,R;
	L=left[root];
	R=right[root];
	if ((x<L)||(x>R))return;
	if ((x<=L)&&(R<=x)){
		addflag[root]+=delta;
		tree[root]+=delta;
		return;
	}
	pushdown(root);
	int mid=(L+R)/2;
	add(root*2,x,delta);
	add(root*2+1,x,delta);
	tree[root]=tree[root*2]+tree[root*2+1];
}
int main(){
//	freopen("1166.in","r",stdin);
//	freopen("1166.out","w",stdout);
	memset(tree,0,sizeof(tree));
	memset(addflag,0,sizeof(addflag));
	scanf("%d\n",&Q);
	for (int t=1;t<=Q;t++){
		scanf("%d",&n);
		for (int i=1;i<=n;i++)scanf("%d",&a[i]);
		scanf("\n");
		build(1,1,n);
		printf("Case %d:\n",t);
		while (true){
			char ch,ch1,ch2,ch3,ch4;
			int x,y;
			scanf("%c",&ch);
			if (ch=='A'){
				scanf("%c%c",&ch1,&ch2);
				scanf("%d%d\n",&x,&y);
				add(1,x,y);
			}else if (ch=='S'){
				scanf("%c%c",&ch1,&ch2);
				scanf("%d%d\n",&x,&y);
				add(1,x,-y);
			}else if (ch=='Q'){
				scanf("%c%c%c%c",&ch1,&ch2,&ch3,&ch4);
				scanf("%d%d\n",&x,&y);
				printf("%d\n",query(1,x,y));
			}else if (ch=='E'){
				scanf("%c%c\n",&ch1,&ch2);
				break;
			}
		}			
	}
	return 0;
}	
【写的有漏洞的,欢迎路过大神吐槽】

  2017/07/10 21:20:24

  Ending. 


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值