NC51100 A Simple Problem with Integers
题目
题目描述
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的更多相关文章
- POJ 3468 A Simple Problem with Integers(线段树 成段增减+区间求和)
A Simple Problem with Integers [题目链接]A Simple Problem with Integers [题目类型]线段树 成段增减+区间求和 &题解: 线段树 ...
- POJ 3468 A Simple Problem with Integers(线段树/区间更新)
题目链接: 传送门 A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Description Yo ...
- poj 3468:A Simple Problem with Integers(线段树,区间修改求和)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 58269 ...
- ACM: A Simple Problem with Integers 解题报告-线段树
A Simple Problem with Integers Time Limit:5000MS Memory Limit:131072KB 64bit IO Format:%lld & %l ...
- poj3468 A Simple Problem with Integers (线段树区间最大值)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 92127 ...
- POJ3648 A Simple Problem with Integers(线段树之成段更新。入门题)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 53169 Acc ...
- 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 ...
- POJ 3468 A Simple Problem with Integers(线段树区间更新区间查询)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 92632 ...
- A Simple Problem with Integers(树状数组HDU4267)
A Simple Problem with Integers Time Limit: 5000/1500 MS (Java/Others) Memory Limit: 32768/32768 K (J ...
- A Simple Problem with Integers
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 77964 Acc ...
随机推荐
- 基于Html+腾讯云播SDK开发的m3u8播放器
周末业余时间在家无事,学习了一下腾讯的云播放sdk,并制作了一个小demo(m3u8播放器),该在线工具是基于腾讯的云播sdk开发的,云播sdk非常牛,可以支持多种播放格式. 预览地址 m3u8pla ...
- 【面试题精讲】Redis如何实现分布式锁
首发博客地址 系列文章地址 Redis 可以使用分布式锁来实现多个进程或多个线程之间的并发控制,以确保在给定时间内只有一个进程或线程可以访问临界资源.以下是一种使用 Redis 实现分布式锁的常见方法 ...
- 【转帖】ChatGPT重塑Windows!微软王炸更新:操作系统全面接入,Bing也能用插件了
https://cloud.tencent.com/developer/article/2291078?areaSource=&traceId= 金磊 丰色 西风 发自 凹非寺 量子位 | 公 ...
- JVM启动参数脚本的再学习与研究
JVM启动参数脚本的再学习与研究 摘要 学无止境 前段时间一直再研究JVM参数调优. 但是最近也在想不应该仅研究如何调优. 因为不管怎么设置, 总有猪队友会把环境搞崩. 所以应该想办法在无人值守的情况 ...
- 【scikit-learn基础】--『回归模型评估』之可视化评估
在scikit-learn中,回归模型的可视化评估是一个重要环节.它帮助我们理解模型的性能,分析模型的预测能力,以及检查模型是否存在潜在的问题.通过可视化评估,我们可以更直观地了解回归模型的效果,而不 ...
- ClickHouse(14)ClickHouse合并树MergeTree家族表引擎之VersionedCollapsingMergeTree详细解析
目录 建表语法 使用场景 合并算法 使用例子. 资料分享 参考文章 VersionedCollapsingMergeTree引擎继承自MergeTree并将折叠行的逻辑添加到合并数据部分的算法中.Ve ...
- Protocol Buffer命名空间冲突
原文在这里. 什么是Protocol Buffer命名空间冲突? 所有链接到Go二进制文件的Protocol Buffer声明都被插入到一个全局注册表中. 每个Protocol Buffer声明(例如 ...
- 基于NET Core 的Nuget包制作、发布和运用流程
开发缘由:公司需要调用天眼查-开放平台 ,验证客户的的营业执照信息是否在存续期,并将企业基本信息返回,之后和使用百度图文识别的企业信息进行对照是否一致. 前期准备 在网站中注册后,需要够买套餐.之后点 ...
- navicat连接远程docker中的mysql报错解决
总是报错 填写主机地址有错误,终于找到方法,是因为docker中的mysql没有设置ip地址,navicat不识别,奉上 https://blog.csdn.net/qq_42838723/artic ...
- 解决: DECODER_ERROR_CLASSES += (brotli.error,) ttributeError: module ‘brotli‘ has no attribute ‘error‘
解决: DECODER_ERROR_CLASSES += (brotli.error,) ttributeError: module 'brotli' has no attribute 'error' ...