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 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(树状数组+线段树)的更多相关文章

  1. 洛谷P2414 阿狸的打字机 [NOI2011] AC自动机+树状数组/线段树

    正解:AC自动机+树状数组/线段树 解题报告: 传送门! 这道题,首先想到暴力思路还是不难的,首先看到y有那么多个,菜鸡如我还不怎么会可持久化之类的,那就直接排个序什么的然后按顺序做就好,这样听说有7 ...

  2. 树状数组 && 线段树应用 -- 求逆序数

    参考:算法学习(二)——树状数组求逆序数 .线段树或树状数组求逆序数(附例题) 应用树状数组 || 线段树求逆序数是一种很巧妙的技巧,这个技巧的关键在于如何把原来单纯的求区间和操作转换为 求小于等于a ...

  3. hdu1394(枚举/树状数组/线段树单点更新&区间求和)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 题意:给出一个循环数组,求其逆序对最少为多少: 思路:对于逆序对: 交换两个相邻数,逆序数 +1 ...

  4. hdu 1166:敌兵布阵(树状数组 / 线段树,入门练习题)

    敌兵布阵 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  5. hdu 5147 Sequence II【树状数组/线段树】

    Sequence IITime Limit: 5000/2500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem ...

  6. 数据结构--树状数组&&线段树--基本操作

    随笔目的:方便以后对树状数组(BIT)以及基本线段树的回顾 例题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1166 例题:hdu 1166 敌兵布阵 T ...

  7. BZOJ_1901_&_ZJU_2112_Dynamic_Rankings_(主席树+树状数组/线段树+(Treap/Splay))

    描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1901 给出一个长度为n的数列A,有m次询问,询问分两种:1.修改某一位置的值;2.求区间[l, ...

  8. BZOJ 3333 排队计划 树状数组+线段树

    题目大意:给定一个序列.每次选择一个位置,把这个位置之后全部小于等于这个数的数抽出来,排序,再插回去,求每次操作后的逆序对数 首先我们每一次操作 对于这个位置前面的数 因为排序的数与前面的数位置关系不 ...

  9. 第十四个目标(dp + 树状数组 + 线段树)

    Problem 2236 第十四个目标 Accept: 17    Submit: 35 Time Limit: 1000 mSec    Memory Limit : 32768 KB  Probl ...

随机推荐

  1. android layout的布局

    1.android:layout_width.android:layout_heigth 表示控件的大小,长宽 2.andoid:layout_gravity .android:gravity表示控件 ...

  2. YY的困境:除了终止私有化 还有更多的担忧

    界面 刘莎 已大热一段时间的中概股私有化浪潮随着中国股市的下跌而降温,很多在美上市的中概股不得不因此叫停私有化,欢聚时代(下称YY)首当其冲,成为私有化大军中首个被迫撤退的中资公司. 虽然从表面看,私 ...

  3. [WPF 如何] 如何向 ComboBox 添加一个空白选项

    原文:[WPF 如何] 如何向 ComboBox 添加一个空白选项 看到这个问题,你可能会蔑视一笑 : 这也能成文章? 确实,你只需要在 ItemsSource 的0位置上插入一个空白的项就是了,如: ...

  4. wordpress参考网站

    wordpress大学http://www.wpdaxue.com/post-tags-and-categories-for-pages.html

  5. Google map v3 using simple tool file google.map.util.js v 1.1

    /** * GOOGLE地图开发使用工具 * @author BOONYACHENGDU@GMAIL.COM * @date 2013-08-23 * @notice 地图容器的z-index不能小于 ...

  6. 【LeetCode练习题】Evaluate Reverse Polish Notation

    Evaluate Reverse Polish Notation Evaluate the value of an arithmetic expression in Reverse Polish No ...

  7. 【C/C++】Linux下system()函数引发的错误

    http://my.oschina.net/renhc/blog/54582 [C/C++]Linux下system()函数引发的错误 恋恋美食  恋恋美食 发布时间: 2012/04/21 11:3 ...

  8. ListView嵌套ListView时发生:View too large to fit into drawing cache的问题

    watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcXkxMzg3/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/d ...

  9. 利用maven中resources插件的copy-resources目标进行资源copy和过滤

    maven用可以利用如下配置进行资源过滤,pom.xml的配置如下: <build> <!-- 主资源目录 --> <resources> <resource ...

  10. .NET之反射(1)

    .NET之反射 第一版 内容摘要 前言 反射 查看元数据 加载程序集 晚期绑定 方法调用 反射.晚期绑定和自定义特性的使用背景 参考资料 前言 关于反射的内容,向来是隐藏在C#高级编程类别中的大部头书 ...