https://cn.vjudge.net/problem/HDU-1166
中文题目,没啥好说,线段树的点修改和区间查询。用树状数组做也是ok的
#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <string> #include <algorithm> #include <cmath> #include <ctime> #include <vector> #include <queue> #include <map> #include <stack> #include <set> #include <bitset> using namespace std; typedef long long ll; typedef unsigned long long ull; #define ms(a, b) memset(a, b, sizeof(a)) #define pb push_back #define mp make_pair #define pii pair<int, int> #define eps 0.0000000001 #define IOS ios::sync_with_stdio(0);cin.tie(0); #define random(a, b) rand()*rand()%(b-a+1)+a #define pi acos(-1) const ll INF = 0x3f3f3f3f3f3f3f3fll; const int inf = 0x3f3f3f3f; const int maxn = 50000 + 10; const int maxm = 200000 + 10; const int mod = 998244353; int n; struct ND{ int l,r; int sum; }tree[maxn<<2]; int a[maxn]; void pushup(int rt){ tree[rt].sum=tree[rt<<1].sum+tree[rt<<1|1].sum; } void build(int rt,int l,int r){ tree[rt].l=l,tree[rt].r=r; if(l==r){ tree[rt].sum=a[l]; return; } int mid=(l+r)>>1; build(rt<<1,l,mid); build(rt<<1|1,mid+1,r); pushup(rt); } void update(int rt,int pos,int val){ if(tree[rt].l==tree[rt].r){ tree[rt].sum+=val; return; } int mid=(tree[rt].l+tree[rt].r)>>1; if(mid>=pos) update(rt<<1,pos,val); else update(rt<<1|1,pos,val); pushup(rt); } int query(int rt,int L,int R){ if(L<=tree[rt].l&&tree[rt].r<=R) return tree[rt].sum; int sum=0; int mid=(tree[rt].l+tree[rt].r)>>1; if(mid<R) sum+=query(rt<<1|1,L,R); if(mid>=L) sum+=query(rt<<1,L,R); return sum; } int main() { #ifdef LOCAL freopen("in.txt", "r", stdin); // freopen("output.txt", "w", stdout); #endif int t; int cas=1; scanf("%d",&t); while(t--){ printf("Case %d: ",cas++); scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%d",&a[i]); build(1,1,n); char op[10]; int x,y; while(scanf("%s",op)==1){ if(op[0]=='E') break; if(op[0]=='A'){ scanf("%d%d",&x,&y); update(1,x,y); }else if(op[0]=='S'){ scanf("%d%d",&x,&y); update(1,x,-y); }else{ scanf("%d%d",&x,&y); printf("%d ",query(1,x,y)); } } } return 0; }