首页 > 其他分享 >U405333 帕鲁世界迷路的一天 题解

U405333 帕鲁世界迷路的一天 题解

时间:2024-02-06 13:56:28浏览次数:35  
标签:typedef U405333 int 题解 template include 帕鲁 id define

题目链接:帕鲁世界迷路的一天

前置弱化版: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}) \]

标签:typedef,U405333,int,题解,template,include,帕鲁,id,define
From: https://www.cnblogs.com/Athanasy/p/18009549

相关文章

  • [ARC171] A~D 题解
    [ARC171]A~D题解A.NoAttacking最优策略是车隔行放,分讨一下就可以了。if(n<a)cout<<"No\n";else{if(a*2<n)b-=(a+1)*(n-a);else{b-=(n-a)*(n-a);if(b<=0)cout<<"Yes\n";......
  • P3219 [HNOI2012] 三角形覆盖问题&P1222 三角形 题解
    严格单$\log$做法,附实现细节和代码。考虑从左往右扫描线,发现每次操作是线段上端点$-1$,插入线段,删除退化成点的线段。如果某时刻一条线段被另一条完全覆盖,那么这条线段显然不会产生贡献,可以删去。最后得到的线段一定是左端点单调递增时,右端点也单调递增,可以用set维护。当两......
  • P10145 [WC/CTS2024] 线段树 题解
    Link纪念一下场切题。题意:给定一棵(分点不一定为中点)的线段树,给定若干个询问区间,问有多少个线段树上结点的集合,知道了这些结点对应的区间和就可以知道任何一个询问区间的和。从询问区间开始考虑。会发现可以把所有\(a_i\)分成若干个集合,只要知道每个集合的和就可以知道所有询......
  • 浅谈甲类生产厂房应急照明和疏散指示系统的设计问题解析
    摘要:文章结合电气设计项目实践经验,分析了甲类生产厂房消防应急照明和疏散指示系统设计中照明灯和标志灯的设置、疏散走道和疏散通道的规划、集中控制型系统类型的选择、电压等级的选择中存在的问题,总结了相关经验,可以为同类工程提供参考。关键词:甲类生产厂房;消防应急照明和疏散指示......
  • 题解 CF1876B
    题意给定一个数组\([a_1,a_2,a_3.\cdots,a_n]\),一开始所有元素都为白色。你可以选定其中的至少一个元素涂成黑色,共有\(2^n-1\)种涂法。此时对于所有黑色元素\(a_i\),下标为\(i\)的倍数的所有白色元素都将变成绿色。此时数组中所有黑色和绿色元素的最大值记为此种涂法的......
  • 洛谷 P10145 [WC/CTS2024] 线段树 题解--zhengjun
    提供一种考场做法,在思路上和官方题解的差异蛮大的,虽然结果差不多。首先需要发现\([l,r)\)区间可以算出来的充要条件是:如果对于每个选中的节点\(u\),连无向边\((L_u,R_u)\),则当且仅当\(l\)和\(r\)连通时区间\([l,r)\)可以算出来。证明的话,用前缀和理解这些东西,分别考虑......
  • 题解 CF1849D
    萌新的第一篇题解题目大意对于一个初始颜色都为蓝色的数组\(a_1,a_2,\dots,a_n\)其中\(a_n\in\{0,1,2\}\),现在可以进行两个操作:花费\(1\)个金币,将\(a_i\)涂成红色;选择一个红色的\(a_i>0\),将\(a_{i-1}\)或\(a_{i+1}\)涂成红色,同时\(a_i\)减\(1\)。输出金......
  • F. 乘积最大(题解)
    题目今年是国际数学联盟确定的“2000——世界数学年”,又恰逢我国著名数学家华罗庚先生诞辰90周年。在华罗庚先生的家乡江苏金坛,组织了一场别开生面的数学智力竞赛的活动,你的一个好朋友XZ也有幸得以参加。活动中,主持人给所有参加活动的选手出了这样一道题目:设有一个长度为......
  • [ARC135D] Add to Square 题解
    题目链接点击打开链接题目解法很牛的题!!!先考虑一步很牛的转化:把矩阵黑白染色,且\(i+j\)为奇数的位置的值取反,每次操作变为左上右下\(+v\),左下右上\(-v\)这样有啥好处?操作不会使行和列的和改变考虑答案的下界显然是:\(\max\{\)行的绝对值之和,列的绝对值之和\(\}\)这里给出......
  • luogu2647题解
    首先这道题没有规定选几个。一开始我以为是全选,然后想了个贪心才发现看错了。那我们先来假设选\(m\)个。这个题的每个物品都会影响后面物品的收益,不好看。由于每个物品\(i\)对后选的其他物品减少的收益都是\(R_i\),因此我们在保证总价值不变的情况下转化一下,把该物品的价......