hdu 1166 敌兵布阵 线段树 点更新

// hdu 1166 敌兵布阵 线段树 点更新
//
// 这道题裸的线段树的点更新,直接写就能够了
//
// 一直以来想要进线段树的坑,结果一直没有跳进去,今天算是跳进去吧,
// 尽管十分简单。十分的水,继续加油 #include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#define ceil(a,b) (((a)+(b)-1)/(b))
#define endl '\n'
#define gcd __gcd
#define highBit(x) (1ULL<<(63-__builtin_clzll(x)))
#define popCount __builtin_popcountll
typedef long long ll;
using namespace std;
const int MOD = 1000000007;
const long double PI = acos(-1.L); template<class T> inline T lcm(const T& a, const T& b) { return a/gcd(a, b)*b; }
template<class T> inline T lowBit(const T& x) { return x&-x; }
template<class T> inline T maximize(T& a, const T& b) { return a=a<b?b:a; }
template<class T> inline T minimize(T& a, const T& b) { return a=a<b?a:b; } const int maxn = 4 * 50008;
int n;
int sum[maxn]; void build(int root,int L,int R){
if (L==R){
scanf("%d",&sum[root]);
return;
}
int M = L + (R - L) / 2;
build(root * 2,L,M);
build(root * 2 + 1, M+1,R);
sum[root] = sum[root * 2] + sum[root * 2 + 1];
} void init(){
scanf("%d",&n);
build(1,1,n);
}
int p,num;
void add_num(int root,int L,int R){
if (L==R){
sum[root] += num;
return;
}
int M = L + (R - L) / 2;
if (p<=M)add_num(root*2,L,M);
else add_num(root*2+1,M+1,R);
sum[root] = sum[root * 2] + sum[root * 2 + 1];
} int query(int root,int ql,int qr,int L,int R){
if (ql<=L&&R<=qr){
return sum[root];
}
int M = L + (R - L) / 2;
int ans = 0;
if (ql<=M) ans += query(root*2,ql,qr,L,M);
if (M<qr) ans += query(root*2+1,ql,qr,M+1,R);
return ans;
} void solve(){
char s[10];
int x,y;
int i = 0;
while(1){
scanf("%s",s);
// cout << i << endl;
if (s[0]=='Q'){
scanf("%d%d",&x,&y);
// cout << i << endl;
printf("%d\n",query(1,x,y,1,n));
}else if (s[0]=='A'){
scanf("%d%d",&x,&y);
p = x;
num = y;
add_num(1,1,n);
}else if (s[0]=='S'){
scanf("%d%d",&x,&y);
p = x;
num = -y;
add_num(1,1,n);
}else {
break;
}
}
} int main() {
int t;
//freopen("G:\\Code\\1.txt","r",stdin);
scanf("%d",&t);
int kase=1;
while(t--){
init();
printf("Case %d:\n",kase++);
solve();
}
return 0;
}
上一篇:HDU1166 敌兵布阵 [线段树模板]


下一篇:HDU1166 敌兵布阵 —— 线段树单点修改