admin管理员组文章数量:1612097
题目链接:
http://lightoj/volume_showproblem.php?problem=1112
1112 - Curious Robin Hood
PDF (English) | Statistics | Forum |
Time Limit: 1 second(s) | Memory Limit: 64 MB |
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 | Output for 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 | Case 1: 5 14 1 13 2 |
Notes
Dataset is huge, use faster I/O methods.
PROBLEM SETTER: JANE ALAM JAN
题意:
给你N个数和M次查询,查询分三种
一,1 i 表示查询i处的值,并把 i 处的值变为0。
二,2 i v 表示将 i 处 值增加 v。
三,3 x y 查询 区间[x, y]的值。
思路:
直接树状数组就可以,注意的是值变为0的时候不能直接 -c[i],需要 sum(i)-sum(i-1)
树状数组的的下标是0--n-1
This is the code
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<iostream>
#include<iomanip>
#include<list>
#include<map>
#include<queue>
#include<sstream>
#include<stack>
#include<string>
#include<set>
#include<vector>
using namespace std;
#define PI acos(-1.0)
#define EPS 1e-8
#define MOD 1e9+7
#define LL long long
#define ULL unsigned long long //1844674407370955161
#define INT_INF 0x7f7f7f7f //2139062143
#define LL_INF 0x7f7f7f7f7f7f7f7f //9187201950435737471
// ios::sync_with_stdio(false);
// 那么cin, 就不能跟C的 scanf,sscanf, getchar, fgets之类的一起使用了。
const int dr[]={0, 0, -1, 1, -1, -1, 1, 1};
const int dc[]={-1, 1, 0, 0, -1, 1, -1, 1};
const int maxn=100000+10;
int c[maxn];
int lowbit(int x)
{
return x&(-x);
}
void add(int x,int v)//添加
{
while(x<=maxn)
{
c[x]+=v;
x+=lowbit(x);
}
}
int sum(int x)//得到a[1]-a[x]的和
{
int ret=0;
while(x>0)
{
ret+=c[x];
x-=lowbit(x);
}
return ret;
}
int main()
{
int T;
scanf("%d",&T);
for(int t=1;t<=T;++t)
{
memset(c,0,sizeof(c));
int n,m,w;
scanf("%d%d",&n,&m);
for(int i=1;i<=n;++i)//i不能为零,但是本题是从0--n-1,下面需要--
{
scanf("%d",&w);
add(i,w);
}
int x,y;
printf("Case %d:\n",t);
while(m--)
{
int tem;
scanf("%d",&tem);
switch(tem)
{
case 1:
{
scanf("%d",&x);
x++;
int Sum=sum(x)-sum(x-1);
printf("%d\n",Sum);
add(x,-Sum);
break;
}
case 2:
{
scanf("%d%d",&x,&y);
x++;
add(x,y);
break;
}
case 3:
{
scanf("%d%d",&x,&y);
x++;
y++;
printf("%d\n",sum(y)-sum(x-1));
break;
}
}
}
}
return 0;
}
版权声明:本文标题:LightOJ 1112 - Curious Robin Hood 树状数组 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://m.elefans.com/dianzi/1728631231a1167109.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论