Lightoj 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.
#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 【单点改动 + 单点、 区间查询】【树状数组 水题】的更多相关文章
- LightOJ 1112 Curious Robin Hood (单点更新+区间求和)
http://lightoj.com/volume_showproblem.php?problem=1112 题目大意: 1 i 将第i个数值输出,并将第i个值清0 2 i v ...
- POJ 2155 Matrix (二维线段树入门,成段更新,单点查询 / 二维树状数组,区间更新,单点查询)
题意: 有一个n*n的矩阵,初始化全部为0.有2中操作: 1.给一个子矩阵,将这个子矩阵里面所有的0变成1,1变成0:2.询问某点的值 方法一:二维线段树 参考链接: http://blog.csdn ...
- HDU-1754 I Hate It (树状数组模板题——单点更新,区间查询最大值)
题目链接 ac代码(注意字符读入前需要注意回车的影响) #include<iostream> #include<cstdio> #include<cstring> ...
- HDU-1166 敌兵布阵 (树状数组模板题——单点更新,区间求和)
题目链接 AC代码: #include<iostream> #include<cstdio> #include<cstring> #include<algor ...
- Curious Robin Hood(树状数组+线段树)
1112 - Curious Robin Hood PDF (English) Statistics Forum Time Limit: 1 second(s) Memory Limit: 64 ...
- HDU 1166 敌兵布阵 (树状数组 单点修改+区间查询)
题目链接 Problem Description C国的死对头A国这段时间正在进行军事演习,所以C国间谍头子Derek和他手下Tidy又开始忙乎了.A国在海岸线沿直线布置了N个工兵营地,Derek和T ...
- POJ 2155 Matrix(二维树状数组+区间更新单点求和)
题意:给你一个n*n的全0矩阵,每次有两个操作: C x1 y1 x2 y2:将(x1,y1)到(x2,y2)的矩阵全部值求反 Q x y:求出(x,y)位置的值 树状数组标准是求单点更新区间求和,但 ...
- NBOJv2 1050 Just Go(线段树/树状数组区间更新单点查询)
Problem 1050: Just Go Time Limits: 3000 MS Memory Limits: 65536 KB 64-bit interger IO format: % ...
- A Simple Problem with Integers 多树状数组分割,区间修改,单点求职。 hdu 4267
A Simple Problem with Integers Time Limit: 5000/1500 MS (Java/Others) Memory Limit: 32768/32768 K ...
随机推荐
- html5的Form新特性
form新增的输入型控件: 名称 说明 图例 email 电子邮箱文本框,显示跟普通的没什么区别 tel 电话号码,pc端不会有明显的变化,但是移动端会自动切换键盘,输入有误不会阻止默认提交 u ...
- nginx编译安装新模块
nginx的模块是需要重新编译nginx,而不是像apache一样配置文件引用.so 这里以安装第三方ngx_http_google_filter_module模块为例 下载第三方扩展模块ngx_ht ...
- HttpServletResponse对象,自己学习的心得
Web服务器收到客户端的http请求,会针对每一次请求,分别创建一个用于代表请求的request对象,和代表响应response对象. request和response对象既然代表请求和响应,那我们要 ...
- Java接口中的成员变量为什么必须声明为public static final?
我想对于每个Java程序员来说,接口都不陌生,接口中的方法也经常使用.而接口中的成员变量,就显得用得少一点,而对于成员变量为什么必须声明为public static final,可能就更不清楚了,而且 ...
- span标签对于margin-top,margin-bottom无效,但是margin-left,margin-right却是有效的。
首先,span是行内元素,是没有宽高的. 我们来做个简单的布局 <div> <span>内容内容内容内容内容内容内容内容</span> </div> 页 ...
- 努比亚 Z17 mini s (Nubia NX589J) 解锁BootLoader 并刷入recovery ROOT
首先下载好工具链接:链接:https://pan.baidu.com/s/1gher4T9 密码:rypn 备用下载链接:https://pan.baidu.com/s/1nxdzt9Z 本篇教程教你 ...
- 查看Oracle数据库表空间大小,是否需要增加表空间的数据文件
在数据库管理中,磁盘空间不足是DBA都会遇到的问题,问题比较常见. --1查看表空间已经使用的百分比 Sql代码 select a.tablespace_name,a.bytes/1024/1024 ...
- 关于Core里的 StartUp里的方法的理解。
public class Startup { public Startup(IConfiguration configuration) { Configuration = configuration; ...
- 【Oracle】解锁用户
登录oracle数据库时有时会显示ERROR: ORA-28000: the account is locked,这是因为所登录的账号被锁定了. 解决办法: sqlplus / as sysdba; ...
- C#自动缩进排列代码的快捷键 c# 代码重新排版 变整齐
C#自动缩进排列代码的快捷键: ctrl + k + d 1.小技巧, 可以把最后一个}去掉, 重新写下,就可以达到排版的效果. 2.快捷键:编辑-高级-设置文档的格式 快捷键Ctrl+E,D,设置 ...