题目来源:[NWPU][2014][SRM][4] A 题
http://vjudge.net/contest/view.action?cid=51335#problem/A
作者:npufz
题目:
Description
Robin Hood likes to loot rich people since he helps the poor people with this money. Instead of keeping all the money together he does another trick. He keeps n sacks where he keeps this money. The sacks are numbered from 0 to n-1.
Now each time he can he can do one of the three tasks.
1) Give all the money of the ith sack to the poor, leaving the sack empty.
2) Add new amount (given in input) in the ith sack.
3) Find the total amount of money from ith sack to jth sack.
Since he is not a programmer, he seeks your help.
Input
Input starts with an integer T (≤ 5), denoting the number of test cases.
Each case contains two integers n (1 ≤ n ≤ 105) and q (1 ≤ q ≤ 50000). The next line contains n space separated integers in the range [0, 1000]. The ith integer denotes the initial amount of money in the ith sack (0 ≤ i < n).
Each of the next q lines contains a task in one of the following form:
1 i Give all the money of the ith(0 ≤ i < n) sack to the poor.
2 i v Add money v (1 ≤ v ≤ 1000) to the ith(0 ≤ i < n) sack.
3 i j Find the total amount of money from ith sack to jth sack (0 ≤ i ≤ j < n).
Output
For each test case, print the case number first. If the query type is 1, then print the amount of money given to the poor. If the query type is 3, print the total amount from ith to jth sack.
Sample Input
1
5 6
3 2 1 4 5
1 4
2 3 4
3 0 3
1 2
3 0 4
1 1
Sample Output
Case 1:
5
14
1
13
2
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
using namespace std;
long long sum1;
typedef struct rootpoint
{
int le,ri;
int sum;
int mid()
{
return (le+ri)/2;
}
} rootpoi;
rootpoi tree[400050];
int built (int root,int l,int r )
{
if(l==r)
{
tree[root].le=l;
tree[root].ri=r;
tree[root].sum=0;
return 0;
}
if(l!=r)
{
tree[root].le=l;
tree[root].ri=r;
tree[root].sum=0;
built(root*2,l,(l+r)/2);
built(root*2+1,(l+r)/2+1,r);
}
return 0;
}
int insert1 (int num,int i,int root)
{
if(tree[root].le==tree[root].ri)
{
tree[root].sum=num;
return 0;
}
if(tree[root].mid()<i)
{
insert1(num,i,root*2+1);
}
else
{
insert1(num,i,root*2);
}
if(tree[root].le!=tree[root].ri)
tree[root].sum=tree[root*2].sum+tree[root*2+1].sum;
return 0;
}
int chaxun(int l,int r,int root)
{
if(tree[root].le==l&&tree[root].ri==r)
{
sum1=sum1+tree[root].sum;
return 0;
}
if(l>tree[root].mid())
{
chaxun(l,r,root*2+1);
}
else if(r<=tree[root].mid())
{
chaxun(l,r,root*2);
}
else
{
chaxun(l,tree[root].mid(),root*2);
chaxun(tree[root].mid()+1,r,root*2+1);
}
return 0;
}
int add (int root,int i,int num)
{
if(tree[root].le==tree[root].ri)
{
tree[root].sum+=num;
return 0;
}
if(tree[root].mid()<i)
{
add(root*2+1,i,num);
}
else
{
add(root*2,i,num);
}
if(tree[root].le!=tree[root].ri)
tree[root].sum=tree[root*2].sum+tree[root*2+1].sum;
return 0;
}
int sub(int root ,int i)
{
if(tree[root].le==tree[root].ri)
{
printf("%d\n",tree[root].sum);
tree[root].sum=0;
return 0;
}
if(tree[root].mid()<i)
{
sub(root*2+1,i);
}
else
{
sub(root*2,i);
}
if(tree[root].le!=tree[root].ri)
tree[root].sum=tree[root*2].sum+tree[root*2+1].sum;
return 0;
}
int main()
{
int n,i,qianshu,left1,right1,bianhao,t,cnt,j,q;
scanf("%d",&t);
for(cnt=1;cnt<=t;cnt++)
{
scanf("%d%d",&n,&q);
built (1,1,n);
for(i=1;i<=n;i++)
{
scanf("%d",&qianshu);
insert1(qianshu,i,1);
}
int s1;
printf("Case %d:\n",cnt);
for(j=0;j<q;j++)
{
scanf("%d",&s1);
if(s1==3)
{
scanf("%d%d",&left1,&right1);
sum1=0;
chaxun(left1+1,right1+1,1);
printf("%lld\n",sum1);//printf("%I64d\n",sum1);会WA掉
}
else if(s1==2)
{
scanf("%d%d",&bianhao,&qianshu);
add(1,bianhao+1,qianshu);
}
else
{
scanf("%d",&bianhao);
sub(1,bianhao+1);
}
}
}
return 0;
}
反思:
看懂题目,看准输入输出要求很重要,一开始I64D 就WA了,改成LLD就A了