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 ...
随机推荐
- Oracle EBS-SQL (BOM-14):检查工艺路线明细.sql
select msi.segment1 装配件编码, msi.description ...
- centos 安装amp 运行环境+配置虚拟地址
一.安装 MySQL 首先来进行 MySQL 的安装.打开超级终端,输入: [root@localhost ~]# yum install mysql mysql-server 安装完毕,让 MySQ ...
- 让Cocos2D-X的示例程序运行起来
没有整理好,现在先标记下 安装好环境后可能遇到的问题: 1.cocos2d-X 2.0版本后创建的Android项目提示org.cocos2dx.lib.Cocos2dxActivity找不到问题 解 ...
- COB Epoxy灌膠時氣泡產生的原因與解決方法
COB的黑膠 (Epoxy)有氣泡通常是不被允許的,因為外部氣孔不但會影響到外觀,內部氣孔更有可能會破壞 Wire bonding 的鋁線穩定度.既使在COB製程剛完成的時候沒有通過功能測試,也不代表 ...
- linux 命令总结(转载)
linux 命令总结(转载) 1. 永久更改ip ifconfig eth0 新ip 然后编辑/etc/sysconfig/network-scripts/ifcfg-eth0,修改ip 2.从Lin ...
- Web.config中rewite 节点引起的500.19错误
刚刚接手一个外包的小项目,客户给了发布后的网站文件和数据库,想在本地搭建一套环境先运行下看看网站原有的效果.数据库还原什么都弄好了,数据库字符串也配置好,部署在本地IIS里面,访问了下,结果看到的是5 ...
- 创建FBI树
需求:数串由2^n个'0' '1'数串组成,对于一个数串,有01混合出现,则视为F,全0数串为B,全1数串为I. 将给定数串进行切割,如10010011可以用二叉树表示为 F(10010011) / ...
- ASP.NET State Service
本文来自:http://www.cnblogs.com/jhxk/articles/1648194.html 这一段就是配置应用程序是如何存储Session信息的了.我们以下的各种操作主要是针对这一段 ...
- (转)flash的Socket通讯沙箱和安全策略问题
一.沙箱和安全策略问题 1.此问题发生在连接时,准确地说是连接前,分别两种情况: 1.本地播放 本地播放时,默认情况下Flash Player将不允许swf访问任何网络. 访问http://www.m ...
- 初学IHttpModule的处理
//集成IRequiresSessionState和IReadOnlySessionState是为了在类中访问session public class ModuleBase : IHttpModule ...