码蹄杯2025 S组T2 题解

· · 题解

码蹄杯2025 S组T2 题解

题目链接(改了取模的值)

题意

给定两个长度为 n 的数列 a, b,记 f(i, j) = \bigoplus_{k = i}^ja_k \times \sum_{k = i}^jb_k,求 \sum_{i = 1}^n\sum_{j = i}^nf(i, j)

思路

暴力的思路就不用讲了吧,直接前缀和秒了。

对于每个 j 相等的 f(i, j),把他们的和快速算出来。我们记两个数组 suma_i = \bigoplus_{j = 1}^ia_j, sumb_i = \sum_{j = 1}^jb_j,即 a 数组的前缀异或和和 b 数组的前缀和。则对于每一个 j,我们可以算出:

\sum_{i = 1}^jf(i, j) = \sum_{i = 1}^j((suma_j \oplus suma_{i - 1}) \times (sumb_j - sumb_{i - 1})) \\ = \sum_{i = 1}^j (sumb_j \times (suma_j \oplus suma_{i - 1})) - \sum_{i = 1}^j (sumb_{i - 1} \times (suma_j \oplus suma_{i - 1})) \\ = sumb_j \times \sum_{i = 1}^j (suma_j \oplus suma_{i - 1}) - \sum_{i = 1}^j (sumb_{i - 1} \times (suma_j \oplus suma_{i - 1}))

然后就算完了。其实还挺简单的,就用一下乘法分配律就好了。

然后,我们考虑把分别维护拆好的式子的减号两边。

左边的这一段不是很难。因为我们可以维护一个数列 c,其中,对于每一个 1 \leq i \leq n,都有 c_i = suma_i \times [i \leq j],即对于 1 \leq i \leq j,有 c_i = suma_i,否则有 c_i = 0。于是,我们处理左边的部分时,需要把区间 [1, j] 全部异或上 suma_j,再更新答案,但是由于没有存 suma_0,因此还要加一个 suma_j \times sumb_j,转移之前再异或回去,也就是把区间 [1, j] 全部异或上 suma_j,在把 j 往小转移时,只需要对 c_j 进行单点修改即可。以上步骤可以用线段树维护,如果不会的,可以看一下 这道题 的题解。

但是,用线段树维护会很复杂,时间复杂度和空间复杂度也会很高,因此需要优化。由于每次我们都是将整个区间 [1, j] 异或上 suma_j,因此我们只需要维护整个区间的信息。对于每一次异或时,我们需要多传一个参数 N,代表当前的 j,对于 suma_j 的每一个二进制表示下为 1 的位,把原来的值 val_i 变为 N - val_i 即可。求值时直接计算 \sum_{i = 0}^{lg}(val_i \times 2^i),其中 lg 为 \log{10^9},原来的单点修改就把 suma_j 对应的每个为 1 的二进制位对应的 val_i 减一即可。于是,左边这一部分就解决了,代码如下:

long long w[100];

void yh(long long N, long long x) {  //区间修改
    for (int j = 0; j <= 60; ++j) {  //保险起见跑到了60
        if (x & (1ll << j)) {
            w[j] = N - w[j];
        }
    }
}

long long query() {  //区间查询
    long long res = 0;
    for (long long j = 0; j <= 60; ++j) {
        res = (res + (1ll << j) % mod * (w[j] % mod) % mod) % mod;
    }
    return res;
}

//以下是主函数中对应的部分
for (int i = n; i >= 1; --i) {
    yh(i, suma[i]);
    ans += sumb[i] * query();
    ans += sumb[i] * suma[i];  //没有加suma[0],因此要加上这句话
    ans %= mod;
    yh(i, suma[i]);
    for (int j = 0; j <= 60; ++j) {
        if (suma[i] & (1ll << j)) {
            --w[j];
        }
    }
}

然后考虑右边的这一段。

我们已经实现过了区间异或无系数的情况,而右边的这一段无非就是加了一个系数。但是这个系数怎么处理呢?没有系数其实就是系数为 1,因此我们考虑把它的系数变为 1。假如说右边有一个项,叫做 5 \times 4,那么我们把它变成 5 \times (4 \oplus x) 时,只用把它变成 (4 \oplus x) + (4 \oplus x) + (4 \oplus x) + (4 \oplus x) + (4 \oplus x),也就是把它拆成很多个异或的结果相加。这样,我们就可以按照处理左边部分的思路处理右边部分了。但是,如果直接拆,项数可能很多,因为 sumb 可能很大。因此,插入的时候,对于每个 suma_i 的为一的二进制位,直接加上 sumb_i 即可,单点修改同理。区间异或时,和左边部分同理,维护一个 N,但是每次修改时要把 N 设为 \sum_{j = 1}^isumb_j,即 b 数组前缀和的前缀和。具体详见代码。

long long w2[MAXN];

void yh2(long long sum, long long x) {
    for (int j = 0; j <= 60; ++j) {
        if (x & (1ll << j)) {
            w2[j] = sum - w2[j];
            w2[j] = (w2[j] % mod + mod) % mod;
        }
    }
}

long long qry() {
    long long res = 0;
    for (long long j = 0; j <= 60; ++j) {
        res = (res + (1ll << j) % mod * (w2[j] % mod) % mod) % mod;
    }
    return res;
}

//以下是主函数中对应的部分:
for (int i = n; i >= 1; --i) {
    yh2(sumbsum, suma[i]);
    ans -= qry();
    ans %= mod;
    yh2(sumbsum, suma[i]);
    for (int j = 0; j <= 60; ++j) {
        if (suma[i] & (1ll << j)) {
            w2[j] -= sumb[i];
            w2[j] = (w2[j] % mod + mod) % mod;
        }
    }
    sumbsum -= sumb[i];
    if (sumbsum > mod) sumbsum = (sumbsum % mod + mod) % mod;
}

全部代码(不要抄qwq,有坑):

#include <bits/stdc++.h> 

#define int long long
//#define getchar_unlocked getchar

using namespace std;

const int MAXN = 5e5 + 100;
long long n, a[MAXN], b[MAXN], suma[MAXN], sumb[MAXN];

const long long mod = 998244353;
long long ans = 0;

inline int read() {
    int rd = 0;
    char ch = getchar();
    while (!(ch >= '0' && ch <= '9')) ch = getchar_unlocked();
    rd = ch - '0'; ch = getchar_unlocked();
    while (ch >= '0' && ch <= '9') rd = rd * 10 + ch - '0', ch = getchar_unlocked();
    return rd;
}

long long w[100];

void yh(long long N, long long x) {
    for (int j = 0; j <= 60; ++j) {
        if (x & (1ll << j)) {
            w[j] = N - w[j];
        }
    }
}

long long query() {
    long long res = 0;
    for (long long j = 0; j <= 60; ++j) {
        res = (res + (1ll << j) % mod * (w[j] % mod) % mod) % mod;
    }
    return res;
}

long long w2[MAXN];

void yh2(long long sum, long long x) {
    for (int j = 0; j <= 60; ++j) {
        if (x & (1ll << j)) {
            w2[j] = sum - w2[j];
            w2[j] = (w2[j] % mod + mod) % mod;
        }
    }
}

long long qry() {
    long long res = 0;
    for (long long j = 0; j <= 60; ++j) {
        res = (res + (1ll << j) % mod * (w2[j] % mod) % mod) % mod;
    }
    return res;
}

signed main() {
    n = read();
    for (int i = 1; i <= n; ++i) {
        a[i] = read();
        suma[i] = suma[i - 1] ^ a[i];

        for (int j = 0; j <= 60; ++j) {
            if ((1ll << j) & suma[i]) {
                ++w[j];
            }
        }
    }

    long long sumbsum = 0;

    for (int i = 1; i <= n; ++i) {
        b[i] = read();
        sumb[i] = sumb[i - 1] + b[i];
        if (sumb[i] > mod) sumb[i] %= mod;
        sumbsum += sumb[i];
        sumbsum %= mod;
    }

    for (int i = 1; i <= n; ++i) {
        for (int j = 0; j <= 60; ++j) {
            if ((1ll << j) & suma[i]) {
                w2[j] += sumb[i];
                w2[j] %= mod;
            }
        }
    }

    for (int i = n; i >= 1; --i) {
        yh(i, suma[i]);
        ans += sumb[i] * query() % mod;
        if (ans > mod) ans %= mod;
        ans += sumb[i] * suma[i];
        if (ans > mod) ans %= mod;
        yh2(sumbsum, suma[i]);
        ans -= qry();
        if (ans > mod) ans %= mod;
        yh2(sumbsum, suma[i]);
        yh(i, suma[i]);
        for (int j = 0; j <= 60; ++j) {
            if (suma[i] & (1ll << j)) {
                --w[j];
                w2[j] -= sumb[i];
                w2[j] = (w2[j] % mod + mod) % mod;
            }
        }
        sumbsum -= sumb[i];
        sumbsum = (sumbsum % mod + mod) % mod;
    }
    if (ans > mod) ans %= mod;
    if (ans < 0) ans += mod;
    if (ans > mod) ans %= mod;

    cout << ans << '\n';

    return 0;
}