中央情報局要研究敵人究竟演習什麼戰術,所以Tidy要隨時向Derek彙報某一段連續的工兵營地一共有多少人,例如Derek問:“Tidy,馬上彙報第3個營地到第10個營地共有多少人!”Tidy就要馬上開始計算這一段的總人數並彙報。但敵兵營地的人數經常變動,而Derek每次詢問的段都不一樣,所以Tidy不得不每次都一個一個營地的去數,很快就精疲力盡了,Derek對Tidy的計算速度越來越不滿:"你個死肥仔,算得這麼慢,我炒你魷魚!”Tidy想:“你自己來算算看,這可真是一項累人的工作!我恨不得你炒我魷魚呢!”無奈之下,Tidy只好打電話向計算機專家Windbreaker求救,Windbreaker說:“死肥仔,叫你平時做多點acm題和看多點算法書,現在嚐到苦果了吧!”Tidy說:"我知錯了。。。"但Windbreaker已經掛掉電話了。Tidy很苦惱,這麼算他真的會崩潰的,聰明的讀者,你能寫個程序幫他完成這項工作嗎?不過如果你的程序效率不夠高的話,Tidy還是會受到Derek的責罵的.
每組數據第一行一個正整數N(N<=50000),表示敵人有N個工兵營地,接下來有N個正整數,第i個正整數ai代表第i個工兵營地裏開始時有ai個人(1<=ai<=50)。
接下來每行有一條命令,命令有4種形式:
(1) Add i j,i和j爲正整數,表示第i個營地增加j個人(j不超過30)
(2)Sub i j ,i和j爲正整數,表示第i個營地減少j個人(j不超過30);
(3)Query i j ,i和j爲正整數,i<=j,表示詢問第i到第j個營地的總人數;
(4)End 表示結束,這條命令在每組數據最後出現;
每組數據最多有40000條命令
對於每個Query詢問,輸出一個整數並回車,表示詢問的段中的總人數,這個數保持在int以內。
兩種寫法
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <algorithm>
#include <math.h>
#include <map>
#include <vector>
#include <string.h>
#include <string>
#include <queue>
#include <set>
using namespace std;
//ios_base::sync_with_stdio(false);
//#include <bits/stdc++.h>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
typedef long long ll;
const int N = 5e5+10;
char str[10];
struct Node {
int l,r;
ll sum;
int mid() {
return (l+r)>>1;
}
}tree[N<<2];
void PushUp(int rt) //回溯時把左右孩子的值帶上來
{
tree[rt].sum = tree[rt<<1].sum+tree[rt<<1|1].sum;
}
void build(int l, int r, int rt)
{
tree[rt].l = l;
tree[rt].r = r;
if(l == r) {
scanf("%I64d",&tree[rt].sum);//小技巧,輸入葉子的值
return;
}
int m = tree[rt].mid();
build(lson);
build(rson);
PushUp(rt);
}
void update(int pos,int rt,int val)
{
if(tree[rt].l == pos && tree[rt].r == pos) {
tree[rt].sum+=val;
return;
}
int m = tree[rt].mid();
if(pos <= m)
update(pos,rt<<1,val);
else
update(pos,rt<<1|1,val);
PushUp(rt);
}
ll query(int l, int r, int rt)
{
if(l == tree[rt].l&&r == tree[rt].r) return tree[rt].sum;
//PushDown(rt,tree[rt].r-tree[rt].l+1);
int m = tree[rt].mid();
ll res = 0;
if(r <= m) res+=query(l, r, rt<<1);
else if(l > m) res+=query(l, r, rt <<1|1);
else {
res+=query(lson);
res+=query(rson);
}
return res;
}
int main()
{
int T,n;
int Cas = 0;
scanf("%d",&T);
while(T--)
{
int a, b;
scanf("%d",&n);
build(1,n,1);
printf("Case %d:\n", ++Cas);
while(scanf("%s",str))
{
if(str[0] == 'E') break;
else if(str[0] == 'Q') {
scanf("%d%d",&a,&b);
query(a,b,1);
printf("%I64d\n",query(a,b,1));
}
else if(str[0] == 'A') {
scanf("%d%d",&a,&b);
update(a,1,b);
}
else if(str[0] == 'S') {
scanf("%d%d",&a,&b);
update(a,1,-b);
}
}
}
return 0;
}
#include <stdio.h>
#include <iostream>
#include <cstring>
#include <stdlib.h>
#include <math.h>
#include <algorithm>
#include <queue>
using namespace std;
const int maxn = 1<<20;
struct Node {
int value;
int left, right;
}node[maxn*2];
int father[maxn*2];
void BuildTree(int i, int left, int right)
{
node[i].left = left;
node[i].right = right;
node[i].value = 0;
if(left == right) {
father[left] = i;
return;
}
BuildTree(i<<1, left, (right+left)/2);
BuildTree((i<<1)+1, (right+left)/2+1, right);
}
void UpdateTree(int ri, int x)
{
if(ri == 0) return;
node[ri].value += x;
UpdateTree(ri/2, x);
}
int sum = 0;
void Query(int i, int l, int r)
{
if(node[i].left == l && node[i].right == r) {
sum += node[i].value;
return;
}
i = i << 1;
if(l <= node[i].right) {
if(r <= node[i].right)
Query(i, l, r);
else
Query(i, l, node[i].right);
}
i+=1;
if (r >= node[i].left) {
if(l >= node[i].left)
Query(i, l, r);
else
Query(i, node[i].left, r);
}
}
int main()
{
//freopen("in.txt", "r", stdin);
int T;
scanf("%d", &T);
int cas = 0;
while(T--)
{
int n;
scanf("%d", &n);
BuildTree(1, 1, n);
for(int i = 1; i <=n; i++)
{
int a;
scanf("%d", &a);
UpdateTree(father[i], a);
}
char op[10];
int x, y;
printf("Case %d:\n", ++cas);
while(scanf("%s", op))
{
if(op[0] == 'E') break;
else if(op[0] == 'A') {
scanf("%d%d", &x, &y);
UpdateTree(father[x], y);
}
else if(op[0] == 'S') {
scanf("%d%d", &x, &y);
UpdateTree(father[x], -y);
}
else if(op[0] == 'Q') {
scanf("%d%d", &x, &y);
sum = 0;
Query(1, x, y);
printf("%d\n", sum);
}
}
}
return 0;
}