https://vjudge.net/problem/POJ-3468

线段树区间更新(lazy数组)模板题

 #include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<map>
#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1
#define INF 0x3f3f3f3f
typedef long long ll;
using namespace std;
ll sum[<<], lazy[<<];
void PushUP(int rt)
{
sum[rt] = sum[rt<<]+sum[rt<<|];
}
void PushDown(int rt, int m)
{
if(lazy[rt]){//若有标记下移
lazy[rt<<] += lazy[rt];
lazy[rt<<|] += lazy[rt];
sum[rt<<] += (m-(m>>))*lazy[rt];
sum[rt<<|] += (m>>)*lazy[rt];
lazy[rt] = ;//取消本层标记
}
}
void build(int l, int r, int rt)
{
lazy[rt] = ;//一定写在if外初始化
if(l == r){
scanf("%lld", &sum[rt]);
return ;
}
int m = (l+r)>>;
build(lson);
build(rson);
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 ans=;
if(L <= m) ans += Query(L, R, lson);
if(R > m) ans += Query(L, R, rson);
return ans;
}
void Update(int L, int R, ll add, int l, int r, int rt)
{
if(L<=l&&R>=r){
sum[rt] += (r-l+)*add;
lazy[rt] += add;
return ;
}
PushDown(rt, r-l+);//向下而更新
int m=(l+r)>>;
if(L <= m) Update(L, R, add, lson);
if(R > m) Update(L, R, add, rson);
PushUP(rt);
}
int main()
{
int n, m, a, b;
ll c;
char op[];
scanf("%d%d", &n, &m);
build(, n, );
for(int i = ; i < m; i++){
scanf("%s", &op);
if(op[] == 'Q'){
scanf("%d%d", &a, &b);
printf("%lld\n", Query(a, b, , n, ));
}
else{
scanf("%d%d%lld", &a, &b, &c);
Update(a, b, c, , n, );
}
}
return ;
}

poj3468 A Simple Problem with Integers(线段树区间更新)的更多相关文章

  1. poj 3468 A Simple Problem with Integers (线段树区间更新求和lazy思想)

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 75541   ...

  2. (简单) POJ 3468 A Simple Problem with Integers , 线段树+区间更新。

    Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. On ...

  3. A Simple Problem with Integers 线段树 区间更新 区间查询

    Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 115624   Accepted: 35897 Case Time Lim ...

  4. poj3468 A Simple Problem with Integers (线段树区间最大值)

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 92127   ...

  5. [POJ] 3468 A Simple Problem with Integers [线段树区间更新求和]

    A Simple Problem with Integers   Description You have N integers, A1, A2, ... , AN. You need to deal ...

  6. POJ 3468A Simple Problem with Integers(线段树区间更新)

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 112228 ...

  7. poj 3468 A Simple Problem with Integers 线段树区间更新

    id=3468">点击打开链接题目链接 A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072 ...

  8. POJ 3468 A Simple Problem with Integers(线段树,区间更新,区间求和)

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 67511   ...

  9. POJ 3468 A Simple Problem with Integers(线段树区间更新)

    题目地址:POJ 3468 打了个篮球回来果然神经有点冲动. . 无脑的狂交了8次WA..竟然是更新的时候把r-l写成了l-r... 这题就是区间更新裸题. 区间更新就是加一个lazy标记,延迟标记, ...

  10. POJ3468 A Simple Problem with Integers —— 线段树 区间修改

    题目链接:https://vjudge.net/problem/POJ-3468 You have N integers, A1, A2, ... , AN. You need to deal wit ...

随机推荐

  1. js条件语句初步练习

    var a=18            if(a<10){                alert("便宜")            }            else{  ...

  2. 开源工具软件XMusicDownloader——音乐下载神器

    XMusicDownloader,一款 支持从百度.网易.qq和酷狗等音乐网站搜索并下载歌曲的程序. 缘起: 一直用网易音乐听歌,但是诸如李健.周杰伦的不少歌曲,网易都没有版权,要从QQ等音乐去下载, ...

  3. 【转】ArcGIS10.0完全卸载全攻略

    ArcGIS10.0完全卸载详细步骤: 1.开始>控制面板>添加删除程序,卸载所有ArcGIS软件和帮助文档,以及所有ArcGIS补丁.2.从添加删除程序面板中删除所有Python相关的应 ...

  4. Python_lambda简单函数表达式

    lambda表达式只能用于简单函数的书写 def funx(a): a+=1 return a print(funx(99)) 用lambda实现上面函数: funx = lambda a: a+1 ...

  5. python的paramiko模块

        paramiko是用python语言写的一个模块,遵循SSH2协议,支持以加密和认证的方式,进行远程服务器的连接.paramiko支持Linux, Solaris, BSD, MacOS X, ...

  6. 了解fastadmin标准的控制器模块js的表格事件

    controller/A.php<-------------->public/assets/js/backend/a.js controller/b/A.php<---------- ...

  7. 利用shell脚本实现nginx 的logs日志分割

    Nginx 是一个非常轻量的 Web 服务器,体积小.性能高.速度快等诸多优点.但不足的是也存在缺点,比如其产生的访问日志文件一直就是一个,不会自动地进行切割,如果访问量很大的话,将 导致日志文件容量 ...

  8. webpack+vue+es6+postcss

    本篇文章仿照vue-cli生成的项目结构,自己重新搭建目录,写一个小demo进一步巩固单文件组件的知识,并且学习中间件.一.要解决的问题1.如果把webpack.config.js放进build文件夹 ...

  9. [OC] Block的使用

    由ControllerA跳转到controllerB,在controllerB中选择一个参数的值,并将它传回给controllerA. 首先,在controllerB的.h文件中写入: @interf ...

  10. RFID的winform程序心得1

    /// 根据日期获取当周星期一 /// </summary> /// <param name="dt"></param> /// <ret ...