P4867 Gty的妹子序列 题解

题目链接:Gty的妹子序列

关于莫队的一些教程

类似题教学

关于本题,重新提一下关于普通莫队最优块长,在教学当中提到了每次移动为 \(B\) 大小,一共会移动 \(q\) 次,那么复杂度为 \(qB\),我们考虑取块长为 \(\frac{n}{\sqrt{q}}\),那么我们可以得到最佳复杂度为:\(O(n\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 = 1e6 + 10;
constexpr int MX = 1e5;
int siz;
constexpr int SIZE = sqrt(MX);
constexpr int CNT = (MX + SIZE - 1) / SIZE + 1;
#define pos(i) ((i-1)/siz+1)
#define s(i) ((i-1)*SIZE+1)
#define e(i) min(i*SIZE,MX)
#define valPos(i) ((i-1)/SIZE+1)

struct Mo
{
    int l, r, id, a, b;

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

int n, q;
int cnt[N], valColor[CNT];

inline void add(const int val)
{
    const int curr = valPos(val);
    if (cnt[val]++ == 0)valColor[curr]++;
}

inline void del(const int val)
{
    const int curr = valPos(val);
    if (--cnt[val] == 0)valColor[curr]--;
}

inline int query(const int l, const int r)
{
    const int L = valPos(l), R = valPos(r);
    int ans = 0;
    if (L == R)
    {
        forn(i, l, r)ans += cnt[i] != 0;
        return ans;
    }
    forn(i, l, e(L))ans += cnt[i] != 0;
    forn(i, s(R), r)ans += cnt[i] != 0;
    forn(i, L+1, R-1)ans += valColor[i];
    return ans;
}

int val[N], ans[N];

inline void solve()
{
    read(n, q);
    siz = n / sqrt(q);
    forn(i, 1, n)read(val[i]);
    forn(i, 1, q)
    {
        auto& [l,r,id,a,b] = node[i];
        read(l, r, a, b), id = i;
    }
    sortArr(node, q);
    int l = 1, r = 0;
    forn(i, 1, q)
    {
        const auto [L,R,id,a,b] = node[i];
        while (l < L)del(val[l++]);
        while (l > L)add(val[--l]);
        while (r > R)del(val[r--]);
        while (r < R)add(val[++r]);
        ans[id] = query(a, b);
    }
    forn(i, 1, q)write(endl, ans[i]);
}

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(n\sqrt{m}+m\sqrt{n}),莫队修改\ +\ 查询的复杂度 \]

posted @ 2024-03-22 16:45  Athanasy  阅读(11)  评论(0编辑  收藏  举报