(线段树 点更新 区间求和)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 ...
随机推荐
- 吴裕雄 数据挖掘与分析案例实战(13)——GBDT模型的应用
# 导入第三方包import pandas as pdimport matplotlib.pyplot as plt # 读入数据default = pd.read_excel(r'F:\\pytho ...
- 关于关闭TAB,IFRAME占用的内存不能释放问题
资料来源:http://jxd-zxf.iteye.com/blog/1440611 使用TAB时注意,如果TAB是引用IFRAME,关闭TAB时IFRAME不会被销毁从而导致内存不能释放,大量使用T ...
- 3.Longest Substring Without Repeating Characters(string; HashTable)
Given a string, find the length of the longest substring without repeating characters. For example, ...
- VMware克隆CentOS网络配置
配置网络 如果是克隆CentOS的: vi /etc/udev/rules.d/70-persistent-net.rules 注释掉网络eth0,把最后一个改为eth0,记录下mac地址. vi / ...
- day12:vcp考试
Q221. An administrator is creating a new Platform Service Controller Password Policy with the follow ...
- php中错误处理机制
php中,异常处理机制是有限的,无法自动抛出异常,必须手动进行,并且内置异常有限. php把许多异常看作错误,这样就可以把这些异常想错误一样用set_error_handler接管,进而主动抛出异常. ...
- C语言文本处理
一.conf文本 http://blog.163.com/lixiangqiu_9202/blog/static/53575037201431743236762/ http://blog.csdn.n ...
- geoserver 开发1
打开项目,会看见下面这些包(其实还有很多插件之类的包,我都删除了) 5)可以从Eclipse启动GeoServer了. 如果你已经安装了GeoServer,现在也可以打开它的登陆页面进行操作. 三 结 ...
- OSGi 系列(二)之 Hello World
OSGi 系列(二)之 Hello World 之前曾介绍过 OSGi 是什么,下面将继续上篇介绍的内容,讲述一个简单的 OSGi Bundle:Hello World 是如何开发的. 在 OSGi ...
- 用pyqt5做一个能python程序能插入图片的ide
之前只是放到github上了,现在一想应该开源,大家想继续做好这个ide的都能从这里起步. #注意在.py文件相同目录下放一个1.png做测试图片 #本质就是用html来实现图片 #写在前面的话:这个 ...