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. SpringCloud(二) 服务注册与发现Eureka

    1.eureka是干什么的? 上篇说了,微服务之间需要互相之间通信,那么通信就需要各种网络信息,我们可以通过使用硬编码的方式来进行通信,但是这种方式显然不合适,不可能说一个微服务的地址发生变动,那么整 ...

  2. Python-操作符和表达式

    //: 除后向下取正  -3//2=-2 **: 幂 3**3 = 27 not: ! and: && or: || 除了以上几个之外,其余与C++相同 length = 3 widt ...

  3. Spring 的优秀工具类盘点---转

    第 1 部分: 文件资源操作和 Web 相关工具类 http://www.ibm.com/developerworks/cn/java/j-lo-spring-utils1/ 文件资源操作 文件资源的 ...

  4. objc_setAssociatedObject获取cell上button对应所在的行

    #import <UIKit/UIKit.h> @interface TestCell : UITableViewCell @property (weak, nonatomic) IBOu ...

  5. CSS选择器优先级计算

    优先级从高到低排列,浏览器优先满足前面的规则 1,!important优先级最高 2,内联样式 3,作者>读者>浏览器 4,优先级权重加法 id选择器+100/个 类/伪类选择器+10/个 ...

  6. avaScript中变量的声明和赋值

    变量是指程序中一个已经命名的存储单元,它的主要作用就是为数据操作提供存放信息的容器.变量是相对常量而言的.常量是一个不会改变的固定值,而变量的值可能会随着程序的执行而改变.变量有两个基本特征,即变量名 ...

  7. Android_传感器光学

    上一篇写了一个小案例方向传感器,与这光学传感器原理大致类似,但其实代码的主要区别得到的类型不一样在这里我一一列举出来: * Sensor.TYPE_ORIENTATION:方向传感器. * Senso ...

  8. VHDL之code structure

     1 VHDL units VHDL code is composed of at least 3 fundamental sections: 1) LIBRARY declarations: Con ...

  9. 三维重建7:Visual SLAM算法笔记

    VSLAM研究了几十年,新的东西不是很多,三维重建的VSLAM方法可以用一篇文章总结一下. 此文是一个好的视觉SLAM综述,对视觉SLAM总结比较全面,是SLAM那本书的很好的补充.介绍了基于滤波器的 ...

  10. 图像连通域检测的2路算法Code

    本文算法描述参考链接:http://blog.csdn.net/icvpr/article/details/10259577 两遍扫描法: (1)第一次扫描: 访问当前像素B(x,y),如果B(x,y ...