首页 > 其他分享 >G - Smaller Sum

G - Smaller Sum

时间:2024-02-04 20:34:20浏览次数:29  
标签:Smaller int Sum beta alpha query oplus gamma

G - Smaller Sum

Problem Statement

You are given a sequence $A=(A_1,A_2,\dots,A_N)$ of length $N$.

Answer the following $Q$ queries. The $i$-th query is as follows:

  • Find the sum of the elements among $A_{L_i},A_{L_i+1},\dots,A_{R_i}$ that are not greater than $X_i$.

Here, you need to answer these queries online.
That is, only after you answer the current query is the next query revealed.

For this reason, instead of the $i$-th query itself, you are given encrypted inputs $\alpha_i, \beta_i, \gamma_i$ for the query. Restore the original $i$-th query using the following steps and then answer it.

  • Let $B_0=0$ and $B_i =$ (the answer to the $i$-th query).
  • Then, the query can be decrypted as follows:
    • $L_i = \alpha_i \oplus B_{i-1}$
    • $R_i = \beta_i \oplus B_{i-1}$
    • $X_i = \gamma_i \oplus B_{i-1}$

Here, $x \oplus y$ denotes the bitwise XOR of $x$ and $y$.

What is bitwise XOR? The bitwise XOR of non-negative integers $A$ and $B$, $A \oplus B$, is defined as follows:
  • The digit in the $2^k$ place ($k \geq 0$) of $A \oplus B$ in binary is $1$ if exactly one of the corresponding digits of $A$ and $B$ in binary is $1$, and $0$ otherwise.
For example, $3 \oplus 5 = 6$ (in binary: $011 \oplus 101 = 110$).

Constraints

  • All input values are integers.
  • $1 \le N \le 2 \times 10^5$
  • $0 \le A_i \le 10^9$
  • $1 \le Q \le 2 \times 10^5$
  • For the encrypted inputs, the following holds:
    • $0 \le \alpha_i, \beta_i, \gamma_i \le 10^{18}$
  • For the decrypted queries, the following holds:
    • $1 \le L_i \le R_i \le N$
    • $0 \le X_i \le 10^9$

Input

The input is given from Standard Input in the following format:

$N$
$A_1$ $A_2$ $\dots$ $A_N$
$Q$
$\alpha_1$ $\beta_1$ $\gamma_1$
$\alpha_2$ $\beta_2$ $\gamma_2$
$\vdots$
$\alpha_Q$ $\beta_Q$ $\gamma_Q$

Output

Print $Q$ lines.
The $i$-th line should contain the answer to the $i$-th query.


Sample Input 1

8
2 0 2 4 0 2 0 3
5
1 8 3
10 12 11
3 3 2
3 6 5
12 0 11

Sample Output 1

9
2
0
8
5

The given sequence is $A=(2,0,2,4,0,2,0,3)$.
This input contains five queries.

  • Initially, $B_0=0$.
  • The first query is $\alpha = 1, \beta = 8, \gamma = 3$.
    • After decryption, we get $L_i = \alpha \oplus B_0 = 1, R_i = \beta \oplus B_0 = 8, X_i = \gamma \oplus B_0 = 3$.
    • The answer to this query is $9$. This becomes $B_1$.
  • The next query is $\alpha = 10, \beta = 12, \gamma = 11$.
    • After decryption, we get $L_i = \alpha \oplus B_1 = 3, R_i = \beta \oplus B_1 = 5, X_i = \gamma \oplus B_1 = 2$.
    • The answer to this query is $2$. This becomes $B_2$.
  • The next query is $\alpha = 3, \beta = 3, \gamma = 2$.
    • After decryption, we get $L_i = \alpha \oplus B_2 = 1, R_i = \beta \oplus B_2 = 1, X_i = \gamma \oplus B_2 = 0$.
    • The answer to this query is $0$. This becomes $B_3$.
  • The next query is $\alpha = 3, \beta = 6, \gamma = 5$.
    • After decryption, we get $L_i = \alpha \oplus B_3 = 3, R_i = \beta \oplus B_3 = 6, X_i = \gamma \oplus B_3 = 5$.
    • The answer to this query is $8$. This becomes $B_4$.
  • The next query is $\alpha = 12, \beta = 0, \gamma = 11$.
    • After decryption, we get $L_i = \alpha \oplus B_4 = 4, R_i = \beta \oplus B_4 = 8, X_i = \gamma \oplus B_4 = 3$.
    • The answer to this query is $5$. This becomes $B_5$.

 

解题思路

  题目大意就是每次询问区间 $i \in [l, r]$ 内不超过 $x$ 的 $a_i$ 的总和,并且由于输入的特殊性导致强行在线处理。

  可以用可持久化线段树来做。具体来说,这里的线段树是权值线段树,线段树节点维护的是对应值域内出现的数的总和,记作 $s$。然后维护出 $n$ 个版本的线段树,其中第 $i$ 个版本的线段树是指插入 $a_1 \sim a_i$ 后的线段树。那么对于询问 $(l,r,x)$,只需对第 $r$ 个和第 $l-1$ 个版本的线段树求 $[0, x]$ 范围内 $s$ 的值即可,这里的 $s$ 指第 $r$ 个和第 $l-1$ 个版本的线段树中每个节点的 $s$ 的差值,表示的只考虑在 $a_l \sim a_r$ 中某个值域范围内出现的数的总和。

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

#include <bits/stdc++.h>
using namespace std;

typedef long long LL;

const int N = 2e5 + 10;

int a[N];
struct Node {
    int l, r;
    LL s;
}tr[N * 35];
int root[N], idx;

int modify(int u, int l, int r, int x, int c) {
    int v = ++idx;
    tr[v] = tr[u];
    if (l == r) {
        tr[v].s += c;
        return v;
    }
    int mid = l + r >> 1;
    if (x <= mid) tr[v].l = modify(tr[u].l, l, mid, x, c);
    else tr[v].r = modify(tr[u].r, mid + 1, r, x, c);
    tr[v].s = tr[tr[v].l].s + tr[tr[v].r].s;
    return v;
}

LL query(int u, int v, int l, int r, int ql, int qr) {
    if (l >= ql && r <= qr) return tr[u].s - tr[v].s;
    int mid = l + r >> 1;
    LL ret = 0;
    if (ql <= mid) ret = query(tr[u].l, tr[v].l, l, mid, ql, qr);
    if (qr >= mid + 1) ret += query(tr[u].r, tr[v].r, mid + 1, r, ql, qr);
    return ret;
}

int main() {
    int n, m, mx = 0;
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) {
        scanf("%d", a + i);
        mx = max(mx, a[i]);
    }
    for (int i = 1; i <= n; i++) {
        root[i] = modify(root[i - 1], 0, mx, a[i], a[i]);
    }
    scanf("%d", &m);
    LL t = 0;
    while (m--) {
        LL x, y, z;
        scanf("%lld %lld %lld", &x, &y, &z);
        int l = x ^ t, r = y ^ t, c = z ^ t;
        t = query(root[r], root[l - 1], 0, mx, 0, c);
        printf("%lld\n", t);
    }
    
    return 0;
}

 

参考资料

  AtCoder Beginner Contest 339:https://www.cnblogs.com/Lanly/p/18005285

标签:Smaller,int,Sum,beta,alpha,query,oplus,gamma
From: https://www.cnblogs.com/onlyblues/p/18006944

相关文章

  • [office] excel sumproduct函数如何多条件求和
    1.打开含有模拟数据的EXCEL工作表;2.为工作方便,我们先将数据区定义名称,选中F2:F22,将名称定义为“销售额”;3.将E2:E22定义为“商品数据”;4.将D2:D22定义为“区域数据”;5.将C2:C22定义为“项目数据”;相关推荐:《excel基础教程》6.将B2:B22定义为“姓名数据”;7.将A2:A22定义为“月数据......
  • CodeCraft-22 and Codeforces Round 795 (Div. 2)C. Sum of Substrings(分类讨论、贪
    感觉分类讨论的能有点弱。遇到复杂一点的分类讨论的题目,代码就写的巨长。首先观察到处在中间位置的1对答案的贡献是11,具体在中间哪个位置是没有关系的。只有两端的两个位置是比较特殊的\(1位置处的1对答案的贡献是10\)\(2位置处的1对答案的贡献是1\)所有我们考虑将最左端第一......
  • ABC339_g Smaller Sum 题解
    题目链接:Atcoder或者洛谷比价朴素的题,首先有暴力的想法就是树套树或者分块。这两种就不再赘述,这里来正式提提主xi树(应该不能打出来这玩意)的本质而不再停留在板题找第\(k\)大上。对于可差性问题和传统问题不同,我们对于可差性问题往往都有更好的优化方案。例如对于树类问......
  • nvm安装Nodejs时报错,Could not retrieve https://npm.taobao.org/mirrors/node/latest
    1.首先要使用管理员运行命令2.在安装nvm的目录下找到settings.txt,没有就手动增加一个node_mirror:https://npm.taobao.org/mirrors/node/npm_mirror:https://npm.taobao.org/mirrors/npm/这个地方有点奇怪,安装18的时候把上面的Https://去掉以后就下载成功了3.安装19以及......
  • Solution - Median Sum
    其它题不是很写得动了跑来写一下这个题,还是挺有趣的。给定由\(n\)个正整数\(a_1,a_2,\dots,a_n\)组成的可重集合,求出它的非空子集的和的中位数。设\(sum=\sum\limits_{i=1}^na_i\)。首先是对于任意一个子集,设其和为\(x\),我们将其取反,就是选的改成不选,不选的改......
  • [ARC163D] Sum of SCC 题解
    题目链接点击打开链接题目解法好牛的性质!!!首先一个家喻户晓的性质是:竞赛图缩点之后是一条链考虑从这个上面拓展出一个更优美的性质:竞赛图的\(scc\)个数\(=\)把点集分成两个集合\(A,B\),满足\(\forallu\inA,v\inB\),\(u,v\)之间边的方向为\(u\tov\)的方案数\(-1\)......
  • TUniDBGrid控件之Summary例子
    TUniDBGrid控件之Summary例子(记录一下,方便以后备查)在这个例子中,主要用到TUniDBGrid、TClientDataSet和TDataSource三个控件。本文除去介绍使用TUniDBGrid控件之Summary外,TClientDataSet使用FieldDefs用于自定义的字段名表(即:不使用dataprovider)参考:Delphi中ClientDataSet的用......
  • Summary - ber 中周赛 Round 24
    倒数第一我最强,worthreflecting,故记之。T1一开始读错题了,浪费了不知道多久。然后后面实在不会了打了一个很丑的\(O(n\logn)\)。中途提示说学了双指针,但是认为那是T3用的,,,总之就没想到。但是为什么呀?固定好了右端点逐渐变大,左端点也变大,那么中间的最优决策点也逐渐变大。......
  • ARC127D Sum of Min of Xor
    ARC127DSumofMinofXor性质分析加通用套路。思路首先我们把这题的\(\min\)给去掉,那么我们按位算贡献,可以求出和。这是这种式子的通用套路。考虑加上\(\min\),那么我们先按照\((a_i,b_i)\)的最高位分为:\((1,0)\),\((0,1)\),\((1,1)\),\((0,0)\)四种情况。可以发现用贡献......
  • CF1920B Summation Game
    题目传送门codeforces洛谷题面AliceandBobareplayingagame.Theyhaveanarray$a_1,a_2,\ldots,a_n$.Thegameconsistsoftwosteps:First,Alicewillremoveatmost\(k\)elementsfromthearray.Second,Bobwillmultiplyatmost\(x\)elementsoft......