链接:

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

转载于:https://www.cnblogs.com/YY56/p/4738471.html

(线段树 点更新 区间求和)lightoj1112相关推荐

  1. HDU 1166 敌兵布阵(线段树:点更新,区间求和)

    HDU 1166 敌兵布阵(线段树:点更新,区间求和) http://acm.hdu.edu.cn/showproblem.php?pid=1166 题意: 给你n个整数,然后给你多条命令,每条命令如 ...

  2. hdu4417:线段树单点更新区间求和,离线 Super Mario

    Description Mario is world-famous plumber. His "burly" figure and amazing jumping ability ...

  3. hdu 1540(线段树单点更新 区间合并)

    解题思路:这一题要求的是连续区间,所以可以把它的子区间合并,这里运用线段树,但是在保存节点信息的方面要做一点修改 lsum:从这个区间的左端点往右能够找到的最大连续区间: rsum:从这个区间的右端点 ...

  4. 树链剖分+线段树 单点修改 区间求和 模板

    马上要去西安打邀请赛了,存下板子 首先是vector存图的: #include<bits/stdc++.h> using namespace std; #define ll long lo ...

  5. A - 卿学姐与公主(线段树+单点更新+区间极值)

    A - 卿学姐与公主 Time Limit: 2000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Submi ...

  6. 线段树2 求区间最小值

    线段树2 求区间最小值 递归,DFS,尤其是二叉树,我只要知道我的返回节点就好,因为DFS的顺序一定是一样的,不同的题目和数据范围也是一样的,只是返回节点让DFS的深度不同. 递归的内容只有两部分:1 ...

  7. NYOJ 1068 ST(线段树之 成段更新+区间求和)

    ST 时间限制:1000 ms  |  内存限制:65535 KB 难度:1 描述 "麻雀"lengdan用随机数生成了后台数据,但是笨笨的他被妹纸的问题给难住了... 已知len ...

  8. 数据结构之线段树进阶(区间更新lazy标记)

    之前说了线段树的点更新和区间求和.其实点更新是区间更新的一种最基础的做法.我们把一个点想像成一个区间的话,不就是最简单的区间更新了嘛. 为什么要把区间更新和点更新分开来看呢?假如我们对区间[l,r]进 ...

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

    题目链接 Description During the War of Resistance Against Japan, tunnel warfare was carried out extensiv ...

最新文章

  1. 2021年大数据Flink(一):乘风破浪的Flink-Flink概述
  2. mysql 负载 查看_Mysql-命令查询当前正在负载运行的SQL语句
  3. ansible的条件判断、迭代执行、tags
  4. python简单爬虫入门一_Python简单爬虫入门二
  5. 如何在一台电脑上同时启动多个tomcat,及如何解决tomcat一闪而过
  6. 《树莓派Python编程入门与实战(第2版)》——1.7 排除树莓派的故障
  7. 认认真真推荐几个机器学习和Python类的公众号
  8. Push代码:Git@github.com: Permission denied (publickey)
  9. JavaScript onerror 事件( window.onerror = )
  10. Java 中的日期与时间
  11. mysql not in优化_98%的人不知道的MySQL优化器原理
  12. python压缩算法_用python实现LZ78压缩算法
  13. [poj1410]Intersection
  14. Java课堂作业-------参数求和
  15. Boostrap Table学习笔记
  16. Tomcat安装步骤及详细配置教程(2022最新版)
  17. 网络模型早停earlystopping详解
  18. 奥地利邮政服务推出加密收藏邮票
  19. VB6.0 Select Case语句
  20. 某教程学习笔记(一):1、windows基础

热门文章

  1. Centos7换yum源
  2. 表字段注释放在哪_橱柜台面6种材质优缺点大解剖,我来测评,告诉你哪一种适合你!...
  3. vue css自定义标签,Vue如何使用CSS自定义变量
  4. win10 linux安卓模拟器,genymotion安卓模拟器在Window10中使用的问题
  5. linux变量赋值取值,linuxshell编程对变量的赋值
  6. sql server查询历史进程_学习笔记 | SequoiaDB SQL查询语句执行过程
  7. c语言编写一个函数判断闰年,C语言:实现一个函数判断year是不是闰年
  8. Spring+Hibernate+SpringMVC+MySql实现配置多个数据源!
  9. 四川省中职计算机考试题,四川省计算机等级考试模拟试题(一级)
  10. opencv计算图像亮度调节_OpenCV教程创建Trackbar图像对比度、亮度值调整