HDU 1166 敌兵布阵(树状数组)

请不要随便指点别人该怎么做、每个人的人生都应该自己掌握、你给不了别人一切、你也不懂别人的忧伤、

                                                                                          微笑不代表快乐、哭泣不一定悲伤

               不努力怎么让关心你的人幸福、不努力怎么让看不起你的人绝望、

                                                                                                                                    

                                                                                                                                                              我用生命在奋斗——lx_Zz

—————————————————————————————————————————————————————————————

—————————————————————————    华丽的分割线    ————————————————————————————

—————————————————————————————————————————————————————————————

10732768 2014-05-13 13:39:16 Accepted 1166 156MS 632K 1480 B C++ 给我再去相....

上课前切一道树状数组

7分钟树状数组搞定、上课去了。。。O(∩_∩)O哈哈~

/* 题目: HDU 1166    */
/* 作者: lx_Zz       */
/* 时间: 2014.5.13   */
#include<cstdio>
#include<queue>
#include<vector>
#include<map>
#include<string>
#include<iostream>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std;
#define INF 0x7fffffff
#define LL __int64

int n;
const int maxn=50005;
int num[maxn];
int c[maxn];

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

void add(int x,int d)
{
	while(x<=n)
	{
		c[x]+=d;
		x+=lowbit(x);
	}
}

int sum(int x)
{
	int ret=0;
	while(x>0)
	{
		ret+=c[x];
		x-=lowbit(x);
	}
	return ret;
}

int main()
{
    //freopen("C:\\Users\\终将我要华丽的逆袭\\Desktop\\lx_Zz_in.txt","r",stdin);
    //freopen("C:\\Users\\终将我要华丽的逆袭\\Desktop\\lx_Zz_out.txt","w",stdout);

	int T;
	int cas=1;
	scanf("%d",&T);
	while(T--)
	{
		printf("Case %d:\n",cas++);
		memset(c,0,sizeof(c));
		scanf("%d",&n);
		for(int i=1;i<=n;i++)
		{
			scanf("%d",&num[i]);
			add(i,num[i]);
		}
		while(true)
		{
			char str[15];
			scanf("%s",str);
			if(strcmp(str,"End")==0)break;
			else if(strcmp(str,"Add")==0)
			{
				int a,b;
				scanf("%d%d",&a,&b);
				add(a,b);
			}
			else if(strcmp(str,"Sub")==0)
			{
				int a,b;
				scanf("%d%d",&a,&b);
				add(a,-b);
			}
			else if(strcmp(str,"Query")==0)
			{
				int a,b;
				scanf("%d%d",&a,&b);
				int ans=sum(b)-sum(a-1);
				printf("%d\n",ans);
			}
		}
	}
    return 0;
}


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