首页 > 其他分享 >F - Two Sequence Queries

F - Two Sequence Queries

时间:2024-06-09 18:10:58浏览次数:12  
标签:limits Sequence cdot sum Two times leq Queries query

F - Two Sequence Queries

Problem Statement

You are given sequences of length $N$, $A=(A_1,A_2,\ldots,A_N)$ and $B=(B_1,B_2,\ldots,B_N)$.
You are also given $Q$ queries to process in order.

There are three types of queries:

  • 1 l r x : Add $x$ to each of $A_l, A_{l+1}, \ldots, A_r$.
  • 2 l r x : Add $x$ to each of $B_l, B_{l+1}, \ldots, B_r$.
  • 3 l r : Print the remainder of $\displaystyle\sum_{i=l}^r (A_i\times B_i)$ when divided by $998244353$.

Constraints

  • $1\leq N,Q\leq 2\times 10^5$
  • $0\leq A_i,B_i\leq 10^9$
  • $1\leq l\leq r\leq N$
  • $1\leq x\leq 10^9$
  • 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, $\mathrm{query}_i$ $(1\leq i\leq Q)$ is the $i$-th query to be processed.

$N$ $Q$
$A_1$ $A_2$ $\ldots$ $A_N$
$B_1$ $B_2$ $\ldots$ $B_N$
$\mathrm{query}_1$
$\mathrm{query}_2$
$\vdots$
$\mathrm{query}_Q$

Each query is given in one of the following formats:

$1$ $l$ $r$ $x$

$2$ $l$ $r$ $x$

$3$ $l$ $r$

Output

If there are $K$ queries of the third type, print $K$ lines.
The $i$-th line ($1\leq i\leq K$) should contain the output for the $i$-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, $A=(1,3,5,6,8)$ and $B=(3,1,2,1,2)$. The queries are processed in the following order:

  • For the first query, print $(1\times 3)+(3\times 1)+(5\times 2)=16$ modulo $998244353$, which is $16$.
  • For the second query, add $3$ to $A_2,A_3,A_4,A_5$. Now $A=(1,6,8,9,11)$.
  • For the third query, print $(1\times 3)+(6\times 1)+(8\times 2)=25$ modulo $998244353$, which is $25$.
  • For the fourth query, add $1$ to $A_1,A_2,A_3$. Now $A=(2,7,9,9,11)$.
  • For the fifth query, add $2$ to $B_5$. Now $B=(3,1,2,1,4)$.
  • For the sixth query, print $(2\times 3)+(7\times 1)+(9\times 2)+(9\times 1)+(11\times 4)=84$ modulo $998244353$, which is $84$.

Thus, the first, second, and third lines should contain $16$, $25$, and $84$, 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 $998244353$ for the third type of query.

 

解题思路

  将修改操作统一成同时对区间内的 $a_i$ 和 $b_i$ 进行修改。具体的,对于操作 $1$,对 $i \in [l,r]$ 内的每个 $a_i$ 加上 $x$,每个 $b_i$ 加上 $0$;对于操作 $2$,则每个 $a_i$ 加上 $0$,每个 $b_i$ 加上 $x$。由于涉及到区间加,显然要用到线段树,下面考虑需要维护哪些信息。

  当对区间 $[l,r]$ 内的 $a_i$ 加上 $x$,$b_i$ 加上 $y$,原先的 $\sum\limits_{i=l}^{r}{a_i \cdot b_i}$ 就会变成

\begin{align*}
&\sum\limits_{i=l}^{r}{(a_i + x) \cdot (b_i + y)} \\
= &\sum\limits_{i=l}^{r}{a_i \cdot b_i + a_i \cdot y + b_i \cdot x + x \cdot y} \\
= &\sum\limits_{i=l}^{r}{a_i \cdot b_i} + y \cdot \sum\limits_{i=l}^{r}{a_i} + x \cdot \sum\limits_{i=l}^{r}{b_i} + x \cdot y \cdot (r - l + 1)
\end{align*}

  因此对于每个维护着区间 $[l,r]$ 的线段树节点,我们需要维护 $s_1$ 表示区间内 $a_i \cdot b_i$ 的和;$s_2$ 表示区间 $a_i$ 的和;$s_3$ 表示区间 $b_i$ 的和。当对整个节点的 $a_i$ 加上 $x$,$b_i$ 加上 $y$,那么对应的更新操作就是 $\displaylines{\begin{cases} s_1 \gets s_1 + s_2 \cdot y + s_3 \cdot x + x \cdot y \cdot (r - l + 1) \\ s_2 \gets s_2 + x \cdot (r - l + 1) \\ s_3 \gets s_3 + y \cdot (r - l + 1) \end{cases}}$。

  AC 代码如下,时间复杂度为 $O(q\log{n})$:

#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

标签:limits,Sequence,cdot,sum,Two,times,leq,Queries,query
From: https://www.cnblogs.com/onlyblues/p/18239834

相关文章

  • Linux命令 (network statistics -all numeric programs | Global Regular Expression P
    文章目录1、第一种解释2、第二种解释3、第三种解释4、第四种解释5、第五种解释6、netstat--help在Windows中,杀死端口占用的博客链接在Linux中,grep的英文全称是GlobalRegularExpressionPrint全局正则表达式打印。它用于在文本中搜索与指定模式匹配的行,并将这......
  • 裁剪序列Cut the Sequence
    首先,我们可以先想一想朴素算法,推出DP,i表示分了几段,则可以推出$$F[i]=min_{1<=j<=i}(f[j]+max_{j+1<=k<=i}(a[k]))$$点击查看代码 memset(f,0x3f,sizeoff); f[0]=0; for(inti=1;i<=n;i++) { for(intj=0;j<i;j++) { inttmp=0;llsum=0; for(intk=j+1;k<=i;k++......
  • jQWidgets 19.2.0 Visualize Sequences
    jQWidgets19.2.0VisualizeSequencesjQWidgets19.2.0addsanewcomponentforvisualizingchronologicaldatawithsupportforinteractivescrolling,customizablestyles,andrichcontent.jQWidgetsisacomprehensiveJavaScriptUIframeworkofferi......
  • 题解:ABC270G Sequence in mod P
    题目传送门思路题目给出了一个数列\[x_{i+1}\equiv{a\timesx_i+b}\pmod{p}\]并想要求最小的\(x_i=G\),那我们可以考虑求出这个数列的通项公式。具体的,观察到原式可以转化成一个等比数列的形式,所以我们可以先设一个常数\(k\),使得\[x_{i+1}+k=a(x_i+k)\]替换进原先的式子......
  • uniapp-two-day-two之基础内容and滑动组件和滚动栏
    基础内容又是码农无聊的一天,今天也就上了一节早课,下课想学习的服了结果玩了半天手机,终于是在下午学上了,真的是很难控制自己。闲聊结束。text标签text中有上面这几个属性,其中在我看来selectable是挺重要的一个属性,是吧现在不都说是cv工程师吗?可不就是这个来组成了我们工程师......
  • GCD-sequence(Round 950)
    #include<bits/stdc++.h>#defineendl'\n'usingll=longlong;typedefunsignedlonglongull;usingnamespacestd;voidGordenGhost();signedmain(){#ifdefGordenfreopen("in.txt","rt",stdin);freopen......
  • network xxx was found but has incorrect label com.docker.compose.network set to
    在执行docker-composedown之后,再执行docker-composeup-d提示已有同名称标签的虚拟网卡  解决1、执行dockernetworkls命令展示所有的虚拟network2、执行dockernetworkrm<networkId>删除已存在的network3、再重新运行docker-composeup-d启动容器  扩......
  • ESSEN: Improving Evolution State Estimation for Temporal Networks using Von Neum
    我们采用以下六个分类标准:研究重点:这个标准突出了研究的核心目标。网络表示学习旨在找到有效的方法,将复杂的网络结构表示在低维空间中,使其更易于分析并在机器学习任务中使用。例如,Kipf和Welling[7]引入了图卷积网络(GCN)用于静态图上的半监督分类,而Nguyen等人[1......
  • TexQ: Zero-shot Network Quantization with Texture Feature Distribution Calibrati
    我们使用以下这六个标准对网络量化和相关领域的研究进行分类。以下是每个标准的详细解释,并结合了参考文献中的相关研究:研究领域:该标准将研究大致分为三个主要领域:量化:这是上传论文的核心焦点。它涉及减少模型参数的位宽(例如,从32位浮点数到4位整数)等技术,以压缩模型并提......
  • [pg]指定 schema 下所有用户或角色对 sequence
    在PostgreSQL中可以使用以下查询来获取指定schema下所有用户或角色对sequence的权限详细信息:查询指定schema下所有用户对sequence的权限:SELECTpg_catalog.pg_get_userbyid(g.grantee)ASgrantee,c.relnameASsequence_name,array_to_string(array......