敌兵布阵
Time Limit: 2000MS Memory Limit: 32768 K
Description
Input
Output
Sample Input
1
10
1 2 3 4 5 6 7 8 9 10
Query 1 3
Add 3 6
Query 2 7
Sub 10 2
Add 6 3
Query 3 10
End
Sample Output
Case 1:
6
33
59
#include <cstdio>
#include<iostream>
using namespace std;
#define lson l , m , rt << 1
#define rson m + 1 , r , rt << 1 | 1
const int maxn = 55555;
int sum[maxn<<2];
void PushUP(int rt)
{
sum[rt] = sum[rt<<1] + sum[rt<<1|1];
}
void build(int l,int r,int rt)
{
if (l == r)
{
scanf("%d",&sum[rt]);
return ;
}
int m = (l + r) >> 1;
build(lson);
build(rson);
PushUP(rt);
}
void update(int p,int add,int l,int r,int rt)
{
if (l == r)
{
sum[rt] += add;
return ;
}
int m = (l + r) >> 1;
if (p <= m) update(p , add , lson);
else update(p , add , rson);
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) >> 1;
int ret = 0;
if (L <= m) ret += query(L , R , lson);
if (R > m) ret += query(L , R , rson);
return ret;
}
int main()
{
int T , n;
scanf("%d",&T);
for (int cas = 1 ; cas <= T ; cas ++)
{
printf("Case %d:\n",cas);
scanf("%d",&n);
build(1 , n , 1);
char op[10];
while (scanf("%s",op))
{
if (op[0] == 'E') break;
int a , b;
scanf("%d%d",&a,&b);
if (op[0] == 'Q') printf("%d\n",query(a , b , 1 , n , 1));
else if (op[0] == 'S') update(a , -b , 1 , n , 1);
else update(a , b , 1 , n , 1);
}
}
return 0;
}
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxx = 50005;
struct Node{
int left,right,sum;
};
int ans[maxx];
Node tree[maxx<<2];
void build(int left,int right,int t)
{
tree[t].left = left;
tree[t].right = right;
if (left == right)
{
tree[t].sum = ans[right];
return;
}
int mid = (left+right)>>1;
build(left,mid,t<<1);
build(mid+1,right,t<<1|1);
tree[t].sum = tree[t<<1].sum + tree[t<<1|1].sum;
}
int qry(int left,int right,int t)
{
if (tree[t].left == left && tree[t].right == right) return tree[t].sum;
int mid = (tree[t].left + tree[t].right) >> 1;
if (right <= mid) return qry(left,right,t<<1);
else if (left > mid) return qry(left,right,t<<1|1);
else return qry(left,mid,t<<1) + qry(mid+1,right,t<<1|1);
}
void upd(int id,int add,int t)
{
if (tree[t].left == tree[t].right)
{
tree[t].sum += add;
return;
}
else
{
tree[t].sum += add;
if(id <= tree[t<<1].right)upd(id,add,t<<1);
else upd(id,add,t<<1|1);
}
}
int main()
{
int T,cnt = 1;
scanf("%d",&T);
while (T--)
{
int N;
char str[5];
memset(str,0,sizeof(str));
memset(ans,0,sizeof(ans));
memset(tree,0,sizeof(tree));
scanf("%d",&N);
for (int i = 1;i <= N;i++)
{
scanf("%d",&ans[i]);
}
build(1,N,1);
printf("Case %d:\n",cnt++);
while (~scanf("%s",str))
{
if(str[0] == 'E') break;
int x,y;
scanf("%d%d",&x,&y);
if (str[0] == 'Q')
{
printf("%d\n",qry(x,y,1));
}
if (str[0] == 'A')
{
upd(x,y,1);
}
if (str[0] == 'S')
{
upd(x,-y,1);
}
}
}
return 0;
}