F - Two Sequence Queries
F - Two Sequence Queries
Problem Statement
You are given sequences of length , and .
You are also given queries to process in order.
There are three types of queries:
1 l r x
: Add to each of .2 l r x
: Add to each of .3 l r
: Print the remainder of when divided by .
Constraints
- All input values are integers.
- There is at least one query of the third type.
Input
The input is given from Standard Input in the following format. Here, is the -th query to be processed.
Each query is given in one of the following formats:
Output
If there are queries of the third type, print lines.
The -th line () should contain the output for the -th query of the third type.
Sample Input 1
5 6
1 3 5 6 8
3 1 2 1 2
3 1 3
1 2 5 3
3 1 3
1 1 3 1
2 5 5 2
3 1 5
Sample Output 1
16
25
84
Initially, and . The queries are processed in the following order:
- For the first query, print modulo , which is .
- For the second query, add to . Now .
- For the third query, print modulo , which is .
- For the fourth query, add to . Now .
- For the fifth query, add to . Now .
- For the sixth query, print modulo , which is .
Thus, the first, second, and third lines should contain , , and , respectively.
Sample Input 2
2 3
1000000000 1000000000
1000000000 1000000000
3 1 1
1 2 2 1000000000
3 1 2
Sample Output 2
716070898
151723988
Make sure to print the sum modulo for the third type of query.
解题思路
将修改操作统一成同时对区间内的 和 进行修改。具体的,对于操作 ,对 内的每个 加上 ,每个 加上 ;对于操作 ,则每个 加上 ,每个 加上 。由于涉及到区间加,显然要用到线段树,下面考虑需要维护哪些信息。
当对区间 内的 加上 , 加上 ,原先的 就会变成
因此对于每个维护着区间 的线段树节点,我们需要维护 表示区间内 的和; 表示区间 的和; 表示区间 的和。当对整个节点的 加上 , 加上 ,那么对应的更新操作就是 。
AC 代码如下,时间复杂度为 :
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 2e5 + 5, mod = 998244353;
int a[N], b[N];
struct Node {
int l, r, s1, s2, s3, sum1, sum2;
}tr[N * 4];
void pushup(int u) {
tr[u].s1 = (tr[u << 1].s1 + tr[u << 1 | 1].s1) % mod;
tr[u].s2 = (tr[u << 1].s2 + tr[u << 1 | 1].s2) % mod;
tr[u].s3 = (tr[u << 1].s3 + tr[u << 1 | 1].s3) % mod;
}
void build(int u, int l, int r) {
tr[u] = {l, r};
if (l == r) {
tr[u].s1 = 1ll * a[l] * b[l] % mod;
tr[u].s2 = a[l] % mod;
tr[u].s3 = b[l] % mod;
}
else {
int mid = l + r >> 1;
build(u << 1, l, mid);
build(u << 1 | 1, mid + 1, r);
pushup(u);
}
}
void upd(int u, int x, int y) {
int len = tr[u].r - tr[u].l + 1;
tr[u].s1 = (tr[u].s1 + 1ll * tr[u].s2 * y + 1ll * tr[u].s3 * x + 1ll * x * y % mod * len) % mod;
tr[u].s2 = (tr[u].s2 + 1ll * x * len) % mod;
tr[u].s3 = (tr[u].s3 + 1ll * y * len) % mod;
tr[u].sum1 = (tr[u].sum1 + x) % mod;
tr[u].sum2 = (tr[u].sum2 + y) % mod;
}
void pushdown(int u) {
upd(u << 1, tr[u].sum1, tr[u].sum2);
upd(u << 1 | 1, tr[u].sum1, tr[u].sum2);
tr[u].sum1 = tr[u].sum2 = 0;
}
void modify(int u, int l, int r, int x, int y) {
if (tr[u].l >= l && tr[u].r <= r) {
upd(u, x, y);
}
else {
pushdown(u);
int mid = tr[u].l + tr[u].r >> 1;
if (l <= mid) modify(u << 1, l, r, x, y);
if (r >= mid + 1) modify(u << 1 | 1, l, r, x, y);
pushup(u);
}
}
int query(int u, int l, int r) {
if (tr[u].l >= l && tr[u].r <= r) return tr[u].s1;
pushdown(u);
int mid = tr[u].l + tr[u].r >> 1;
if (r <= mid) return query(u << 1, l, r);
if (l >= mid + 1) return query(u << 1 | 1, l, r);
return (query(u << 1, l, r) + query(u << 1 | 1, l, r)) % mod;
}
int main() {
int n, m;
scanf("%d %d", &n, &m);
for (int i = 1; i <= n; i++) {
scanf("%d", a + i);
}
for (int i = 1; i <= n; i++) {
scanf("%d", b + i);
}
build(1, 1, n);
while (m--) {
int op, l, r, x;
scanf("%d %d %d", &op, &l, &r);
if (op == 1) {
scanf("%d", &x);
modify(1, l, r, x, 0);
}
else if (op == 2) {
scanf("%d", &x);
modify(1, l, r, 0, x);
}
else {
printf("%d\n", query(1, l, r));
}
}
return 0;
}
参考资料
Editorial - SuntoryProgrammingContest2024(AtCoder Beginner Contest 357):https://atcoder.jp/contests/abc357/editorial/10187
本文来自博客园,作者:onlyblues,转载请注明原文链接:https://www.cnblogs.com/onlyblues/p/18239834
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探
· 为什么 退出登录 或 修改密码 无法使 token 失效
2022-06-09 闪烁