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;
}


最新文章

  1. 你还可以再诡异点吗——SQL日志文件不断增长
  2. Jquery ui autocomplete简单api
  3. 发布FTP服务,防火墙配置
  4. PHP正则匹配邮件地址、URL
  5. SVN中文件属性
  6. Web 检测代码 web analysis 开源 open source
  7. SQL Server 日志文件增长原因定位
  8. 笔记之Cyclone IV 第一卷第一章FPGA 器件系列概述
  9. spring boot认识
  10. jQuery事件 (jQuery实现图片轮播)
  11. CSRF、XSS、clickjacking、SQL 的攻击与防御
  12. IntelliJ IDEA 注册码 (秘钥)
  13. 洛谷P3183食物链题解
  14. 数据库并发控制及SQL Server的并发控制机制
  15. python:异常处理、自定义异常、断言
  16. MySql语句分类
  17. Go语言编程 (许式伟 等 著)
  18. Java并发-懒汉式单例设计模式加volatile的原因
  19. .8-浅析express源码之请求处理流程(1)
  20. 锋利的Jquery之插件Cookie记住密码

热门文章

  1. ASP.NET Core 多环境
  2. 快速掌握C#7
  3. BZOJ 1407 exgcd
  4. 第一课trie 树 POJ 2001
  5. B - Is your horseshoe on the other hoof?
  6. SpringMVC参数绑定(二)
  7. 单个句子&lt;code&gt; 多行代码显示&lt;pre&gt; 键盘输入&lt;kbd&gt;
  8. 【sqli-labs】 less21 Cookie Injection- Error Based- complex - string ( 基于错误的复杂的字符型Cookie注入)
  9. 【转】虚拟化(四):vsphere高可用功能前提-共享存储搭建
  10. 【转载】使用JSONObject生成和解析json