U405333 帕鲁大陆迷路的一天 题解

题目链接:帕鲁大陆迷路的一天

前置弱化版:P3604 美好的每一天 题解

一个非常简单的普通莫队解很容易写出来,具体的看我前置弱化版题解,然而这个复杂度高达 \(O(26n\sqrt{q})\),显然无法通过强化版。

一种看上去很正确的 “假解”

我们思考如何去掉这个 \(26\),我们猜想:

能够组成 \(pre[curr] \oplus pre[otherr]=t,且 \ popcount(t) \le 1\)\(other\) 数量并不是很多,然后为了防止 \(MLE\),我们可以加入离散化,考虑预处理出这些 \(other\)。在随机的表现下是正确的,但这个是错误的。感谢 cpchenpi大神 提供的构造 Hack 数据,以及哈希函数。提供一种构造方案:

这样一来,我们可以轻松构建出每个桶基本都为 \(\ge 26\)了。在 \(3e5\) 下的测试情况为:

这种假解在不特意构造的情况下,很容易导致每个桶内的数并不会太多,这样枚举的成本常数较小,复杂度即为:\(O(avg \times n\sqrt{q})\)\(avg\) 为平均数。

PS:本题离散化如果采用 \(map\) 因为有枚举 \(26n\) 的预处理,所以枚举的时候,这个预处理复杂度是接近双 \(log\) 的,\(5e5\) 显然不对,所以需要用纯正的哈希表实现 \(O(1)\) 访问。当然使用二分离散化的,最好需要哈希集合判断是否存在某个数,而不是二分查找,这样才不会退化,不过如果你写的常数不大,并没有卡你正解做法。

纯正的哈希表一般有三种,stl 的和 pbds 的两种,本题有些数据会卡哈希,但常数小的代码并不影响 ac,建议使用手写哈希函数,这里使用了大神的哈希函数,实测配合 pbds 的 gp_hash_table 跑得很快,不过这些都不是本题的重点。

错解参照代码
#include <bits/stdc++.h>

// #pragma GCC optimize(2)
// #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native")

#define isPbdsFile

#ifdef isPbdsFile

#include <bits/extc++.h>

#else

#include <ext/pb_ds/priority_queue.hpp>
#include <ext/pb_ds/hash_policy.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/trie_policy.hpp>
#include <ext/pb_ds/tag_and_trait.hpp>
#include <ext/pb_ds/hash_policy.hpp>
#include <ext/pb_ds/list_update_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/exception.hpp>
#include <ext/rope>

#endif

using namespace std;
using namespace __gnu_cxx;
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef tuple<int, int, int> tii;
typedef tuple<ll, ll, ll> tll;
typedef unsigned int ui;
typedef unsigned long long ull;
typedef __int128 i128;
#define hash1 unordered_map
#define hash2 gp_hash_table
#define hash3 cc_hash_table
#define stdHeap std::priority_queue
#define pbdsHeap __gnu_pbds::priority_queue
#define sortArr(a, n) sort(a+1,a+n+1)
#define all(v) v.begin(),v.end()
#define yes cout<<"YES"
#define no cout<<"NO"
#define Spider ios_base::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
#define MyFile freopen("..\\input.txt", "r", stdin),freopen("..\\output.txt", "w", stdout);
#define forn(i, a, b) for(int i = a; i <= b; i++)
#define forv(i, a, b) for(int i=a;i>=b;i--)
#define ls(x) (x<<1)
#define rs(x) (x<<1|1)
#define endl '\n'
//用于Miller-Rabin
[[maybe_unused]] static int Prime_Number[13] = {0, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37};

template <typename T>
int disc(T* a, int n)
{
    return unique(a + 1, a + n + 1) - (a + 1);
}

template <typename T>
T lowBit(T x)
{
    return x & -x;
}

template <typename T>
T Rand(T l, T r)
{
    static mt19937 Rand(time(nullptr));
    uniform_int_distribution<T> dis(l, r);
    return dis(Rand);
}

template <typename T1, typename T2>
T1 modt(T1 a, T2 b)
{
    return (a % b + b) % b;
}

template <typename T1, typename T2, typename T3>
T1 qPow(T1 a, T2 b, T3 c)
{
    a %= c;
    T1 ans = 1;
    for (; b; b >>= 1, (a *= a) %= c)if (b & 1)(ans *= a) %= c;
    return modt(ans, c);
}

template <typename T>
void read(T& x)
{
    x = 0;
    T sign = 1;
    char ch = getchar();
    while (!isdigit(ch))
    {
        if (ch == '-')sign = -1;
        ch = getchar();
    }
    while (isdigit(ch))
    {
        x = (x << 3) + (x << 1) + (ch ^ 48);
        ch = getchar();
    }
    x *= sign;
}

template <typename T, typename... U>
void read(T& x, U&... y)
{
    read(x);
    read(y...);
}

template <typename T>
void write(T x)
{
    if (typeid(x) == typeid(char))return;
    if (x < 0)x = -x, putchar('-');
    if (x > 9)write(x / 10);
    putchar(x % 10 ^ 48);
}

template <typename C, typename T, typename... U>
void write(C c, T x, U... y)
{
    write(x), putchar(c);
    write(c, y...);
}


template <typename T11, typename T22, typename T33>
struct T3
{
    T11 one;
    T22 tow;
    T33 three;

    bool operator<(const T3 other) const
    {
        if (one == other.one)
        {
            if (tow == other.tow)return three < other.three;
            return tow < other.tow;
        }
        return one < other.one;
    }

    T3() { one = tow = three = 0; }

    T3(T11 one, T22 tow, T33 three) : one(one), tow(tow), three(three)
    {
    }
};

template <typename T1, typename T2>
void uMax(T1& x, T2 y)
{
    if (x < y)x = y;
}

template <typename T1, typename T2>
void uMin(T1& x, T2 y)
{
    if (x > y)x = y;
}

constexpr int N = 5e5 + 10;
int pos[N];

struct Mo
{
    int l, r, id;

    bool operator<(const Mo& other) const
    {
        return pos[l] ^ pos[other.l] ? l < other.l : pos[l] & 1 ? r < other.r : r > other.r;
    }
} node[N];

constexpr int MX = 1 << 26 | 1;
int n, q, siz;
int cnt[26 * N];

struct Hash
{
    static uint64_t splitmix64(uint64_t x)
    {
        x += 0x9e3779b97f4a7c15;
        x = (x ^ x >> 30) * 0xbf58476d1ce4e5b9;
        x = (x ^ x >> 27) * 0x94d049bb133111eb;
        return x ^ x >> 31;
    }

    static size_t get(const uint64_t x)
    {
        static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
        return splitmix64(x + FIXED_RANDOM);
    }

    template <typename T>
    size_t operator()(T x) const
    {
        return get(std::hash<T>()(x));
    }

    template <typename F, typename S>
    size_t operator()(pair<F, S> p) const
    {
        return get(std::hash<F>()(p.first)) ^ std::hash<S>(p.second);
    }
};

char c;
int preXor[N];
ll ans[N];
ll mp[N];
vector<int> t[N];
int idx;
hash2<int, int, Hash> ord;
ll res;

inline void add(const int curr)
{
    for (auto v : t[curr])res += cnt[v];
    cnt[preXor[curr]]++;
}

inline void del(const int curr)
{
    cnt[preXor[curr]]--;
    for (const auto v : t[curr])res -= cnt[v];
}

inline void solve()
{
    cin >> n >> q;
    siz = sqrt(n);
    idx = n;
    forn(i, 1, n)
    {
        pos[i] = (i - 1) / siz + 1;
        cin >> c, preXor[i] = preXor[i - 1] ^ 1 << c - 'a';
        mp[i] = preXor[i];
    }
    mp[++idx] = 0;
    sortArr(mp, idx), idx = disc(mp, idx);
    forn(i, 1, idx)ord[mp[i]] = i;
    forn(i, 0, n)
    {
        t[i].push_back(ord[preXor[i]]);
        forn(j, 0, 25)
        {
            const int nxt = preXor[i] ^ 1 << j;
            if (ord.find(nxt) != ord.end())t[i].push_back(ord[nxt]);
        }
        preXor[i] = ord[preXor[i]];
    }
    forn(i, 1, q)
    {
        auto& [l,r,id] = node[i];
        cin >> l >> r, id = i;
        l--;
    }
    sortArr(node, q);
    int l = 1, r = 0;
    forn(i, 1, q)
    {
        auto [L,R,id] = node[i];
        while (l < L)del(l++);
        while (l > L)add(--l);
        while (r < R)add(++r);
        while (r > R)del(r--);
        ans[id] = res;
    }
    forn(i, 1, q)cout << ans[i] << endl;
}

signed int main()
{
    // MyFile
    Spider
    //------------------------------------------------------
    // clock_t start = clock();
    int test = 1;
    //    read(test);
    // cin >> test;
    forn(i, 1, test)solve();
    // while (cin >> n, n)solve();
    //    while (cin >> test)solve();
    // clock_t end = clock();
    // cerr << "time = " << double(end - start) / CLOCKS_PER_SEC << "s" << endl;
}

说说正解

本题正解是莫队二次离线算法。具体的,我们观察到每一个数加入和删除的贡献,都可以转化为前缀和作差,假如当前待加入或者删除的位置为 \(pos\),即转化为某个加上或减去数对 \([0,r]\) 或者 \([0,pos-1]\) 的影响作差。我们观察到其中一部分 \([0,pos-1]\) 这部分影响完全可以从左到右跑一遍 \(26n\) 预处理出来这个前缀影响,剩下的则可以转化为扫描线离线计算。这个影响即为,有多少个对和它组成的异或即为上述所说的 \(popcount \le 1\) 的对。这样我们就轻松地写出了 \(O(52n+2n\sqrt{q})\) 的代码了。

参照代码
#include <bits/stdc++.h>

// #pragma GCC optimize(2)
// #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native")

#define isPbdsFile

#ifdef isPbdsFile

#include <bits/extc++.h>

#else

#include <ext/pb_ds/priority_queue.hpp>
#include <ext/pb_ds/hash_policy.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/trie_policy.hpp>
#include <ext/pb_ds/tag_and_trait.hpp>
#include <ext/pb_ds/hash_policy.hpp>
#include <ext/pb_ds/list_update_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/exception.hpp>
#include <ext/rope>

#endif

using namespace std;
using namespace __gnu_cxx;
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef tuple<int, int, int> tii;
typedef tuple<ll, ll, ll> tll;
typedef unsigned int ui;
typedef unsigned long long ull;
typedef __int128 i128;
#define hash1 unordered_map
#define hash2 gp_hash_table
#define hash3 cc_hash_table
#define stdHeap std::priority_queue
#define pbdsHeap __gnu_pbds::priority_queue
#define sortArr(a, n) sort(a+1,a+n+1)
#define all(v) v.begin(),v.end()
#define yes cout<<"YES"
#define no cout<<"NO"
#define Spider ios_base::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
#define MyFile freopen("..\\input.txt", "r", stdin),freopen("..\\output.txt", "w", stdout);
#define forn(i, a, b) for(int i = a; i <= b; i++)
#define forv(i, a, b) for(int i=a;i>=b;i--)
#define ls(x) (x<<1)
#define rs(x) (x<<1|1)
#define endl '\n'
//用于Miller-Rabin
[[maybe_unused]] static int Prime_Number[13] = {0, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37};

template <typename T>
int disc(T* a, int n)
{
    return unique(a + 1, a + n + 1) - (a + 1);
}

template <typename T>
T lowBit(T x)
{
    return x & -x;
}

template <typename T>
T Rand(T l, T r)
{
    static mt19937 Rand(time(nullptr));
    uniform_int_distribution<T> dis(l, r);
    return dis(Rand);
}

template <typename T1, typename T2>
T1 modt(T1 a, T2 b)
{
    return (a % b + b) % b;
}

template <typename T1, typename T2, typename T3>
T1 qPow(T1 a, T2 b, T3 c)
{
    a %= c;
    T1 ans = 1;
    for (; b; b >>= 1, (a *= a) %= c)if (b & 1)(ans *= a) %= c;
    return modt(ans, c);
}

template <typename T>
void read(T& x)
{
    x = 0;
    T sign = 1;
    char ch = getchar();
    while (!isdigit(ch))
    {
        if (ch == '-')sign = -1;
        ch = getchar();
    }
    while (isdigit(ch))
    {
        x = (x << 3) + (x << 1) + (ch ^ 48);
        ch = getchar();
    }
    x *= sign;
}

template <typename T, typename... U>
void read(T& x, U&... y)
{
    read(x);
    read(y...);
}

template <typename T>
void write(T x)
{
    if (typeid(x) == typeid(char))return;
    if (x < 0)x = -x, putchar('-');
    if (x > 9)write(x / 10);
    putchar(x % 10 ^ 48);
}

template <typename C, typename T, typename... U>
void write(C c, T x, U... y)
{
    write(x), putchar(c);
    write(c, y...);
}


template <typename T11, typename T22, typename T33>
struct T3
{
    T11 one;
    T22 tow;
    T33 three;

    bool operator<(const T3 other) const
    {
        if (one == other.one)
        {
            if (tow == other.tow)return three < other.three;
            return tow < other.tow;
        }
        return one < other.one;
    }

    T3() { one = tow = three = 0; }

    T3(T11 one, T22 tow, T33 three) : one(one), tow(tow), three(three)
    {
    }
};

template <typename T1, typename T2>
void uMax(T1& x, T2 y)
{
    if (x < y)x = y;
}

template <typename T1, typename T2>
void uMin(T1& x, T2 y)
{
    if (x > y)x = y;
}


constexpr int N = 5e5 + 10;
int pos[N];

struct Mo
{
    int l, r, id;

    bool operator<(const Mo& other) const
    {
        return pos[l] ^ pos[other.l] ? l < other.l : pos[l] & 1 ? r < other.r : r > other.r;
    }
} node[N];

int n, q, siz;
ll pre[N];
constexpr int MX = 1 << 26 | 1;
int cnt[MX];
vector<tii> seg[N];

inline void add(const int curr)
{
    cnt[curr]++;
    forn(i, 0, 25)cnt[curr ^ 1 << i]++;
}

char c;
int preXor[N];
ll ans[N];

inline void solve()
{
    cin >> n >> q;
    siz = sqrt(n);
    add(0);
    forn(i, 1, n)
    {
        pos[i] = (i - 1) / siz + 1;
        cin >> c, preXor[i] = preXor[i - 1] ^ (1 << c - 'a');
        pre[i] = cnt[preXor[i]];
        add(preXor[i]);
    }
    forn(i, 1, q)
    {
        auto& [l,r,id] = node[i];
        cin >> l >> r, id = i;
        l--;
    }
    sortArr(node, q);
    int l = 1, r = 0;
    forn(i, 1, q)
    {
        auto [L,R,id] = node[i];
        if (l < L)seg[r].emplace_back(l, L - 1, -id);
        while (l < L)ans[id] += pre[l++];
        if (l > L)seg[r].emplace_back(L, l - 1, id);
        while (l > L)ans[id] -= pre[--l];
        if (r < R and l)seg[l - 1].emplace_back(r + 1, R, -id);
        while (r < R)ans[id] += pre[++r];
        if (r > R and l)seg[l - 1].emplace_back(R + 1, r, id);
        while (r > R)ans[id] -= pre[r--];
    }
    memset(cnt, 0, sizeof cnt);
    forn(i, 0, n)
    {
        add(preXor[i]);
        for (const auto [l,r,id] : seg[i])
        {
            ll v = id / abs(id);
            forn(j, l, r)ans[abs(id)] += v * (cnt[preXor[j]] - (j <= i));
        }
    }
    forn(i, 1, q)ans[node[i].id] += ans[node[i - 1].id];
    forn(i, 1, q)cout << ans[i] << endl;
}

signed int main()
{
    // MyFile
    Spider
    //------------------------------------------------------
    // clock_t start = clock();
    int test = 1;
    //    read(test);
    // cin >> test;
    forn(i, 1, test)solve();
    //    while (cin >> n, n)solve();
    //    while (cin >> test)solve();
    // clock_t end = clock();
    // cerr << "time = " << double(end - start) / CLOCKS_PER_SEC << "s" << endl;
}

这份代码足矣通过,注意到其实我们可以利用前面提到的错解优化常数技巧,大大优化我们的常数。

优化常数后的代码
#include <bits/stdc++.h>

// #pragma GCC optimize(2)
// #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native")

#define isPbdsFile

#ifdef isPbdsFile

#include <bits/extc++.h>

#else

#include <ext/pb_ds/priority_queue.hpp>
#include <ext/pb_ds/hash_policy.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/trie_policy.hpp>
#include <ext/pb_ds/tag_and_trait.hpp>
#include <ext/pb_ds/hash_policy.hpp>
#include <ext/pb_ds/list_update_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/exception.hpp>
#include <ext/rope>

#endif

using namespace std;
using namespace __gnu_cxx;
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef tuple<int, int, int> tii;
typedef tuple<ll, ll, ll> tll;
typedef unsigned int ui;
typedef unsigned long long ull;
typedef __int128 i128;
#define hash1 unordered_map
#define hash2 gp_hash_table
#define hash3 cc_hash_table
#define stdHeap std::priority_queue
#define pbdsHeap __gnu_pbds::priority_queue
#define sortArr(a, n) sort(a+1,a+n+1)
#define all(v) v.begin(),v.end()
#define yes cout<<"YES"
#define no cout<<"NO"
#define Spider ios_base::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
#define MyFile freopen("..\\input.txt", "r", stdin),freopen("..\\output.txt", "w", stdout);
#define forn(i, a, b) for(int i = a; i <= b; i++)
#define forv(i, a, b) for(int i=a;i>=b;i--)
#define ls(x) (x<<1)
#define rs(x) (x<<1|1)
#define endl '\n'
//用于Miller-Rabin
[[maybe_unused]] static int Prime_Number[13] = {0, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37};

template <typename T>
int disc(T* a, int n)
{
    return unique(a + 1, a + n + 1) - (a + 1);
}

template <typename T>
T lowBit(T x)
{
    return x & -x;
}

template <typename T>
T Rand(T l, T r)
{
    static mt19937 Rand(time(nullptr));
    uniform_int_distribution<T> dis(l, r);
    return dis(Rand);
}

template <typename T1, typename T2>
T1 modt(T1 a, T2 b)
{
    return (a % b + b) % b;
}

template <typename T1, typename T2, typename T3>
T1 qPow(T1 a, T2 b, T3 c)
{
    a %= c;
    T1 ans = 1;
    for (; b; b >>= 1, (a *= a) %= c)if (b & 1)(ans *= a) %= c;
    return modt(ans, c);
}

template <typename T>
void read(T& x)
{
    x = 0;
    T sign = 1;
    char ch = getchar();
    while (!isdigit(ch))
    {
        if (ch == '-')sign = -1;
        ch = getchar();
    }
    while (isdigit(ch))
    {
        x = (x << 3) + (x << 1) + (ch ^ 48);
        ch = getchar();
    }
    x *= sign;
}

template <typename T, typename... U>
void read(T& x, U&... y)
{
    read(x);
    read(y...);
}

template <typename T>
void write(T x)
{
    if (typeid(x) == typeid(char))return;
    if (x < 0)x = -x, putchar('-');
    if (x > 9)write(x / 10);
    putchar(x % 10 ^ 48);
}

template <typename C, typename T, typename... U>
void write(C c, T x, U... y)
{
    write(x), putchar(c);
    write(c, y...);
}


template <typename T11, typename T22, typename T33>
struct T3
{
    T11 one;
    T22 tow;
    T33 three;

    bool operator<(const T3 other) const
    {
        if (one == other.one)
        {
            if (tow == other.tow)return three < other.three;
            return tow < other.tow;
        }
        return one < other.one;
    }

    T3() { one = tow = three = 0; }

    T3(T11 one, T22 tow, T33 three) : one(one), tow(tow), three(three)
    {
    }
};

template <typename T1, typename T2>
void uMax(T1& x, T2 y)
{
    if (x < y)x = y;
}

template <typename T1, typename T2>
void uMin(T1& x, T2 y)
{
    if (x > y)x = y;
}

constexpr int N = 5e5 + 10;
int pos[N];

struct Mo
{
    int l, r, id;

    bool operator<(const Mo& other) const
    {
        return pos[l] ^ pos[other.l] ? l < other.l : pos[l] & 1 ? r < other.r : r > other.r;
    }
} node[N];

struct Hash
{
    static uint64_t splitmix64(uint64_t x)
    {
        x += 0x9e3779b97f4a7c15;
        x = (x ^ x >> 30) * 0xbf58476d1ce4e5b9;
        x = (x ^ x >> 27) * 0x94d049bb133111eb;
        return x ^ x >> 31;
    }

    static size_t get(const uint64_t x)
    {
        static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
        return splitmix64(x + FIXED_RANDOM);
    }

    template <typename T>
    size_t operator()(T x) const
    {
        return get(std::hash<T>()(x));
    }

    template <typename F, typename S>
    size_t operator()(pair<F, S> p) const
    {
        return get(std::hash<F>()(p.first)) ^ std::hash<S>(p.second);
    }
};

constexpr int MX = 1 << 26 | 1;
int n, q, siz;
ll pre[N];
vector<tii> seg[N];
int cnt[26 * N];


char c;
int preXor[N];
ll ans[N];
ll mp[N];
vector<int> t[N];
int idx;
hash2<int, int, Hash> ord;

inline void add(const int curr)
{
    for (auto v : t[curr])cnt[v]++;
}

inline void solve()
{
    cin >> n >> q;
    siz = sqrt(n);
    idx = n;
    forn(i, 1, n)
    {
        pos[i] = (i - 1) / siz + 1;
        cin >> c, preXor[i] = preXor[i - 1] ^ 1 << c - 'a';
        mp[i] = preXor[i];
    }
    mp[++idx] = 0;
    sortArr(mp, idx), idx = disc(mp, idx);
    forn(i, 1, idx)ord[mp[i]] = i;
    forn(i, 0, n)
    {
        t[i].push_back(ord[preXor[i]]);
        forn(j, 0, 25)
        {
            const int nxt = preXor[i] ^ 1 << j;
            if (ord.find(nxt) != ord.end())t[i].push_back(ord[nxt]);
        }
        preXor[i] = ord[preXor[i]];
    }
    forn(i, 0, n)
    {
        pre[i] = cnt[preXor[i]];
        add(i);
    }
    forn(i, 1, q)
    {
        auto& [l,r,id] = node[i];
        cin >> l >> r, id = i;
        l--;
    }
    sortArr(node, q);
    int l = 1, r = 0;
    forn(i, 1, q)
    {
        auto [L,R,id] = node[i];
        if (l < L)seg[r].emplace_back(l, L - 1, -id);
        while (l < L)ans[id] += pre[l++];
        if (l > L)seg[r].emplace_back(L, l - 1, id);
        while (l > L)ans[id] -= pre[--l];
        if (r < R and l)seg[l - 1].emplace_back(r + 1, R, -id);
        while (r < R)ans[id] += pre[++r];
        if (r > R and l)seg[l - 1].emplace_back(R + 1, r, id);
        while (r > R)ans[id] -= pre[r--];
    }
    memset(cnt, 0, sizeof cnt);
    forn(i, 0, n)
    {
        add(i);
        for (const auto [l,r,id] : seg[i])
        {
            ll v = id / abs(id);
            forn(j, l, r)ans[abs(id)] += v * (cnt[preXor[j]] - (j <= i));
        }
    }
    forn(i, 1, q)ans[node[i].id] += ans[node[i - 1].id];
    forn(i, 1, q)cout << ans[i] << endl;
}

signed int main()
{
    // MyFile
    Spider
    //------------------------------------------------------
    // clock_t start = clock();
    int test = 1;
    //    read(test);
    // cin >> test;
    forn(i, 1, test)solve();
    // while (cin >> n, n)solve();
    //    while (cin >> test)solve();
    // clock_t end = clock();
    // cerr << "time = " << double(end - start) / CLOCKS_PER_SEC << "s" << endl;
}

\[ 最终优化后的时间复杂度为:O(2\times avg \times n+2n\sqrt{q}) \]

posted @ 2024-02-06 13:46  Athanasy  阅读(52)  评论(0编辑  收藏  举报