Light oj 1112 - Curious Robin Hood【单点更新】

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.

题意:

给出一组数,进行题目给出的三种操作,输出需要的结果。

题解:

最基础的线段树题目,只需要单点更新和区间查询

很久没做线段树了,没想到还能 1 A....这是题目有多水....

/*
http://blog.csdn.net/liuke19950717
*/
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
const int maxn=1e5+5;
int x[maxn],sumtree[maxn*4],kase=0;
void build(int rt,int l,int r)
{
	if(l==r)
	{
		sumtree[rt]=x[l];
		return;
	}
	int mid=(l+r)>>1,tp=rt<<1;
	build(tp,l,mid);build(tp|1,mid+1,r);
	sumtree[rt]=sumtree[tp]+sumtree[tp|1];
}
void update(int rt,int l,int r,int a,int b)
{
	if(l==r)
	{
		sumtree[rt]=x[l]=b;
		return;
	}
	int mid=(l+r)>>1,tp=rt<<1;
	if(mid>=a)
	{
		update(tp,l,mid,a,b);
	}
	else
	{
		update(tp|1,mid+1,r,a,b);
	}
	sumtree[rt]=sumtree[tp]+sumtree[tp|1];
}
int find(int rt,int l,int r,int a,int b)
{
	if(l>=a&&r<=b)
	{
		return sumtree[rt];
	}
	int mid=(l+r)>>1,tp=rt<<1,ans=0;
	if(mid>=a)
	{
		ans+=find(tp,l,mid,a,b);
	}
	if(mid<b)
	{
		ans+=find(tp|1,mid+1,r,a,b);
	}
	return ans;
}
int main()
{
	//freopen("shuju.txt","r",stdin);
	int t;
	scanf("%d",&t);
	while(t--)
	{
		int n,m;
		memset(sumtree,0,sizeof(sumtree));
		scanf("%d%d",&n,&m);
		for(int i=1;i<=n;++i)
		{
			scanf("%d",&x[i]);
		}
		build(1,1,n);
		printf("Case %d:\n",++kase);
		while(m--)
		{
			int num;
			scanf("%d",&num);
			if(num==1)
			{
				int a;
				scanf("%d",&a);
				printf("%d\n",x[a+1]);
				update(1,1,n,a+1,0);
			}
			else if(num==2)
			{
				int a,b;
				scanf("%d%d",&a,&b);
				update(1,1,n,a+1,x[a+1]+b);
			}
			else
			{
				int a,b;
				scanf("%d%d",&a,&b);
				printf("%d\n",find(1,1,n,a+1,b+1));
			}
		}
	}
	return 0;
}
时间: 2024-10-07 09:20:21

Light oj 1112 - Curious Robin Hood【单点更新】的相关文章

Lightoj 1112 - Curious Robin Hood 【单点改动 + 单点、 区间查询】【树状数组 水题】

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.

Lightoj 1112 - Curious Robin Hood 【单点修改 + 单点、 区间查询】【树状数组 水题】

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.

LightOj 1112 Curious Robin Hood(线段树||树状数组)

Curious Robin Hood 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.

LightOJ 1112 Curious Robin Hood (单点更新+区间求和)

http://lightoj.com/volume_showproblem.php?problem=1112 题目大意: 1 i        将第i个数值输出,并将第i个值清0 2 i v     将第i个数值加v 3 i j      输出从i到j的数值和 简单的单点更新+区间求和,代码很简单的模板 但此题有一个神坑的地方当操作为1是输出第i个数值不是直接输出,而是通过查找输出(太坑了...) #include<stdio.h> #include<string.h> #incl

Curious Robin Hood(树状数组+线段树)

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 tri

light oj 1348 树链剖分(单点更新区间求值)

http://lightoj.com/volume_showproblem.php?problem=1348 Finally the Great Magical Lamp was in Aladdin's hand. Now he wanted to return home. But he didn't want to take any help from the Genie because he thought that it might be another adventure for hi

Light OJ 1080 - Binary Simulation - (线段树区间更新 单点查询)

Description Given a binary number, we are about to do some operations on the number. Two types of operations can be here. 'I i j'    which means invert the bit from i to j (inclusive) 'Q i'    answer whether the ith bit is 0 or 1 The MSB (most signif

Light OJ 1411 Rip Van Winkle`s Code 线段树成段更新

题目来源:Light OJ 1411 Rip Van Winkle`s Code 题意:3中操作 1种查询 求区间和 其中每次可以把一段区间从左到右加上1,2,3,...或者从右到左加上...3,2,1 或者把某个区间的数都置为v 思路:我是加了6个域 add是这段区间每个数都要加上add  add是这么来的 对与123456...这个等差数列 可能要分为2个区间 那么我就分成123和123 两个右边的等差数列每个数还应该加上3 所以右区间add加3 v是这个区间都要置为v 他的优先级最高 b是

Codeforces Round #352 (Div. 2) D. Robin Hood

题目连接请戳此处 题意:n个人每人有一定的财富值ci,每天Robin Hood从最富的人手中取财富1分给最穷的人(取后最穷, 即可以退回),若有多个最穷的人,任选一个给出财富值.问k天后最富的人和最穷的人财富差值为多少. 1 ≤ n ≤ 500 000, 0 ≤ k ≤ 109 1 ≤ ci ≤ 109 分析: 每一天随着财富值的取和给,最穷的人和最富的人都在动态更新. 最后要求的是  (richest-poorest)min,那么  要使这个等式最小,只需求出k天后richest的最小值和po