poj 3468 线段树 成段增减 区间求和
题意:Q是询问区间和,C是在区间内每个节点加上一个值
Sample Input
10 5
1 2 3 4 5 6 7 8 9 10
Q 4 4
Q 1 10
Q 2 4
C 3 6 3
Q 2 4
Sample Output
4
55
9
15
# include <iostream>
# include <cstdio>
# include <cstring>
# include <algorithm>
# include <cmath>
# include <queue>
# define LL long long
using namespace std ; const int maxn = ; LL sum[maxn<<] ; //结点开4倍
LL add[maxn<<] ; //延迟标记 void PushUP(int rt) //更新到父节点
{
sum[rt] = sum[rt * ] + sum[rt * + ] ; //rt 为当前结点
} void PushDown(int rt , int m ) //向下更新
{
if (add[rt])
{
add[rt * ] += add[rt] ;
add[rt * + ] += add[rt] ;
sum[rt * ] += (m - m / ) * add[rt] ;
sum[rt * + ] += (m / ) * add[rt] ;
add[rt] = ;
}
} void build(int l , int r , int rt) //构建线段树
{
add[rt] = ;
if (l == r)
{
cin>>sum[rt];
return ;
}
int m = (l + r) / ;
build(l , m , rt * ) ;
build(m + , r , rt * +) ;
PushUP(rt) ;
} void updata(int L , int R , int c , int l , int r , int rt) //成段增减
{
if (L <= l && r <= R)
{
add[rt] += c ;
sum[rt] += (r - l + ) * c ;
return ;
}
PushDown(rt , r - l + ) ;
int m = (l + r) / ; if (L <= m)
updata(L , R , c , l , m , rt * ) ;
if (R > m)
updata(L , R , c , m + , r , rt * + ) ;
PushUP(rt) ;
} LL query(int L , int R , int l , int r , int rt) //区间求和
{
if (L <= l && r <= R)
return sum[rt] ;
PushDown(rt , r - l + ) ;
int m = (l + r) / ;
LL ret = ;
if (L <= m)
ret += query(L , R , l , m , rt * ) ;
if (R > m)
ret += query(L , R , m + , r , rt * + ) ;
return ret ;
} int main ()
{
//freopen("in.txt","r",stdin) ;
int n , m ;
scanf("%d %d" , &n , &m) ; build( , n , ) ;
while(m--)
{
char op[] ;
int a , b , c ;
scanf("%s" , op) ;
if(op[] == 'Q')
{
scanf("%d %d" , &a , &b) ;
cout<<query(a , b , , n , )<<endl ;
}
else
{
scanf("%d %d %d" , &a , &b , &c) ;
updata(a , b , c , , n , ) ;
}
} return ;
}
poj 3468 线段树 成段增减 区间求和的更多相关文章
- POJ 3468 A Simple Problem with Integers(线段树 成段增减+区间求和)
A Simple Problem with Integers [题目链接]A Simple Problem with Integers [题目类型]线段树 成段增减+区间求和 &题解: 线段树 ...
- POJ 3468 线段树 成段更新 懒惰标记
A Simple Problem with Integers Time Limit:5000MS Memory Limit:131072K Case Time Limit:2000MS Descr ...
- poj 3669 线段树成段更新+区间合并
添加 lsum[ ] , rsum[ ] , msum[ ] 来记录从左到右的区间,从右到左的区间和最大的区间: #include<stdio.h> #define lson l,m,rt ...
- poj 3468 线段树成段更新
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 54012 ...
- HDU 3577 Fast Arrangement ( 线段树 成段更新 区间最值 区间最大覆盖次数 )
线段树成段更新+区间最值. 注意某人的乘车区间是[a, b-1],因为他在b站就下车了. #include <cstdio> #include <cstring> #inclu ...
- poj 3648 线段树成段更新
线段树成段更新需要用到延迟标记(或者说懒惰标记),简单来说就是每次更新的时候不要更新到底,用延迟标记使得更新延迟到下次需要更新or询问到的时候.延迟标记的意思是:这个区间的左右儿子都需要被更新,但是当 ...
- 线段树---poj3468 A Simple Problem with Integers:成段增减:区间求和
poj3468 A Simple Problem with Integers 题意:O(-1) 思路:O(-1) 线段树功能:update:成段增减 query:区间求和 Sample Input 1 ...
- POJ训练计划2777_Count Color(线段树/成段更新/区间染色)
解题报告 题意: 对线段染色.询问线段区间的颜色种数. 思路: 本来直接在线段树上染色,lz标记颜色.每次查询的话訪问线段树,求出颜色种数.结果超时了,最坏的情况下,染色能够染到叶子节点. 换成存下区 ...
- C - A Simple Problem with Integers POJ - 3468 线段树模版(区间查询区间修改)
参考qsc大佬的视频 太强惹 先膜一下 视频在b站 直接搜线段树即可 #include<cstdio> using namespace std; ; int n,a[maxn]; stru ...
随机推荐
- SSM 中 BaseController 使用 session
转载: http://blog.sina.com.cn/s/blog_7085382f0102v9jg.html public class BaseController { //region Http ...
- SpringBoot设置事务隔离等级
"If you're gonna play the game, boy, ya gotta learn to play it right" Spring Boot 使用事务非常简单 ...
- python编码与存储读取数据(数组字典)
Camp时在python2的编码上坑了不少. 理解pyhon2的编码 python2字符串类型只有两种: str类型:b'xxx'即是str类型, 是编码后的类型,len()按字节计算 unicode ...
- Spring点滴九:Spring bean的延迟初始化
Spring bean延迟初始化: 官网API: By default, ApplicationContext implementations eagerly create and configure ...
- IntelliJ IDEA远程调试运行中的JAVA程序/项目
一.IntelliJ IDEA配置 1.添加一个运行配置(remote项) 2.打开remote项配置对话框 3.远程jvm参数配置提示 4.远程调试的ip地址和端口号,ip就是java项目所在机器i ...
- 关于NIO一些优化
1. 使用NIO开发web服务,传输文件内容,可以使用FileChannel.transferTo(position,count,socketChannel)来提升性能: 经过测试,确实能提升10% ...
- springcloud与dubbo对比:
我们直接将结论先列出来,然后逐个分析: 本博客借鉴此文章:http://blog.csdn.net/shuijieshuijie/article/details/53133082 打个不恰当的比喻: ...
- C++ new动态数组初始化
strlen函数是不包括‘\0’的长度的,sizeof计算的结果才包括'\0'的长度: C++ new动态数组初始化void testnew( const char* str ) { if (!str ...
- P2513 [HAOI2009]逆序对数列
P2513 [HAOI2009]逆序对数列 题目描述 对于一个数列{ai},如果有iaj,那么我们称ai与aj为一对逆序对数.若对于任意一个由1~n自然数组成的数列,可以很容易求出有多少个逆序对数.那 ...
- P2073 送花
P2073 送花 题目背景 小明准备给小红送一束花,以表达他对小红的爱意.他在花店看中了一些花,准备用它们包成花束. 题目描述 这些花都很漂亮,每朵花有一个美丽值W,价格为C. 小明一开始有一个空的花 ...