首页 > 其他分享 >P4113 [HEOI2012] 采花 题解

P4113 [HEOI2012] 采花 题解

时间:2024-02-14 21:44:06浏览次数:22  
标签:typedef ch P4113 int 题解 HEOI2012 template include define

题目链接:采花

这题数据加强到卡了 \(2e6\) 的可持久化线段树在线做法,先给只 tle 了最后一个点的代码:

卡常参照代码
#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;
}

namespace fasI
{
    constexpr int BF_SIZE = 1 << 12;
    bool IOerr = false;

    inline char nc()
    {
        static char buf[BF_SIZE], *p1 = buf + BF_SIZE, *pend = buf + BF_SIZE;
        if (p1 == pend)
        {
            p1 = buf;
            pend = buf + fread(buf, 1, BF_SIZE,stdin);
            if (pend == p1)
            {
                IOerr = true;
                return -1;
            }
        }
        return *p1++;
    }

    inline bool bla(const char ch) { return ch == ' ' || ch == '\n' || ch == '\r' || ch == '\t'; }

    template <typename T>
    void Rd(T& x)
    {
        char ch;
        while (bla(ch = nc()));
        T sign = 1;
        if (ch == '-')sign = -1, ch = nc();
        for (x = ch - '0'; (ch = nc()) >= '0' && ch <= '9'; x = x * 10 + ch - '0');
        x *= sign;
    }
#undef BF_SIZE
}

using namespace fasI;
constexpr int N = 2e6 + 10;

struct Node
{
    int left, right;
    int cnt;
} node[N << 5];

#define left(x) node[x].left
#define right(x) node[x].right
#define cnt(x) node[x].cnt
int cnt;
int root[N];
int n, c, m;

inline void add(const int pre, int& curr, const int pos, const int val, const int l = 1, const int r = n)
{
    node[curr = ++cnt] = node[pre];
    cnt(curr) += val;
    const int mid = l + r >> 1;
    if (l == r)return;
    if (pos <= mid)add(left(pre),left(curr), pos, val, l, mid);
    else add(right(pre),right(curr), pos, val, mid + 1, r);
}

inline int query(const int L, const int R, const int pos, const int l = 1, const int r = n)
{
    if (!L and !R)return 0;
    if (l == r)return cnt(R) - cnt(L);
    const int mid = l + r >> 1;
    int rightCnt = cnt(right(R)) - cnt(right(L));
    if (pos <= mid)return rightCnt + query(left(L),left(R), pos, l, mid);
    return query(right(L),right(R), pos, mid + 1, r);
}

int pre[N], pre_pre[N];
int x;

inline void solve()
{
    Rd(n), Rd(c), Rd(m);
    forn(i, 1, n)
    {
        Rd(x);
        root[i] = root[i - 1];
        if (pre[x])add(root[i], root[i], pre[x], -1);
        if (pre_pre[x])add(root[i], root[i], pre[x] = pre_pre[x], 1);
        pre_pre[x] = i;
    }
    while (m--)
    {
        int l, r;
        Rd(l), Rd(r);
        write(endl, query(root[l - 1], root[r], l));
    }
}

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;
}

这玩意挺经典的,难怪这题给离线,题目翻译下,就是问区间上有多少种数的数量 \(\ge 2\),首先在线可持久化线段树维护一下前驱贡献类似 HH的项链。不过卡常了,因为涉及到删除,所以预处理应该是 \(2\) 倍原先建树复杂度。

讲讲离线做法,扫描线处理下查询,从左往右修改,每加入一个数,如果原先有前,我们去掉前驱的贡献,并且我们开一个桶,当然这里由于只涉及到去区间上只有两个数,其实我们再记录前驱的前驱就能表示出上一个贡献位置,如果这题改为区间内该数的数量 \(\ge k\),可以直接开桶找前面第 \(k\) 个位置即为贡献位置。将贡献位置平移一位,用新的前第 \(2\) 个数代替原有的贡献,树状数组上最终查询即可,读入比较大,可以考虑快读或者究极快读。

参照代码
#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 = 2e6 + 10;
int bit[N];
int n, c, m;

inline void add(int x, const int val)
{
    while (x <= n)bit[x] += val, x += lowBit(x);
}

inline int query(int x)
{
    int ans = 0;
    while (x)ans += bit[x], x -= lowBit(x);
    return ans;
}

vector<pii> seg[N];
int pre[N], pre_pre[N];
int a[N];
int ans[N];

inline void solve()
{
    read(n, c, m);
    forn(i, 1, n)read(a[i]);
    forn(i, 1, m)
    {
        int l, r;
        read(l, r);
        seg[r].emplace_back(l, i);
    }
    forn(r, 1, n)
    {
        if (pre[a[r]])add(pre[a[r]], -1);
        if (pre_pre[a[r]])pre[a[r]] = pre_pre[a[r]], add(pre[a[r]], 1);
        pre_pre[a[r]] = r;
        for (const auto [l,id] : seg[r])ans[id] = query(r) - query(l - 1);
    }
    forn(i, 1, m)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+m)\log{n}) \]

标签:typedef,ch,P4113,int,题解,HEOI2012,template,include,define
From: https://www.cnblogs.com/Athanasy/p/18015652

相关文章

  • LOJ #2876. 「JOISC 2014 Day2」水壶 题解
    DescriptionJOI君所居住的IOI市以一年四季都十分炎热著称。IOI市被分成\(H\)行,每行包含\(W\)块区域。每个区域都是建筑物、原野、墙壁之一。IOI市有\(P\)个区域是建筑物,坐标分别为\((A_1,B_1),\)\((A_2,B_2),\)\(\ldots,\)\((A_P,B_P)\)。JOI君只能进入建......
  • CF1931F Chat Screenshots 另一种题解
    题目链接:CF或者洛谷本题拓扑排序不再赘述,来说说字符串哈希怎么做这题。本篇以另一种角度剖析题目背景,并不追求最优,例如有些地方其实可以暴力判断,主要以构造的角度阐述,顺便感谢灵茶山的朋友的讨论。结论三个串及其以上必定能构造出最初的那个串。下面进行证明:首先一个串,显......
  • P8683题解
    首先,看题目描述,给定N个加号,与M个减号,要你求后缀表达式最大值,实际上就是求这些符号和数字排列起来的最大值。这题很容易让人想到贪心。但是呢,又怎么个贪心法呢?很容易看出来,我们可以先用sort进行这么一个排序,之后,我们对于前N大的数加起来,对于剩下的数就减去,于是代码大体就......
  • 麦肯锡问题解决流程-为希望提升水平的产品经理量身定制
    您是否想知道世界上最成功的产品经理如何始终如一地提供不仅满足而且超出预期的解决方案?秘密可能就在于世界上最负盛名的咨询公司之一麦肯锡公司所磨练的方法论。本文深入探讨了麦肯锡的问题解决流程,该流程专为希望提升水平的产品经理量身定制。01.麦肯锡方法:产品管理风......
  • SP34020 ADAPET - Ada and Pet 题解
    题目传送门前置知识前缀函数与KMP算法解法经检验样例,我们发现\(|S|k\)并不是最优答案。考虑利用luoguP4391[BOI2009]RadioTransmission无线传输结论的逆命题,首先必须要有一个完整的\(S\),然后将\(|S|-next_{S}\)复制\(k-1\)次即可。故\(|S|+(|S|-next_{|S|}......
  • P5350 序列 题解
    题目链接:P5350序列比较不难的可持久化文艺平衡树?有道弱化版:数组操作不过弱化版没有随机数据,很显然ODT会直接被卡,这题数据随机倒是能用ODT做一下,而且码量也比较小,可以自己写写,或者参照我给的弱化版我给了这题部分操作的ODT写法。我们还是来讲最实用的可持久化文艺平衡树......
  • UVA12467 Secret Word 题解
    题目传送门前置知识前缀函数与KMP算法解法考虑将\(S\)翻转后得到\(S'\),然后就转化为求\(S'\)的一个最长子串使得其是\(S\)的前缀。使用KMP求解即可。代码#include<bits/stdc++.h>usingnamespacestd;#definelllonglong#defineullunsignedlonglong#d......
  • WGOI R1 真夏飞焰 题解.18014664
    【题目大意】给定序列\(a\),我们定义序列\(a,b\)是「\(k\)相似」的,当且仅当对于\(a\)中每一个四元组\((l_1,r_1,l_2,r_2)\),若满足\(r_1-l_1+1=r_2-l_2+1\lek,l_2=r_1+1,a_{l_1\ldotsr_1}=a_{l_2\ldotsr_2}\),则有\(b_{l_1\ldotsr_1}=b_{l_2\ldot......
  • CF609D题解
    Gadgetsfordollarsandpounds题目传送门题解给一个单\(\log\)题解。“求最早完成买\(k\)样东西的天数。”——很明显的二分答案。在检验函数中,我们应当把前\(k\)小的物品费用之和与总金额作比较,其它题解大多使用直接排序的方法,于是就多了一只\(\log\)。其实没有必......
  • P4559 [JSOI2018] 列队 题解
    题目链接:列队半年前mark的题,结果现在一下子就会做了。顺便写写我的手玩过程和复杂度说明。考虑比较特殊的情况:比较特殊的,发现从黑色到红色区间我们无论咋选择,由于\(\left|a_{right}-a_{left}\right|\),这玩意如果\(right\)表示红色的一边,那么这个绝对值可以直接拆掉,那么......