题目链接

题目

题目描述

You have N integers, \(A_1, A_2, ... , A_N\) .You need to deal with two kinds of operations. One type of operation is to add some given number to each number in a given interval. The other is to ask for the sum of numbers in a given interval.

输入描述

The first line contains two numbers N and Q. \(1 \leq N,Q \leq 100000\) .

The second line contains N numbers, the initial values of \(A_1, A_2, ... , A_N\) \(-1000000000 \leq A_i \leq 1000000000\) .

Each of the next Q lines represents an operation.

"C a b c" means adding c to each of \(A_a, A_{a+1}, ... , A_b\) , \(10000 \leq c \leq 10000\) .

"Q a b" means querying the sum of \(A_a, A_{a+1}, ... , A_b\) .

输出描述

You need to answer all Q commands in order. One answer in a line.

示例1

输入

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

输出

4
55
9
15

备注

The sums may exceed the range of 32-bit integers.

题解

知识点:线段树。

线段树懒标记实现区间修改的板子题。

时间复杂度 \(O((n+q)\log n)\)

空间复杂度 \(O(n)\)

代码

#include <bits/stdc++.h>
using namespace std;
using ll = long long; struct T {
int len;
ll sum;
static T e() { return { 0,0 }; }
friend T operator+(const T &a, const T &b) { return { a.len + b.len, a.sum + b.sum }; }
};
struct F {
ll add;
static F e() { return { 0 }; }
T operator()(const T &x) { return { x.len,x.sum + add * x.len }; }
F operator()(const F &g) { return { g.add + add }; }
};
template<class T, class F>
class SegmentTreeLazy {
int n;
vector<T> node;
vector<F> lazy; void push_down(int rt) {
node[rt << 1] = lazy[rt](node[rt << 1]);
lazy[rt << 1] = lazy[rt](lazy[rt << 1]);
node[rt << 1 | 1] = lazy[rt](node[rt << 1 | 1]);
lazy[rt << 1 | 1] = lazy[rt](lazy[rt << 1 | 1]);
lazy[rt] = F::e();
} void update(int rt, int l, int r, int x, int y, F f) {
if (r < x || y < l) return;
if (x <= l && r <= y) return node[rt] = f(node[rt]), lazy[rt] = f(lazy[rt]), void();
push_down(rt);
int mid = l + r >> 1;
update(rt << 1, l, mid, x, y, f);
update(rt << 1 | 1, mid + 1, r, x, y, f);
node[rt] = node[rt << 1] + node[rt << 1 | 1];
} T query(int rt, int l, int r, int x, int y) {
if (r < x || y < l)return T::e();
if (x <= l && r <= y) return node[rt];
push_down(rt);
int mid = l + r >> 1;
return query(rt << 1, l, mid, x, y) + query(rt << 1 | 1, mid + 1, r, x, y);
} public:
SegmentTreeLazy(int _n = 0) { init(_n); }
SegmentTreeLazy(const vector<T> &src) { init(src); } void init(int _n) {
n = _n;
node.assign(n << 2, T::e());
lazy.assign(n << 2, F::e());
}
void init(const vector<T> &src) {
assert(src.size());
init(src.size() - 1);
function<void(int, int, int)> build = [&](int rt, int l, int r) {
if (l == r) return node[rt] = src[l], void();
int mid = l + r >> 1;
build(rt << 1, l, mid);
build(rt << 1 | 1, mid + 1, r);
node[rt] = node[rt << 1] + node[rt << 1 | 1];
};
build(1, 1, n);
} void update(int x, int y, F f) { update(1, 1, n, x, y, f); } T query(int x, int y) { return query(1, 1, n, x, y); }
}; int main() {
std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int n, q;
cin >> n >> q;
vector<T> a(n + 1);
for (int i = 1;i <= n;i++) {
int x;
cin >> x;
a[i] = { 1,x };
}
SegmentTreeLazy<T, F> sgt(a);
while (q--) {
char op;
int a, b;
cin >> op >> a >> b;
if (op == 'C') {
int c;
cin >> c;
sgt.update(a, b, { c });
}
else cout << sgt.query(a, b).sum << '\n';
}
return 0;
}

NC51100 A Simple Problem with Integers的更多相关文章

  1. POJ 3468 A Simple Problem with Integers(线段树 成段增减+区间求和)

    A Simple Problem with Integers [题目链接]A Simple Problem with Integers [题目类型]线段树 成段增减+区间求和 &题解: 线段树 ...

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

    题目链接: 传送门 A Simple Problem with Integers Time Limit: 5000MS     Memory Limit: 131072K Description Yo ...

  3. poj 3468:A Simple Problem with Integers(线段树,区间修改求和)

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

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

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

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

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

  6. POJ3648 A Simple Problem with Integers(线段树之成段更新。入门题)

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

  7. BZOJ-3212 Pku3468 A Simple Problem with Integers 裸线段树区间维护查询

    3212: Pku3468 A Simple Problem with Integers Time Limit: 1 Sec Memory Limit: 128 MB Submit: 1278 Sol ...

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

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

  9. A Simple Problem with Integers(树状数组HDU4267)

    A Simple Problem with Integers Time Limit: 5000/1500 MS (Java/Others) Memory Limit: 32768/32768 K (J ...

  10. A Simple Problem with Integers

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

随机推荐

  1. 理解 Kubernetes volume 和 共享存储

    1. Kubernetes volume 文章 介绍了 Docker volume.与 docker volume 类似的,在 kubernetes 中存在 Pod 级别的 volume,Pod 的 ...

  2. 简单剖析Hashmap

    剖析 Java Hashmap 源码 在 Java 的集合框架中,HashMap 是一颗璀璨的明珠.通过深入挖掘其源码,我们将揭开 HashMap 的神秘面纱,理解其底层原理.扩容机制和数据结构. 1 ...

  3. Feign源码解析7:nacos loadbalancer不支持静态ip的负载均衡

    背景 在feign中,一般是通过eureka.nacos等获取服务实例,但有时候调用一些服务时,人家给的是ip或域名,我们这时候还能用Feign这一套吗? 可以的. 有两种方式,一种是直接指定url: ...

  4. AI正在改变人类社会 - 内容行业的衰落

    现在的 AI 技术,每天都在进化.我有一种感觉,普通人大概没意识到,它马上就要改变人类社会了. 历史上,这种事一再发生.在你不知不觉中,某些大事件悄悄酝酿,突然就冲击到了你的生活,将你的人生全部打乱, ...

  5. 使用Amber计算单点能三步走

    技术背景 Amber是一个在分子动力学中非常常用的一个软件,可以用于进行分子动力学模拟计算,可以与一些软件配合进行增强采样.这里我们简单介绍一下如何使用Amber去计算一个分子构象的单点势能值,及其对 ...

  6. [转帖]shell 使用sed或awk将文本中的上下两行合并为一行

    例如要装下面文本上下两行合并为一行 文件test内容: # cat test a1 ce ef 12 45 57 efef 5656 gfg 455 上下两行合并为一行: # sed -n '{N;s ...

  7. [转帖]一个轻量的Linux运维监控脚本

    https://zhuanlan.zhihu.com/p/472040635 写在前面 我的需求 嗯,有几台很老的机器,上面部署的几个很老的应用 我需要每周对机器上的一些内存,磁盘,线程,应用和数据库 ...

  8. [转帖]Cgroups资源限制

    https://cloud.tencent.com/developer/article/2108816?areaSource=105001.13&traceId=QzVtWN5jGl8zeYZ ...

  9. [转帖]【JVM】类加载机制

    什么是类的加载 将类的.class文件中的二进制数据读入到内存中,将其放在运行时数据区的方法区内,然后在堆区创建一个java.lang.Class对象,用来封装类在方法区内的数据结构.类的加载的最终产 ...

  10. XJTU少年班+自动化钱学森班+电气工程辅修专业课笔记合集

    通过百度网盘分享的文件:笔记整理链接:https://pan.baidu.com/s/1BrHQ1EqvlQlbWqpD5h_6Sg?pwd=shsg 提取码:shsg复制这段内容打开「百度网盘APP ...