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 ...
随机推荐
- Windows消息拦截技术的应用(作者博客里有许多相关文章)
民航合肥空管中心 周毅 一.前 言 众所周知,Windows程式的运行是依靠发生的事件来驱动.换句话说,程式不断等待一个消息的发生,然后对这个消息的类型进行判断,再做适当的处理.处理完此次消息后又回到 ...
- 在MFC下实现图像放大镜
当我们想仔细观察某个细微的东西时,一般都会使用放大镜.而要看清显示在计算机屏幕上的图片或文字时通常也可以借助于Windows操作系统附带的放大程序来实现.但该程序只能以固定的放大倍数去进行观看,有时并 ...
- ZZ的计算器
Problem Description ZZ自从上大学以来,脑容量就是以SB计算的,这个吃货竟然连算术运算也不会了,可是当今的计算机可是非常强大的,作为ACMer, 几个简单的算术又算得了什么呢?可是 ...
- 2014.8.15模拟赛【公主的工作】&&bzoj1046[HAOI2007]上升序列
bzoj题目是这样的 Description 对于一个给定的S={a1,a2,a3,…,an},若有P={ax1,ax2,ax3,…,axm},满足(x1 < x2 < … < xm ...
- 关于清晰讲解linux正则表达式的博文分享
http://www.cnblogs.com/chengmo/archive/2010/10/10/1847287.html linux shell 正则表达式(BREs,EREs,PREs)差异比 ...
- CentOS7 yum lamp 虚拟主机配置 lamp各组件简单影响性能的参数调整--for 一定的环境需求
LAMP Server on CentOS 7 Updated Tuesday, January 13, 2015 by Joel Kruger This guide provides step-by ...
- [置顶] Ajax核心--XMLHttpRequest对象
XMLHttpRequest 对象是AJAX功能的核心,学习XMLHttpRequest对象就先从创建XMLHttpRequest 对象开始,了解在不同的浏览器中创建XMLHttpRequest 对象 ...
- Sonar入门(二): Maven集成Sonar
Sonar对maven提供了简单可配的支持,要做的事情很简单——在maven/conf下settings.xml <profiles></profiles>标签之间添加如下内容 ...
- logback.xml配置详解
先附上本文分析用的例子: <?xml version="1.0" encoding="UTF-8" ?> <configuration> ...
- Unity 之 Redux 模式(第二篇)—— Rigidbody 改造,摄像机控制
作者:软件猫 日期:2016年12月8日 转载请注明出处:http://www.cnblogs.com/softcat/p/6144041.html 上一篇文章中存在一个很严重的问题,首先我们先让 M ...