(线段树 点更新 区间求和)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 ...
随机推荐
- 【342】Linear Regression by Python
Reference: 用scikit-learn和pandas学习线性回归 首先获取数据存储在 pandas.DataFrame 中,获取途径(CSV 文件.Numpy 创建) 将数据分成 X 和 y ...
- jsp+Servlet+JavaBean+JDBC+MySQL项目增删改查
1简单的Mvc,分层建包. java resources src/mian/java (1)dao 包 JDBC连接类,连接数据库.增删改查方法,其他的方法. (2)model包 实体类,数据库字段, ...
- [转载]百分之百自动登录2345王牌技术员联盟源代码(delphi)
资源地址:http://download.csdn.net/detail/softlib/9670613
- MongoDB 数据查询
数据查询 基本查询 方法find():查询 db.集合名称.find({条件文档}) 方法findOne():查询,只返回第一个 db.集合名称.findOne({条件文档}) 方法pretty(): ...
- HttpClientUtil 工具类
/* * * * FileName: s.java * * Description:TODO(用一句话描述该文件做什么) * * Created: jiangzhanghong 2017年11月14日 ...
- General error 2006 MySQL server has gone away
写入配置文件的办法: max_allowed_packet = 16M //但是这种有时候不支持,1024*1024*16这种有的也不支持 max_allowed_packet = 16777216 ...
- DOS 命令集锦——最常用命令
一. 常用命令: cd 改变当前目录 sys 制作DOS系统盘 (电脑入门到精通网 www.58116.cn) copy 拷贝文件 del 删除文件 deltree 删除目录树 dir 列 ...
- Java数据类型与MySql数据类型对照表
这篇文章主要介绍了Java数据类型与MySql数据类型对照表,以表格形式分析了java与mysql对应数据类型,并简单讲述了数据类型的选择与使用方法,需要的朋友可以参考下 本文讲述了Java数据类型与 ...
- WPF之数据触发器 改变控件背景色或闪烁
需求,很多矩形表示桶,其中:空桶=红色,满桶=绿色,使用中=红绿闪烁. <Window x:Class="FlickerDemo.MainWindow" xmlns=&quo ...
- 如何配置Notepad++的C_C++语言开发环境
相信很多人用notepad++,但把其配置成为C/C++还是需要小折腾一下的.本人在网上找了很长时间,也没有一个统一的答案,而且很多人说的方法根本不管用,而且也不够通用,所以还是自己摸索了一下,分享给 ...