hdu1166(线段树单点更新区间查询)

/*****************************************
Author      :Crazy_AC(JamesQi)
Time        :2015
File Name   :
*****************************************/
// #pragma comment(linker, "/STACK:1024000000,1024000000")
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <sstream>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <vector>
#include <map>
#include <set>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <limits.h>
using namespace std;
#define MEM(a,b) memset(a,b,sizeof a)
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> ii;
const int inf = 1 << 30;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const int maxn = 50010;
int T,n;
int sum[maxn << 2];
inline int Readint(){
	char c = getchar();
	while(!isdigit(c)) c = getchar();
	int x = 0;
	while(isdigit(c)){
		x = x * 10 + c - '0';
		c = getchar();
	}
	return x;
}
void Build(int L,int R,int rt){
	// sum[rt] = 0;
	if (L == R) {
		sum[rt] = Readint();
		return ;
	}
	int mid = (L + R) >> 1;
	Build(L,mid,rt << 1);
	Build(mid + 1,R,rt << 1 | 1);
	sum[rt] = sum[rt << 1] + sum[rt << 1 | 1];
}
void Insert(int L,int R,int rt,int pos,int val){
	if (L == R){
		sum[rt] += val;
		return ;
	}
	int mid = (L + R) >> 1;
	if (pos <= mid) Insert(L,mid,rt << 1,pos,val);
	else Insert(mid + 1,R,rt << 1 | 1,pos,val);
	sum[rt] = sum[rt << 1] + sum[rt << 1 | 1];
	return ;
}
int Query(int L,int R,int rt,int l,int r){
	if (l <= L && R <= r){
		return sum[rt];
	}
	int mid = (L + R) >> 1;
	int res = 0;
	if (l <= mid) res += Query(L,mid,rt << 1,l,r);
	if (r > mid) res += Query(mid + 1,R,rt << 1 | 1,l,r);
	return res;
}
int main()
{	
	// freopen("in.txt","r",stdin);
	// freopen("out.txt","w",stdout);
	int iCase = 0;
	T = Readint();
	while(T--){
		n = Readint();
		Build(1,n,1);
		int x;
		char op[10];
		int a,b;
		printf("Case %d:\n",++iCase);
		while(scanf("%s",op) != EOF){
			if (strcmp(op,"End") == 0) break;
			a = Readint();
			b = Readint();
			if (strcmp(op,"Query") == 0){
				printf("%d\n",Query(1,n,1,a,b));
			}
			else if (strcmp(op,"Sub") == 0){
				Insert(1,n,1,a,-b);
			}
			else {
				Insert(1,n,1,a,b);
			}
		}
	}
	return 0;
}

你可能感兴趣的:(hdu1166(线段树单点更新区间查询))