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 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 |
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.
题解:简单的树状数组;线段树也很简单;
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).
树状数组代码:
#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<queue>
using namespace std;
const int INF=0x3f3f3f3f;
#define mem(x,y) memset(x,y,sizeof(x))
#define SI(x) scanf("%d",&x)
#define PI(x) printf("%d",x)
typedef long long LL;
const int MAXN=1e5+;
int tree[MAXN];
int lowbit(int x){return x&(-x);}
void add(int x,int v){
while(x<MAXN){
tree[x]+=v;
x+=lowbit(x);
}
}
int SUM(int x){
int sum=;
while(x>){
sum+=tree[x];
x-=lowbit(x);
}
return sum;
}
int main(){
int T,kase=;
int N,M;
SI(T);
while(T--){
SI(N);SI(M);
mem(tree,);
for(int i=;i<=N;i++){
int temp;
SI(temp);
add(i,temp);
}
printf("Case %d:\n",++kase);
while(M--){
int q,a,b,ans;
SI(q);
if(q==){
SI(a);a++;
ans=SUM(a)-SUM(a-);
add(a,-ans);
printf("%d\n",ans);
}
else if(q==){
SI(a);SI(b);
a++;
add(a,b);
}
else{
SI(a);SI(b);a++;b++;
printf("%d\n",SUM(b)-SUM(a-));
}
}
}
return ;
}
线段树:
/*
#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<queue>
using namespace std;
const int INF=0x3f3f3f3f;
#define mem(x,y) memset(x,y,sizeof(x))
#define SI(x) scanf("%d",&x)
#define PI(x) printf("%d",x)
typedef long long LL;
const int MAXN=1e5+100;
int tree[MAXN];
int lowbit(int x){return x&(-x);}
void add(int x,int v){
while(x<MAXN){
tree[x]+=v;
x+=lowbit(x);
}
}
int SUM(int x){
int sum=0;
while(x>0){
sum+=tree[x];
x-=lowbit(x);
}
return sum;
}
int main(){
int T,kase=0;
int N,M;
SI(T);
while(T--){
SI(N);SI(M);
mem(tree,0);
for(int i=1;i<=N;i++){
int temp;
SI(temp);
add(i,temp);
}
printf("Case %d:\n",++kase);
while(M--){
int q,a,b,ans;
SI(q);
if(q==1){
SI(a);a++;
ans=SUM(a)-SUM(a-1);
add(a,-ans);
printf("%d\n",ans);
}
else if(q==2){
SI(a);SI(b);
a++;
add(a,b);
}
else{
SI(a);SI(b);a++;b++;
printf("%d\n",SUM(b)-SUM(a-1));
}
}
}
return 0;
}
*/
#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<queue>
using namespace std;
const int INF=0x3f3f3f3f;
#define mem(x,y) memset(x,y,sizeof(x))
#define SI(x) scanf("%d",&x)
#define PI(x) printf("%d",x)
typedef long long LL;
const int MAXN=1e5+;
#define ll root<<1
#define rr root<<1|1
#define lson ll,l,mid
#define rson rr,mid+1,r
int tree[MAXN<<];
int ans;
void pushup(int root){
tree[root]=tree[ll]+tree[rr];
}
void build(int root,int l,int r){
int mid=(l+r)>>;
if(l==r){
SI(tree[root]);
return;
}
build(lson);build(rson);
pushup(root);
}
void update(int root,int l,int r,int A,int B){
if(l==A&&r==A){
tree[root]+=B;
return;
}
int mid=(l+r)>>;
if(mid>=A)update(lson,A,B);
else update(rson,A,B);
pushup(root);
}
void query(int root,int l,int r,int A,int B){
if(l>=A&&r<=B){
ans+=tree[root];
return ;
}
int mid=(l+r)>>;
if(mid>=A)query(lson,A,B);
if(mid<B)query(rson,A,B);
}
int main(){
int T;
int N,M,kase=;
SI(T);
while(T--){
SI(N);SI(M);
build(,,N);
printf("Case %d:\n",++kase);
while(M--){
int q,a,b;
SI(q);
if(q==){
SI(a);a++;
ans=;
query(,,N,a,a);
printf("%d\n",ans);
update(,,N,a,-ans);
}
else if(q==){
SI(a);SI(b);
a++;
update(,,N,a,b);
}
else{
SI(a);SI(b);a++;b++;
ans=;
query(,,N,a,b);
printf("%d\n",ans);
}
}
}
return ;
}
Curious Robin Hood(树状数组+线段树)的更多相关文章
- 洛谷P2414 阿狸的打字机 [NOI2011] AC自动机+树状数组/线段树
正解:AC自动机+树状数组/线段树 解题报告: 传送门! 这道题,首先想到暴力思路还是不难的,首先看到y有那么多个,菜鸡如我还不怎么会可持久化之类的,那就直接排个序什么的然后按顺序做就好,这样听说有7 ...
- 树状数组 && 线段树应用 -- 求逆序数
参考:算法学习(二)——树状数组求逆序数 .线段树或树状数组求逆序数(附例题) 应用树状数组 || 线段树求逆序数是一种很巧妙的技巧,这个技巧的关键在于如何把原来单纯的求区间和操作转换为 求小于等于a ...
- hdu1394(枚举/树状数组/线段树单点更新&区间求和)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 题意:给出一个循环数组,求其逆序对最少为多少: 思路:对于逆序对: 交换两个相邻数,逆序数 +1 ...
- hdu 1166:敌兵布阵(树状数组 / 线段树,入门练习题)
敌兵布阵 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- hdu 5147 Sequence II【树状数组/线段树】
Sequence IITime Limit: 5000/2500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem ...
- 数据结构--树状数组&&线段树--基本操作
随笔目的:方便以后对树状数组(BIT)以及基本线段树的回顾 例题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1166 例题:hdu 1166 敌兵布阵 T ...
- BZOJ_1901_&_ZJU_2112_Dynamic_Rankings_(主席树+树状数组/线段树+(Treap/Splay))
描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1901 给出一个长度为n的数列A,有m次询问,询问分两种:1.修改某一位置的值;2.求区间[l, ...
- BZOJ 3333 排队计划 树状数组+线段树
题目大意:给定一个序列.每次选择一个位置,把这个位置之后全部小于等于这个数的数抽出来,排序,再插回去,求每次操作后的逆序对数 首先我们每一次操作 对于这个位置前面的数 因为排序的数与前面的数位置关系不 ...
- 第十四个目标(dp + 树状数组 + 线段树)
Problem 2236 第十四个目标 Accept: 17 Submit: 35 Time Limit: 1000 mSec Memory Limit : 32768 KB Probl ...
随机推荐
- php5.5以上的版本 开启curl
对于php5.5以上的版本开启方法,需要libeay32.dll.ssleay32.dll.libssh2.dll三个文件拷备到C:\Windows目录下,php.ini中 扩展开启,重启apache ...
- 关于URL编码的问题
在进行WEB开发时,字符集编码常常困扰着我们.我们需要区分两种情况,一是URL编码,二是HTTP Body编码.这两种编码所处理的机制不同. URL编码和解码 客户端负责对URL编码,服务端负责解码. ...
- Windows上安装Xampp后通过命令行进入MariaDB
题外话:读<MYSQL必知必会>,书中让我找个数据库服务器练手,我就去下了个Xampp,由于看的08年网易上的动态网站开发,那个时候Xampp中的m代表MYSQL,后来通过命令行进入MYS ...
- php IP string与整型互换
PHP中有内置函数ip2long可以将ip地址转换整型. 使用long2ip把整型转换回ip地址 例子: $ip = '58.6.207.207'; $ip_int = ip2long($ip); e ...
- Android 指定纯色图标的颜色
最近项目用到了系统图标,但是设计师设计的颜色却与系统图标不一样: 如果每张图片都要用Photoshop进行颜色填充势必增加了工作量,而且不灵活,占资源: 例如同一张图片,希望点击的时候改变颜色 ...
- Hibernate问题之'hibernate.dialect' not set
继前文:Hibernate4中buildSessionFactory方法废弃问题.后 继续有问题.本来之前好好的项目,用了这种新的方法后发现问题. 出现 Connection cannot be n ...
- rem详解
rem这是个低调的css单位,近一两年开始崭露头角,有许多同学对rem的评价不一,有的在尝试使用,有的在使用过程中遇到坑就弃用了.但是我对rem综合评价是用来做web app它绝对是最合适的人选之一. ...
- HTML5 Web Workers来加速您的移动Web应用
一直以来,Web 应用程序被局限在一个单线程世界中.这的确限制了开发人员在他们的代码中的作为,因为任何太复杂的东西都存在冻结应用程序 UI 的风险.通过将多线程引入 Web 应用程… 在本文中,您将使 ...
- 6T GPT 移动硬盘在linux下的挂载
实验室拿来了一个6T的移动硬盘,拿到后没有分区就直接用了,在Windows上用的好好的,插到上Linux后起初不会挂载,折腾了一会,成功挂载,很简单. 运行fdisk –l后,显示如下: 很明显,sd ...
- inline函数
C语言中的inline函数并不是单纯的用函数块内容来替换,也可能存在局部变量啥的.另外,使用inline定义的函数只是建议编译器作为内联函数处理,但并不一定真会这样处理.inline一般直接在头文件中 ...