链接:

http://acm.hust.edu.cn/vjudge/contest/view.action?cid=88230#problem/D (密码0817)

Description

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

1

5 6

3 2 1 4 5

1 4

2 3 4

3 0 3

1 2

3 0 4

1 1

Sample Output

Case 1:

5

14

1

13

2

代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; #define Lson (r<<1)
#define Rson (r<<1|1)
#define Mid e[r].mid() const int N = ; struct node
{
int L, R, sum;
int mid()
{
return (L+R)/;
}
} e[N<<]; int a[N], sum; void BuildTree(int r, int L, int R)
{
e[r].L = L , e[r].R = R; if(L==R)
{
e[r].sum = a[L];
return ;
} BuildTree(Lson, L, Mid);
BuildTree(Rson, Mid+, R); e[r].sum = e[Lson].sum + e[Rson].sum;
} void Oper(int r, int i, int w)
{
if(e[r].L == e[r].R)
{
e[r].sum = w;
return ;
}
if(i<=Mid)
Oper(Lson, i, w);
else
Oper(Rson, i, w); e[r].sum = e[Lson].sum + e[Rson].sum;
} int Query(int r, int L, int R)
{
if(e[r].L==L && e[r].R==R)
return e[r].sum; if(R<=Mid)
return Query(Lson, L, R);
else if(L>Mid)
return Query(Rson, L, R);
else
{
int LL = Query(Lson, L, Mid);
int RR = Query(Rson, Mid+, R); return LL + RR;
} } int main()
{
int t, n, m, iCase=;
scanf("%d", &t); while(t--)
{
int i, L, R, w, x; scanf("%d%d", &n, &m); for(i=; i<=n; i++)
scanf("%d", &a[i]); BuildTree(, , n); printf("Case %d:\n", iCase++); while(m--)
{
scanf("%d", &x);
if(x==)
{
scanf("%d%d", &L, &R);
sum = ;
L++, R++;
printf("%d\n", Query(, L, R));
}
else
{
scanf("%d", &i);
i++; if(x==)
{
printf("%d\n", a[i]);
a[i] = ;
}
else
{
scanf("%d", &w);
a[i] += w;
}
Oper(, i, a[i]); ///由于都是点的操作,可以直接操做后在去操作树,感觉和直接操作树是一样的,不过对于///这题来说,这种似乎更好些, 因为有加和清零的,对点的操作是不一样的, 然而区间查询就很简单了,不说了
}
} }
return ;
}

(线段树 点更新 区间求和)lightoj1112的更多相关文章

  1. POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和)

    POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和) 题意分析 卡卡屋前有一株苹果树,每年秋天,树上长了许多苹果.卡卡很喜欢苹果.树上有N个节点,卡卡给他们编号1到N,根 ...

  2. POJ.2299 Ultra-QuickSort (线段树 单点更新 区间求和 逆序对 离散化)

    POJ.2299 Ultra-QuickSort (线段树 单点更新 区间求和 逆序对 离散化) 题意分析 前置技能 线段树求逆序对 离散化 线段树求逆序对已经说过了,具体方法请看这里 离散化 有些数 ...

  3. HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对)

    HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对) 题意分析 给出n个数的序列,a1,a2,a3--an,ai∈[0,n-1],求环序列中逆序对 ...

  4. hdu 1166线段树 单点更新 区间求和

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

  5. hdu1166(线段树单点更新&区间求和模板)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1166 题意:中文题诶- 思路:线段树单点更新,区间求和模板 代码: #include <iost ...

  6. HDU 1166 敌兵布阵(线段树点更新区间求和裸题)

    Problem Description C国的死对头A国这段时间正在进行军事演习,所以C国间谍头子Derek和他手下Tidy又开始忙乎了.A国在海岸线沿直线布置了N个工兵营地,Derek和Tidy的任 ...

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

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

  8. hdu2795(线段树单点更新&区间最值)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2795 题意:有一个 h * w 的板子,要在上面贴 n 条 1 * x 的广告,在贴第 i 条广告时要 ...

  9. POJ 2892 Tunnel Warfare(线段树单点更新区间合并)

    Tunnel Warfare Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 7876   Accepted: 3259 D ...

随机推荐

  1. SQL 变量、 运算符、 if 、while

    变量: SQL语言也跟其他编程语言一样,拥有变量.分支.循环等控制语句. 在SQL语言里面把变量分为局部变量和全局变量,全局变量又称系统变量. 局部变量: 使用declare关键字给变量声明,语法非常 ...

  2. git cherry-pick用法

    场景: 如果你的应用已经发布了一个版本2.0, 代码分支叫release-2.0, 现在正在开发3.0, 代码的分支叫dev-3.0. 那么有一天产品说, 要把正在开发的某个特性提前上线, 也就是说要 ...

  3. js中的变量提升(hoisting)

    来看如下代码: function HelloJS(){ var array = [1,2,3,4,5]; for(var i in array){ } alert(i); } HelloJS(); a ...

  4. IEdevelopToolbar ie浏览器的css代码调试工具

    使用IEdevelopToolbar的“选择元素”工具(ctrl+b),选取你要内容的地方下方的DIV,我们就可以找到几个关键字

  5. 神龟快跑,2016做的一款UWP游戏

    神龟快跑,2016做的一款UWP游戏, 实际是H5页面, 用LAYA转AS3得到的 安装地址 https://www.microsoft.com/zh-cn/store/p/神龟快跑/9nblggh4 ...

  6. 【校招面试 之 C/C++】第27题 C++ 智能指针(三)之 unique_ptr

    auto_ptr<string> p1(new string ("auto") : //#1 auto_ptr<string> p2; //#2 p2 = ...

  7. java里的静态成员变量是放在了堆内存还是栈内存

    转自http://bbs.csdn.NET/topics/370001490 堆区: 1.存储的全部是对象,每个对象都包含一个与之对应的class的信息.(class的目的是得到操作指令) 2.jvm ...

  8. Spring框架之CGLIB的代理技术(代码了解)

    1.引入CBLIB的开发包 * 如果想使用CGLIB的技术来生成代理对象,那么需要引入CGLIB的开发的jar包,在Spring框架核心包中已经引入了CGLIB的开发包了.所以直接引入Spring核心 ...

  9. asp.net core web 本地iis开发

    发布后打开没有问题,直接配置到本地iis时,会提示如下错误 HTTP Error 502.5 - Process Failure

  10. PHP在win7安装Phalcon框架

    我的环境是64位的 Win7. 安装 Phalcon 也极其简单,只需要下载一个文件(php_phalcon.dll), 要以 phpinfo() 里面“Architecture”属性为准! 下载地址 ...