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. la 4490

    题解: 这道思路还是比较水的 我们可以等价变形成hi<=7 我们的最优决策是把抽出来的那些相同颜色的书最后插在一起(所以要统计序列中还有没有相同元素的书) f[i][j][k][x]表示前i本书 ...

  2. alpha冲刺6/10

    目录 摘要 团队部分 个人部分 摘要 队名:小白吃 组长博客:hjj 作业博客:感恩节~ 团队部分 后敬甲(组长) 过去两天完成了哪些任务 文字描述 设计了拍照界面和图片上传界面 沟通了前端进度 接下 ...

  3. python之PIL 二值图像处理和保存

    0. 1.参考 http://pszpcl.baike.com/article-77327.htmlwindows 图片右键:属性 详细信息 位深度位深度 用于指定图像中的每个像素可以使用的颜色信息数 ...

  4. js获取file控件的完整路径(上传图片预览)

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...

  5. 关于java中的伪共享的认识和解决

    在并发编程过程中,我们大部分的焦点都放在如何控制共享变量的访问控制上(代码层面),但是很少人会关注系统硬件及 JVM 底层相关的影响因素: CPU缓存 网页浏览器为了加快速度,会在本机存缓存以前浏览过 ...

  6. 013 mysql中find_in_set()函数的使用

    在工作中遇见过,对于新知识,在这里写一写文档. 1.作用 举个例子,也许不理解,在看完后面的SQL示例,再来看就明白了: 有个文章表里面有个type字段,它存储的是文章类型,有 1头条.2推荐.3热点 ...

  7. day43 mysql 基本管理,[破解密码以及用户权限设置]以及慢日志查询配置

    配置文件:详细步骤, 1,找到mysql的安装包,然后打开后会看到一个my.ini命名的程序,把它拖拽到notepad++里面来打开,(应该是其他文本形式也可以打开,可以试一下),直接拖拽即可打开该文 ...

  8. JavaEE 之 WebService

    1.WebService a.定义:WebService是一种跨编程语言和跨操作系统平台的远程调用技术 b.三大技术: XML+XSD,SOAP,WSDL c.SOAP协议 = HTTP协议 + XM ...

  9. Codeforces 919D Substring 【拓扑排序】+【DP】

    <题目链接> 题目大意:有一个具有n个节点,m条边的有向图,每个点对应一个小写字母,现在给出每个顶点对应的字母以及有向边的连接情况,求经过的某一条路上相同字母出现的最多次数.如果次数无限大 ...

  10. linux学习笔记 yum 在线管理软件包

    -y  如果yum在工作过程中需要使用者响应.这个参数可以直接回答yes #yum list 列出资源库中所有可安装或者可更新的rpm包 #yum perl 列出为perl的包 #yum perl* ...