Light oj 1112 - Curious Robin Hood【 线段树-单点更新】题解

1.题目

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

2.代码

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<cstdio>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
using namespace std;
const int maxn = 1e5 + 10;
int sum[maxn << 2];
void PushUP(int rt)
{
	sum[rt] = sum[rt << 1] + sum[rt << 1 | 1];
}
void build(int l, int r, int rt)
{
	if (l == r)
	{
		scanf("%d", &sum[rt]);
		return ;
	}
	int m = (l + r) >> 1;
	build(lson);
	build(rson);
	PushUP(rt);
}
void update(int p, int num, int l, int r, int rt)
{
	if (l == r)
	{
		if (num < 0)
		{
			printf("%d\n", sum[rt]);
			sum[rt] = 0;
		}
		else
		{
			sum[rt] += num;

		}
		return ;
	}
	int m = (l + r) >> 1;
	if (p <= m)  update(p, num, lson);
	else         update(p, num, rson);
	PushUP(rt);
}
int query(int L, int R, int l, int r, int rt)
{
	if (L <= l && r <= R)
	{
		return sum[rt];
	}
	int m = (l + r) >> 1;
	int ans = 0;
	if (L <= m)  ans += query(L, R,lson);
	if (R > m)   ans += query(L, R, rson);
	return ans;
}
int main()
{
	int T, n , m;
	scanf("%d", &T);
	for (int cas = 1; cas <= T; cas++)
	{
		scanf("%d%d", &n, &m);
		build(1, n, 1);
		printf("Case %d:\n", cas);
		while(m--)
		{
			int a, b;
			int c;
			scanf("%d", &c);
			if (c==1)
			{
				scanf("%d", &a);
				update(a+1,-1,1,n,1);
			}
			else if (c==2)
			{
				scanf("%d%d", &a, &b);
				update(a+1, b, 1, n, 1);
			}
			else //if(str=='3')
			{
				scanf("%d%d", &a, &b);
				printf("%d\n", query(a+1, b+1, 1, n, 1));
			}

		}
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

林小鹿@

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值