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



题意:给你N个数和M次查询,查询分三种


一,1 i   表示查询i处的值。并把 i 处的值变为0。
二,2 i v 表示将 i 处 值添加 v。

三,3 x y 查询 区间[x, y]的值。

水题。直接用树状数组就能够了,不是必需用线段树。


AC代码:注意下标是从0開始到N-1的


#include <cstdio>
#include <cstring>
#include <queue>
#include <stack>
#include <vector>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#define MAXN 100000+10
#define MAXM 60000+10
#define INF 1000000
#define eps 1e-8
using namespace std;
int N, M;
int C[MAXN<<1];
int k = 1;
int lowbit(int x)
{
return x & (-x);
}
int sum(int x)
{
int s = 0;
while(x > 0)
{
s += C[x];
x -= lowbit(x);
}
return s;
}
void update(int x, int d)
{
while(x <= N)
{
C[x] += d;
x += lowbit(x);
}
}
void solve()
{
int x, y, d, op;
printf("Case %d:\n", k++);
while(M--)
{
scanf("%d", &op);
if(op == 1)
{
scanf("%d", &x);
x++;
printf("%d\n", sum(x) - sum(x-1));
int t = sum(x) - sum(x-1);
update(x, -t);
}
else if(op == 2)
{
scanf("%d%d", &x, &d);
x++;
update(x, d);
}
else
{
scanf("%d%d", &x, &y);
x++, y++;
printf("%d\n", sum(y) - sum(x-1));
}
}
}
int main()
{
int t;
scanf("%d", &t);
while(t--)
{
scanf("%d%d", &N, &M);
memset(C, 0, sizeof(C));
int a;
for(int i = 1; i <= N; i++)
{
scanf("%d", &a);
update(i, a);
}
solve();
}
return 0;
}


Lightoj 1112 - Curious Robin Hood 【单点改动 + 单点、 区间查询】【树状数组 水题】的更多相关文章

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

    http://lightoj.com/volume_showproblem.php?problem=1112 题目大意: 1 i        将第i个数值输出,并将第i个值清0 2 i v     ...

  2. POJ 2155 Matrix (二维线段树入门,成段更新,单点查询 / 二维树状数组,区间更新,单点查询)

    题意: 有一个n*n的矩阵,初始化全部为0.有2中操作: 1.给一个子矩阵,将这个子矩阵里面所有的0变成1,1变成0:2.询问某点的值 方法一:二维线段树 参考链接: http://blog.csdn ...

  3. HDU-1754 I Hate It (树状数组模板题——单点更新,区间查询最大值)

    题目链接 ac代码(注意字符读入前需要注意回车的影响) #include<iostream> #include<cstdio> #include<cstring> ...

  4. HDU-1166 敌兵布阵 (树状数组模板题——单点更新,区间求和)

    题目链接 AC代码: #include<iostream> #include<cstdio> #include<cstring> #include<algor ...

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

    1112 - Curious Robin Hood    PDF (English) Statistics Forum Time Limit: 1 second(s) Memory Limit: 64 ...

  6. HDU 1166 敌兵布阵 (树状数组 单点修改+区间查询)

    题目链接 Problem Description C国的死对头A国这段时间正在进行军事演习,所以C国间谍头子Derek和他手下Tidy又开始忙乎了.A国在海岸线沿直线布置了N个工兵营地,Derek和T ...

  7. POJ 2155 Matrix(二维树状数组+区间更新单点求和)

    题意:给你一个n*n的全0矩阵,每次有两个操作: C x1 y1 x2 y2:将(x1,y1)到(x2,y2)的矩阵全部值求反 Q x y:求出(x,y)位置的值 树状数组标准是求单点更新区间求和,但 ...

  8. NBOJv2 1050 Just Go(线段树/树状数组区间更新单点查询)

    Problem 1050: Just Go Time Limits:  3000 MS   Memory Limits:  65536 KB 64-bit interger IO format:  % ...

  9. A Simple Problem with Integers 多树状数组分割,区间修改,单点求职。 hdu 4267

    A Simple Problem with Integers Time Limit: 5000/1500 MS (Java/Others)    Memory Limit: 32768/32768 K ...

随机推荐

  1. Appium - xpath

    基本属性定位 以淘宝app为例,定位左上角扫一扫按钮 1.可以通过text文本定位到 //*[@text='text文本属性'] # 定位text driver.find_element_by_xpa ...

  2. D - Vanya and Fence

    Problem description Vanya and his friends are walking along the fence of height h and they do not wa ...

  3. dotnetnuke 中使用ado.net entityframework 如果在程序中动态调用系统的连接字符串信息

    1,打开如下图的Model1.Context.cs文件 2,找到 Base:(ConnString.conn)是我改的.默认生成的是"name=实体连接字符串" Connstrin ...

  4. I2C controller core之Bit controller(05)

    6 generate statemachine 1 -- port cmd_ack : out std_logic; -- command completed 4 -- architecture ty ...

  5. SQL Server存储过程作业(三)

    阶段4:练习——插入入住客人记录 需求说明 使用存储过程将入住客人信息插入客人信息表中,要求: 检查身份证号必须是18个字符组成 押金的默认值为1000元 如果客人记录插入成功,输出客人流水号:否则输 ...

  6. 学习廖雪峰的Python教程之Python基础

    一.缩进 编译器或者解释器就是负责把符合语法的程序代码转换成CPU能够执行的机器码,然后执行. 以#开头的语句是注释,注释是给人看的,可以是任意内容,解释器会忽略掉注释.其他每一行都是一个语句,当语句 ...

  7. Python中join函数和os.path.join用法

    Python中有join和os.path.join()两个函数,具体作用如下: join:连接字符串数组.将字符串.元组.列表中的元素以指定的字符(分隔符)连接生成一个新的字符串 os.path.jo ...

  8. Docker拉取images时报错Error response from daemon

    docker拉取redis时,抛出以下错误: [master@localhost ~]$ docker pull redis Using default tag: latest Error respo ...

  9. Day 1 初识python

    1.Python简介 Python的历史 1989年圣诞节:Guido von Rossum开始写Python语言的编译器. 1991年2月:第一个Python编译器(同时也是解释器)诞生,它是用C语 ...

  10. appium分层自动化的封装

    1.创建一个case包,start_app的python文件 #coding=utf-8from appium import webdriverfrom util.read_init import R ...