//add,懒标记,给以当前节点为根的子树中的每一个点加上add(不包含根节点)
//
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;
const int N = ;
int n, m;
int w[N];
struct Node
{
int l, r;
//总和
//如果只考虑当前节点及子节点上的标记,当前区间和是多少 ,没考虑所有祖先节点上的标记
LL sum;
//懒标记
//给当前区间的所有儿子加上add
LL add;
}tr[N * ];
//用子节点的信息来计算父节点的信息
void pushup(int u)
{
tr[u].sum = tr[u << ].sum + tr[u << | ].sum;
}
void pushdown(int u)
{
Node &root = tr[u], &left = tr[u << ], &right = tr[u << | ];
//如果当前根节点有标记,往下传,还要清空
if (root.add)
{
left.add += root.add;
left.sum += (LL)(left.r - left.l + ) * root.add;
right.add += root.add;
right.sum += (LL)(right.r - right.l + ) * root.add;
root.add = ;
}
}
void build(int u, int l, int r)
{
if (l == r)
tr[u] = {l, r, w[r], };
else
{
tr[u] = {l, r};
int mid = l + r >> ;
build(u << , l, mid);
build(u << | , mid + , r);
pushup(u);
}
}
void modify(int u, int l, int r, int d)
{
if (tr[u].l >= l && tr[u].r <= r)
{
//总和
tr[u].sum += (LL)(tr[u].r - tr[u].l + ) * d;
//懒标记
tr[u].add += d;
}
// 区间太大,一定要分裂
else
{
pushdown(u);
int mid = tr[u].l + tr[u].r >> ;
if (l <= mid)
modify(u << , l, r, d);
if (r > mid)
modify(u << | , l, r, d);
//当前区间和发生变化,需要向上传
pushup(u);
}
}
LL query(int u, int l, int r)
{
if (tr[u].l >= l && tr[u].r <= r)
return tr[u].sum;
//查询子区间
pushdown(u);
int mid = tr[u].l + tr[u].r >> ;
LL sum = ;
if (l <= mid)
sum = query(u << , l, r);
if (r > mid)
sum += query(u << | , l, r);
return sum;
}
int main()
{
scanf("%d%d", &n, &m);
for (int i = ; i <= n; i ++ )
scanf("%d", &w[i]);
build(, , n);
char op[];
int l, r, d;
while (m -- )
{
scanf("%s%d%d", op, &l, &r);
if (*op == 'C')
{
scanf("%d", &d);
modify(, l, r, d);
}
else printf("%lld\n", query(, l, r));
}
return ;
}

A Simple Problem with Integers POJ - 3468 线段树区间修改+区间查询的更多相关文章

  1. C - A Simple Problem with Integers POJ - 3468 线段树模版(区间查询区间修改)

    参考qsc大佬的视频 太强惹 先膜一下 视频在b站 直接搜线段树即可 #include<cstdio> using namespace std; ; int n,a[maxn]; stru ...

  2. A Simple Problem with Integers POJ - 3468 (线段树)

    思路:线段树,区间更新,区间查找 #include<iostream> #include<vector> #include<string> #include< ...

  3. A Simple Problem with Integers poj 3468 多树状数组解决区间修改问题。

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

  4. POJ 3468 线段树区间修改查询(Java,c++实现)

    POJ 3468 (Java,c++实现) Java import java.io.*; import java.util.*; public class Main { static int n, m ...

  5. POJ - 3468 线段树区间修改,区间求和

    由于是区间求和,因此我们在更新某个节点的时候,需要往上更新节点信息,也就有了tree[root].val=tree[L(root)].val+tree[R(root)].val; 但是我们为了把懒标记 ...

  6. poj 2528 线段树区间修改+离散化

    Mayor's posters POJ 2528 传送门 线段树区间修改加离散化 #include <cstdio> #include <iostream> #include ...

  7. ACM: A Simple Problem with Integers 解题报告-线段树

    A Simple Problem with Integers Time Limit:5000MS Memory Limit:131072KB 64bit IO Format:%lld & %l ...

  8. [线段树]区间修改&区间查询问题

    区间修改&区间查询问题 [引言]信息学奥赛中常见有区间操作问题,这种类型的题目一般数据规模极大,无法用简单的模拟通过,因此本篇论文将讨论关于可以实现区间修改和区间查询的一部分算法的优越与否. ...

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

    这题WA了好久,一直以为是lld和I64d的问题,后来发现是自己的pushdown函数写错了,说到底还是因为自己对线段树理解得不好. 因为是懒惰标记,所以只有在区间分开的时候才会将标记往下传递.更新和 ...

随机推荐

  1. qt creator源码全方面分析(2-10-4)

    目录 Plugin Life Cycle Plugin Life Cycle 为了能够编写Qt Creator插件,您必须了解启动或关闭Qt Creator时,插件管理器所采取的步骤. 本节详细描述插 ...

  2. .gitignore配置规则

    1.gitignore文件 在git中如果想忽略掉某个文件,不让这个文件提交到版本库中,可以使用修改 .gitignore 文件的方法.这个文件每一行保存了一个匹配的规则例如: # 此为注释 – 将被 ...

  3. 【转】netty-transport版本冲突

    Springboot整合Elasticsearch报错 今天使用SpringBoot整合Elasticsearch时候,相关的配置完成后,启动项目就报错了. nested exception is j ...

  4. git的使用方法大全

    换了一个新的工作环境,由于以前都是使用SVN管理代码,现在要换成git,但我对git了解不多,只好下功夫咯!脑子不灵活,命令语句容易忘,所以做个笔记记录下~~~1.安装git到Git官网下载合适自己电 ...

  5. PWN之Canary学习

    Canary 参考链接:https://ctf-wiki.github.io/ctf-wiki/pwn/linux/mitigation/canary-zh/ 0x1 简介: 用于防止栈溢出被利用的一 ...

  6. 如何实现广告响应式滚动 OwlCarousel2

    githu    https://github.com/OwlCarousel2/OwlCarousel2 OwlCarousel2 官方网址    http://owlcarousel2.githu ...

  7. kettle:The tablename is not defined (empty)

    报错误The tablename is not defined (empty) 去掉表输出中的"表分区数据"

  8. Nginx 和Apache 中的虚拟主机的概念

    在部署环境的时候,有时候会引用到虚拟主机的概念,什么是虚拟主机呢,博主之前一直把虚拟主机的概念没搞清楚,导致在部署的时候,一直动不动就404 ,或者500,或者服务器不通 所以,什么是虚拟主机呢? 虚 ...

  9. 多字节与Unicode

    编码知识 一.Unicode与多字节(ANSI ) (1)Windows中,Unicode也称为宽字节,多字节也称为窄字节; VS中默认使用Unicode编码,在项目属性>>配置属性> ...

  10. #6029. 「雅礼集训 2017 Day1」市场 [线段树]

    考虑到每次除法,然后加法,差距会变小,于是维护加法lazytag即可 #include <cstdio> #include <cmath> #define int long l ...