(线段树 点更新 区间求和)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 ...
随机推荐
- Tomcat SSL配置及Tomcat CA证书安装
Tomcat既可以作为独立的Servlet容器,也可以作为其他HTTP服务器附加的Servlet容器.如果Tomcat在非独立模式下工作, 通常不必配置SSL,由它从属的HTTP服务器来实现和客户的S ...
- hibernate的异常 Session was already closed
今天写hibernate时候遇到一些异常 代码: Session session = sessionFactory.getCurrentSession(); session.beginTransact ...
- 趣味编程:CPS风格代码(C++11, C++14版)
CPS风格代码(C++11版) #include <iostream> using namespace std; int add(int x, int y){return x + y;} ...
- cookie保存用户名及密码
登陆页中,用户输入用户名密码,点击提交,后台对照mysq数据库中,看是否有对应的用户名,以及密码是否正确.如果正确 则将用户名密码分两份Cookie保存.页面跳转到登陆成功页. 用户再次访问登陆页时, ...
- fm 讲解加代码
转自: 博客 http://blog.csdn.net/google19890102/article/details/45532745/ github https://github.com/zhaoz ...
- css样式占位和不占位隐藏元素的方法
不占位隐藏:display:none; 占位隐藏:visibility:hidden;
- 实例学习SSIS(一)
网址: http://www.cnblogs.com/tenghoo/archive/2009/10/archive/2009/10/archive/2009/10/archive/2009/10/a ...
- Partition List双色问题链表版
[抄题]: Given a linked list and a value x, partition it such that all nodes less than x come before no ...
- 使用maven管理引入jdk1.8
需要在配置文件settings.xml中加入: <profile> <id>jdk-1.8</id> <activation> <activeBy ...
- Java中的NIO及IO
1.概述 Java NIO(New IO) 是从Java 1.4版本开始引入的一个新的IO API,可以替代标准的Java IO API.NIO与原来的IO有同样的作用和目的,但是使用的方式完全不同, ...