admin管理员组文章数量:1612097
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 nsacks 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 ithinteger 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.
裸的树状数组
#include<stdio.h>
#include<string>
#include<string.h>
#include<cstdio>
#include<algorithm>
#include<iostream>
using namespace std;
const int MAXN=100000+5;//最大元素个数
#define ll long long
int n;//元素个数
ll c[MAXN];//c[i]==A[i]+A[i-1]+...+A[i-lowbit(i)+1]
//返回i的二进制最右边1的值
int lowbit(int i)
{
return i&(-i);
}
//返回A[1]+...A[i]的和
ll sum(int i)
{
ll res=0;
while(i>0)
{
res += c[i];
i -= lowbit(i);
}
return res;
}
//令A[i] += val
void add(int i,ll val)
{
while(i<=n)
{
c[i] += val;
i += lowbit(i);
}
}
int main()
{
int T,kase=1;
scanf("%d",&T);
while(T--)
{
int m;
printf("Case %d:\n",kase++);
scanf("%d%d",&n,&m);
memset(c,0,sizeof(c));
for(int i=1;i<=n;i++)
{
ll v;
scanf("%lld",&v);
add(i,v);
// printf("%d",sum(i)-sum(i-1));
}
int op;
while(m--)
{
scanf("%d",&op);
if(op==3)
{
int i,j;
scanf("%d%d",&i,&j);
i++;j++;
printf("%lld\n",sum(j)-sum(i-1));
}
else if(op==2)
{
int x;
ll v;
scanf("%d%lld",&x,&v);
x++;
add(x,v);
}
else if(op==1)
{
int x;
scanf("%d",&x);
x++;
ll su=sum(x)-sum(x-1);
printf("%lld\n",su);
add(x,-su);
}
}
}
}
版权声明:本文标题:Lightoj 1112 - Curious Robin Hood (树状数组:单点修改 + 区间查询) 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://m.elefans.com/dongtai/1728631020a1167084.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论