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 ...
随机推荐
- 清华集训2015-Day 1
玛里苟斯 一个大小为 \(n\) 的可重集合 \(a\) ,求 \(\mathbb E[x^k]\) ,其中 \(x\) 为 \(a\) 的一个子集的异或和. \(n\le 10^5,1\le k\l ...
- 【luogu3768】简单的数学题 欧拉函数(欧拉反演)+杜教筛
题目描述 给出 $n$ 和 $p$ ,求 $(\sum\limits_{i=1}^n\sum\limits_{j=1}^nij\gcd(i,j))\mod p$ . $n\le 10^{10}$ . ...
- Codeforces914G Sum the Fibonacci(FWT)
FWT大杂烩.跟着模拟做很多次FWT即可. #include<iostream> #include<cstdio> #include<cmath> #include ...
- IIS应用程序池相关问题及连接池已满的解决方法
关于应用程序池 在 IIS 6.0 中,引入了应用程序池,应用程序池是将一个或多个应用程序链接到一个或多个工作进程集合的配置.因为应用程序池中的应用程序与其他应用程序被工作进程边界分隔 ...
- Windows 7 安装VS2008 SP1 失败
由于Windows 7自带了.NET Framework 3.5 SP1, 所以在安装VS 2008 SP1的时候会发生fatal error during installation的错误, 网上找来 ...
- 线段树区间更新 lazy
1. hdu1698 http://acm.hdu.edu.cn/showproblem.php?pid=1698 /* x y k x~y的值变为k */ #include <cstdio&g ...
- 实战:使用SVN+apache搭建一个版本控制服务器
今天讲的内容: 实战:使用SVN+apache搭建一个版本控制服务器 每天: 10:00 晚上:21:00 服务端:xuegod63.cn IP:192.168.10.63 服务概述: SVN(s ...
- Python 内置变量
Python 隐藏变量 __doc__ # 表示本文件的注释__file__ # 表示本文件的路径 __package__ # 导入的py文件所在的文件夹路径__cached__ # 导入文件的缓存_ ...
- hdu 5755 Gambler Bo (高斯消元法解同余方程组)
http://acm.hdu.edu.cn/showproblem.php?pid=5755 题意: n*m矩阵,每个格有数字0/1/2 每选择一个格子,这个格子+2,4方向相邻格子+1 如何选择格子 ...
- Mac下配置环境变量重启后不生效解决(.bash_profile vs .bashrc)(bash/zsh下不加载.bashrc问题解决)
参考上一篇文章说明:http://www.cnblogs.com/EasonJim/p/6283094.html 得知加载顺序如下: /etc/profile /etc/paths ~/.bash_p ...