HDOJ 1166 적병 포진(라인 트리)

1954 단어
제목 링크: (\~)(\~\~)~
에이~ 나무수조가 답답하지는 않을 거야......╮(╯Д╰)╭
code:
#include <stdio.h>
typedef struct
{
	int l, r, count;
}node;
node tree[4*50005];
void Init(int root, int left, int right)
{
	int m = (left+right)/2;
	if(left == right)//        
	{
		tree[root].l = tree[root].r = left;
		scanf("%d",&tree[root].count);
		return ;
	}
	tree[root].l = left; tree[root].r = right;
	Init(2*root, left, m);
	Init(2*root+1, m+1, right);
	tree[root].count = tree[2*root].count+tree[2*root+1].count;
}
void modify(int root, int a, int b)//a      , b      
{
	int m = (tree[root].l+tree[root].r)/2;
	if(tree[root].l == tree[root].r && tree[root].l == a)//     
	{
		tree[root].count += b;
		return ;
	}
	if(a>m)
		modify(2*root+1, a, b);
	else
		modify(2*root, a, b);
	tree[root].count = tree[2*root].count+tree[2*root+1].count;
}
int search(int root, int left, int right)
{
	int l = tree[root].l ,r = tree[root].r, m = (tree[root].l+tree[root].r)/2, sum = 0;
	if(left<=l && right>=r)//          
		return  sum += tree[root].count;
	if(left>m)//           
		return sum = search(2*root+1, left, right);
	else if(right<=m)//           
		return sum = search(2*root, left, right);
	else
		return sum = search(2*root+1, left, right)+search(2*root, left, right);
}
int main()
{
	int t = 0, n = 0, count = 0, a = 0, b = 0;
	char ch[10];
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d",&n);
		Init(1, 1, n);
		printf("Case %d:
", ++count); while(1) { scanf("%s",ch); if(ch[0] == 'E') break; if(ch[0] == 'A') { scanf("%d %d",&a,&b); modify(1,a,b); } else if(ch[0] == 'S') { scanf("%d %d",&a,&b); modify(1,a,-b); } else { scanf("%d %d",&a,&b); printf("%d
",search(1,a,b)); } } } return 0; }

좋은 웹페이지 즐겨찾기