敌兵布阵 HDU - 1166
#include <iostream> #include <algorithm> #include <cstring> #include <string> #include <vector> #include <map> #include <set> #include <list> #include <deque> #include <queue> #include <stack> #include <cstdlib> #include <cstdio> #include <cmath> #include <iomanip> #define ull unsigned long long #define ll long long #define pb push_back #define mem(sum,x) memset(sum,x,sizeof(sum)) #define rep(i,start,end) for(int i=start;i<=end;i++) #define per(i,end,start) for(int i=end;i>=start;i--) #define tle ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); using namespace std; const int mod = 998244353; const int mxn = 5e4 +7; ll _,n,m,k,t,u,v,w,ans,cnt,ok; int num[mxn] , last[mxn] , key[mxn] ; map< pair< pair<int,int> ,int > ,int > mp; string str; struct node {int val , l , r , sum ;}; int lowbit(int x) {return x&-x;}; void add(int i,int val){while(i<=n) key[i]+=val , i+=lowbit(i) ;} int ask(int x) { int ans = 0 ; while(x>0) ans+=key[x] , x-=lowbit(x) ; return ans; } int main() { tle; int Case = 1 ; for(cin>>t;t;t--) { cout<<"Case "<<Case++<<":"<<endl; cin>>n;memset(key,0,sizeof(key)); rep(i,1,n) cin>>k , add(i,k) ; while(cin>>str && str[0]!='E') { cin>>u>>v; if(str[0]=='Q') cout<<ask(v)-ask(u-1)<<endl; else if(str[0]=='A') add(u,v); else add(u,-v); } } }
#include <cstdio> #include <iostream> #include <algorithm> #include <cstring> using namespace std; typedef long long LL;const int maxn = 2e5+5; const int INF = 0x3f3f3f3f; int sum[maxn*4], A[maxn]; void PushUp(int rt) { sum[rt] = sum[rt*2] + sum[rt*2+1]; } void Build(int l, int r, int rt) { if (l == r) { sum[rt] = A[l]; return ; } int m = (l+r)/2; Build(l, m, rt*2); Build(m+1, r, rt*2+1); PushUp(rt); } void Update(int L, int c, int l, int r, int rt) { if (r == l) { sum[rt] += c; return ; } int m = (l+r)/2; if (L <= m) Update(L, c, l, m, rt*2); else Update(L, c, m+1, r, rt*2+1); PushUp(rt); } int Query(int L, int R, int l, int r, int rt) { if (L <= l && r <= R) { return sum[rt]; } int m = (l+r)/2; int ans = 0; if (L <= m) ans += Query(L, R, l, m, rt*2); if (R > m) ans += Query(L, R, m+1, r, rt*2+1); return ans; } int main()
{int t , n, m, a, b; char str[10] ; scanf("%d", &t); for (int case = 1; case <= t; case++) { printf("Case %d:\n", case); scanf("%d", &n); memset(sum, 0, sizeof(sum)); memset(A, 0, sizeof(A)); for (int i = 1; i <= n; i++) scanf("%d", A + i); Build(1, n, 1); while (scanf("%s", str) && str[0] != 'E') { scanf("%d%d", &a, &b); if (str[0] == 'Q') printf("%d\n", Query(a, b, 1, n, 1)); else if (str[0] == 'A') Update(a, b, 1, n, 1); else if (str[0] == 'S') Update(a, -b, 1, n, 1); } } return 0; }