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. 【bzoj4811】[Ynoi2017]由乃的OJ 树链剖分+线段树区间合并

    题解: 好像和noi那题并没有什么区别 只是加上了修改和变成树上 比较显然我们可以用树链剖分来维护

  2. Js计算时间差,天数,小时数,余数

    var begintime_ms = Date.parse(new Date(begintime.replace(/-/g, "/"))); //begintime 为开始时间 v ...

  3. 伪分布式hadoop1.1.2和hbase0.94.11配置

    Hadoop 1.1.2 和Hbase 0.94.11版本配置 测试时ip  172.19.32.128 这个版本需要把/etc/hosts的aa-vm改成127.0.0.1,也就是和localhos ...

  4. React 入门实例教程(转载)

    现在最热门的前端框架,毫无疑问是 React . 上周,基于 React 的 React Native 发布,结果一天之内,就获得了 5000 颗星,受瞩目程度可见一斑. React 起源于 Face ...

  5. String.getBytes()和String.tocharArray(),字节数组和字符数组的区别

    String.getBytes()是将字符串转化为一个字节数组.而String.toCharArray()是将一个字符串转化为一个字符数组. [例如] byte bys[] ="国庆60周年 ...

  6. Python交互图表可视化Bokeh:6. 轴线| 浮动| 多图表

    绘图表达进阶操作 ① 轴线设置② 浮动设置③ 多图表设置 1. 轴线标签设置 设置字符串 import numpy as np import pandas as pd import matplotli ...

  7. Kafka生产者案例报警告SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".

    一.SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder". 这个报警告的原因简单来说时因为slf4j的版本 ...

  8. redis+mysql

    redis和mysql要根据具体业务场景去选型 mysql:数据放在磁盘   redis:数据放在内存 redis适合放一些频繁使用,比较热的数据,因为是放在内存中,读写速度都非常快,一般会应用在下面 ...

  9. 自己总结的C#编码规范--3.特定场景下的命名最佳实践

    特定场景下的命名最佳实践 命名空间 要使用PascalCasing,并用点号来分隔名字空间中的各个部分. 如Microsof.Office.PowerPoint 要用公司名作为命名空间的前缀,这样就可 ...

  10. python 列表常用操作(二)

    1.tuple 的 unpack a,b = t 2.格式化输出 print('您的输入:{},值为{}',format(a,b)) 3.日期计算 import datetime as dt impo ...