(线段树 点更新 区间求和)lightoj1112
链接:
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的更多相关文章
- POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和)
POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和) 题意分析 卡卡屋前有一株苹果树,每年秋天,树上长了许多苹果.卡卡很喜欢苹果.树上有N个节点,卡卡给他们编号1到N,根 ...
- POJ.2299 Ultra-QuickSort (线段树 单点更新 区间求和 逆序对 离散化)
POJ.2299 Ultra-QuickSort (线段树 单点更新 区间求和 逆序对 离散化) 题意分析 前置技能 线段树求逆序对 离散化 线段树求逆序对已经说过了,具体方法请看这里 离散化 有些数 ...
- HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对)
HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对) 题意分析 给出n个数的序列,a1,a2,a3--an,ai∈[0,n-1],求环序列中逆序对 ...
- hdu 1166线段树 单点更新 区间求和
敌兵布阵 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- hdu1166(线段树单点更新&区间求和模板)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1166 题意:中文题诶- 思路:线段树单点更新,区间求和模板 代码: #include <iost ...
- HDU 1166 敌兵布阵(线段树点更新区间求和裸题)
Problem Description C国的死对头A国这段时间正在进行军事演习,所以C国间谍头子Derek和他手下Tidy又开始忙乎了.A国在海岸线沿直线布置了N个工兵营地,Derek和Tidy的任 ...
- hdu1394(枚举/树状数组/线段树单点更新&区间求和)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 题意:给出一个循环数组,求其逆序对最少为多少: 思路:对于逆序对: 交换两个相邻数,逆序数 +1 ...
- hdu2795(线段树单点更新&区间最值)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2795 题意:有一个 h * w 的板子,要在上面贴 n 条 1 * x 的广告,在贴第 i 条广告时要 ...
- POJ 2892 Tunnel Warfare(线段树单点更新区间合并)
Tunnel Warfare Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 7876 Accepted: 3259 D ...
随机推荐
- 【344】Jupyter relevant problems
参考:Jupyter Notebook Tutorial: The Definitive Guide 参考:ipython notebook 如何修改一开始打开的文件夹路径? Ref: Install ...
- docker registry ui
https://hub.docker.com/r/parabuzzle/docker-registry-ui/
- print 不换行
[print 不换行] 参考:http://zhidao.baidu.com/link?url=-qC2RyT5_GWzW_N-SyqJYgegVt2sSXwmMWGvHfk_4MjErhm_Pj23 ...
- 关于Application.DoEvents()==转
记得第一次使用Application.DoEvents()是为了在加载大量数据时能够有一个数据加载的提示,不至于系统出现假死的现象,当时也没有深入的去研究他的原理是怎样的,结果在很多地方都用上了App ...
- Linux配置Hadoop 常用的命令
uname -a 看主机位数 ip a 看IP地址 vi /etc/sysconfig/network 改主机的名字 vi /etc/hosts 改映射关系 vi /etc/sysconfig/net ...
- 84. Largest Rectangle in Histogram (Array, Stack; DP)
Given n non-negative integers representing the histogram's bar height where the width of each bar is ...
- python之类之多继承
class A(object): def test_func(self): print("from A") class B(A): pass # def test_func(sel ...
- 【原创】Junit4详解二:Junit4 Runner以及test case执行顺序和源代码理解
概要: 前一篇文章我们总体介绍了Junit4的用法以及一些简单的测试.之前我有个疑惑,Junit4怎么把一个test case跑起来的,在test case之前和之后我们能做些什么? Junit4执行 ...
- .net调用web邮箱发送邮件(转载)
public static void SendEmail() { System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient( ...
- Easyui form 处理 Laravel 返回的 Json 数据
默认地,Easyui Form 请求的格式是 Html/Text,如果服务端 Laravel 返回的数据是 Json 格式,则应当在客户端进行解析.以下是 Easyui 官方文档的说明: Handle ...