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 ...
随机推荐
- Myeclipse 2014配置SVN详细图解
1.什么是SVN? 管理软件开发过程中的版本控制工具. 2.myeclipse安装SVN插件步骤,以myeclipse 2014为例. (1)下载SVN插件 http://subclipse.tigr ...
- JS如何设置计算几天前的时间?
计算多少天前的具体时间.比如今天是9月5日,那7天前正常就是8月29了. 之前曾经直接用时间进行加减,吃了大亏,后来脑残到直接写了一个很复杂的计算闰年,闰月,30.31.28的月份 现在分享一下. f ...
- 【关于360极速浏览器的xx极速模式自动切换到兼容模式】
原理上是可以的. 1 360基于Chromium 开源浏览器内核,它本身就是一个壳子.. 2 7.0之后的极速浏览器,不支持 它官方的那个声明标记.<meta name=”renderer” ...
- hdu 4686 Arc of Dream_矩阵快速幂
题意:略 构造出矩阵就行了 | AX 0 AXBY AXBY 0 | ...
- magento后台登陆404、Front controller reached 100 router match iterations的解决方案
(1)执行如下sql SET SQL_SAFE_UPDATES=; SET FOREIGN_KEY_CHECKS=; UPDATE `core_store` SET store_id = WHERE ...
- hdu - 4709 - Herding
题意:给出N个点的坐标,从中取些点来组成一个多边形,求这个多边形的最小面积,组不成多边形的输出"Impossible"(测试组数 T <= 25, 1 <= N < ...
- JS中的逻辑哲学
1.幻灯片播放. 有重用功能的代码要封入一个函数内,尽量减少调用出口(一般传入的参数为索引值),以便调用: 计数器放在最终调用的函数那里,index++: 明确那部分函数执行什么功能,将代码块只放在相 ...
- 标准C++的string类使用
原文:http://www.cnblogs.com/xFreedom/archive/2011/05/16/2048037.html 要想使用标准C++中string类,必须要包含#include & ...
- BestCoder Round #75 1003 - King's Order
国王演讲后士气大增,但此时战争还没有结束,国王时不时要下发命令. 由于国王的口吃并没有治愈,所以传令中可能出现:“让第三军-军-军,到前线去” 这样的命令.由于大洋国在军队中安插了间谍 , 战事紧急, ...
- ubuntu 12.04 安装谷歌浏览器
http://hi.baidu.com/kevin276/item/29bc1c96a208fabc82d29542 sudo dpkg -i google-chrome-stable_current ...