NC50454 A Simple Problem with Integers
题目
题目描述
给定数列 \(a[1],a[2], \dots,a[n]\) ,你需要依次进行q个操作,操作有两类:
1 l r x:给定l,r,x,对于所有 \(i \in[l,r]\) ,将a[i]加上x(换言之,将 \(a[l],a[l+1], \dots,a[r]\) 分别加上x);
2 l r:给定l,r,求 \(\sum_{i=l}^ra[i]\) 的值(换言之,求 \(a[l]+a[l+1]+ \dots+a[r]\) 的值)。
输入描述
第一行包含2个正整数n,q,表示数列长度和询问个数。保证 \(1 \le n,q \le10^6\) 。
第二行n个整数 \(a[1],a[2], \dots,a[n]\) ,表示初始数列。保证 \(|a[i]| \le10^6\) 。
接下来q行,每行一个操作,为以下两种之一:
1 l r x:对于所有 \(i \in[l,r]\) ,将a[i]加上x;
2 l r:输出 \(\sum_{i=l}^ra[i]\) 的值。保证 \(1 \le l \le r \le n\) , \(|x| \le10^6\) 。
输出描述
对于每个2lr操作,输出一行,每行有一个整数,表示所求的结果。
示例1
输入
5 10
2 6 6 1 1
2 1 4
1 2 5 10
2 1 3
2 2 3
1 2 2 8
1 2 3 7
1 4 4 10
2 1 2
1 4 5 6
2 3 4
输出
15
34
32
33
50
备注
对于所有数据,\(1 \le n,q \le10^6\) , \(|a[i]| \le10^6\) , \(1 \le l \le r \le n\) , \(|x| \le10^6\) 。
题解
方法一
知识点:线段树。
可以用线段树,不过因为线段树空间常数是四倍,容易炸空间,但还是有概率能过。
时间复杂度 \(O((n+q)\log n)\)
空间复杂度 \(O(n)\)
方法二
知识点:树状数组。
本题正解,是用树状数组维护区间和、区间加,是个板子题。
设原数组为 \(a\) ,差分数组为 \(d\) ,有如下公式:
sum_{1,x} &= \sum_{i=1}^{x} a_i \\
&= \sum_{i=1}^{x} \sum_{j=1}^{i} d_j \\
&= \sum_{j=1}^{x} \sum_{i=j}^{x} d_j \\
&= \sum_{j=1}^{x} (x-j+1)d_j \\
&= (x+1) \sum_{j=1}^{x} d_j - \sum_{j=1}^{x} jd_j \\
\end{aligned}
\]
因此只需要维护 \(d_j\) 和 \(jd_j\) 的前缀和即可。同时,在差分意义下,区间加转化为两次单点加。
时间复杂度 \(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--) {
int op, l, r;
cin >> op >> l >> r;
if (op == 1) {
int x;
cin >> x;
sgt.update(l, r, { x });
}
else cout << sgt.query(l, r).sum << '\n';
}
return 0;
}
方法二
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
struct T {
ll sum;
static T e() { return { 0 }; }
T &operator+=(const T &x) { return sum += x.sum, *this; }
};
template<class T>
class Fenwick {
int n;
vector<T> node;
public:
Fenwick(int _n = 0) { init(_n); }
void init(int _n) {
n = _n;
node.assign(n + 1, T::e());
}
void update(int x, T val) { for (int i = x;i <= n;i += i & -i) node[i] += val; }
T query(int x) {
T ans = T::e();
for (int i = x;i >= 1;i -= i & -i) ans += node[i];
return ans;
}
};
int main() {
std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int n, q;
cin >> n >> q;
Fenwick<T> d(n + 1), di(n + 1);
auto update = [&](int l, int r, T val) {
d.update(l, { val.sum }), d.update(r + 1, { -val.sum });
di.update(l, { l * val.sum }), di.update(r + 1, { -(r + 1) * val.sum });
};
auto query = [&](int l, int r) {
return ((r + 1) * d.query(r).sum - di.query(r).sum) -
(l * d.query(l - 1).sum - di.query(l - 1).sum);
};
for (int i = 1;i <= n;i++) {
int x;
cin >> x;
update(i, i, { x });
}
while (q--) {
int op, l, r;
cin >> op >> l >> r;
if (op == 1) {
int x;
cin >> x;
update(l, r, { x });
}
else cout << query(l, r) << '\n';
}
return 0;
}
NC50454 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 ...
随机推荐
- cs 保研经验贴 | 数学试题 · 自动化所特供版
据(2022 年我所看的)往年经验,自动化所比较重视数学. 感觉,按照自动化所的数学题库复习,就足以应付大多数夏令营的笔试面试了. 目录 高等数学 线性代数 概率论 机器学习 复变函数 其他 同站相关 ...
- [转帖]TiDB 统计数据库占用大小的N种方法
TiDB之路2022-03-06 3896 前言 TiDB 如何统计数据库占用空间大小 四种方法 方法一 TiDB 统计数据库占用大小的第一种方法是监控.通过查看 {cluster-name} - O ...
- 【转帖】《MySQL高级篇》四、索引的存储结构
1. 为什么使用索引 假如给数据使用 二叉树 这样的数据结构进行存储,如下图所示 2.索引及其优缺点 2.1 索引概述 2.2 优点 类似大学图书馆建书目索引,提高数据检索的效率,降低 数据库的 IO ...
- CPU 大拿的作品
http://nieyong.github.io/wiki_cpu/index.html 改天学习写一下.
- pytest.ini配置文件
pytest.ini文件是pytest框架独有的配置文件,主要作用就是在运行pytest.main时可指定运行顺序,也 就相当于在Terminal输入pytest+参数+路径效果一致,下面介绍几种简单 ...
- java浅拷贝BeanUtils.copyProperties引发的RPC异常 | 京东物流技术团队
背景 近期参与了一个攻坚项目,前期因为其他流程原因,测试时间已经耽搁了好几天了,本以为已经解决了卡点,后续流程应该顺顺利利的,没想到 人在地铁上,bug从咚咚来~ 没有任何修改的服务接口,抛出异常: ...
- click与addEventListener和removeEventListener事件与移除正确的移除事件详解
1. onclick事件 es5 普通事件就是直接触发事件,相同的事件会被覆盖掉.代码如下: let demoDiv=document.querySelector(".demo") ...
- 【JS 逆向百例】W店UA,OB反混淆,抓包替换CORS跨域错误分析
关注微信公众号:K哥爬虫,持续分享爬虫进阶.JS/安卓逆向等技术干货! 声明 本文章中所有内容仅供学习交流,抓包内容.敏感网址.数据接口均已做脱敏处理,严禁用于商业用途和非法用途,否则由此产生的一切后 ...
- Gorm 入门介绍与基本使用
Gorm 入门介绍与基本使用 目录 Gorm 入门介绍与基本使用 一.ORM简介 1.1 什么是ORM 1.2 使用ORM的好处 1.2.1 避免直接操作SQL语句 1.2.2 提高代码的可维护性 1 ...
- 窗口管理器 dwm安装
上一篇博文中,已经完成了archlinux的安装,但是进去仅仅是一个冰冷冷的交互式命令窗口.没有图像,也无法打开浏览器.离日常使用还差的很远,接下来首先需要做的就是安装桌面环境.这里我不打算使用诸如g ...