P3730 曼哈顿交易 题解
题目链接:曼哈顿交易
比较容易想的题,观察下首先不带修改,考虑维护的东西:次数作为权值,这玩意很显然很难在线维护,考虑下离线算法。看到这种和次数有关的权值,典型的单点加入和删除是非常好找到变化的,那么就莫队离线算法吧。
考虑下莫队如何来做,涉及到权值第 \(k\) 大,解决方法挺多的,但时限容易知道莫队需要 \(O(1)\) 修改,不能带 \(\log\),但查询显然至多 \(q\) 次,记住了,需要 \(O(1)\) 修改,\(O(\sqrt{n})\) 查询用于平衡复杂度的往往都是值域分块。对次数作为权值的值域进行分块,分别维护每个块区间和,单点和,查询第 \(k\) 大,只需要先枚举权值块一直到能确定是哪个值域的权值块,再单点枚举,跟主席树上二分思路是一致的。然后值域比较大,想用桶记录次数离散化一下就好了。这里为了更好地快速拿到次数最多的作为次数作为的值域的上界,就是出现次数最多元素的次数,我们可以用个 \(map\) 同时去重,排序,在插入时找次数最大值。剩下的莫队单修就很好写了,去掉原次数的贡献,增加新次数以后,单点修改它对应的块的信息即可。可以预处理出序列块和值域块的基本信息,剩下的看代码注释即可。
参照代码
#include <bits/stdc++.h>
//#pragma GCC optimize("Ofast,unroll-loops")
#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 = 1e5 + 10;
int cnt[N], Cnt_cnt[N], Sum_Cnt_cnt[N]; //次数,值域块的单点次数出现的次数数组,值域块的块区间次数出现的次数的区间和数组
int pos[N], valPos[N]; //每个位置序列块id,值域块id
int s[N], e[N]; //值域块的起点和终点
int idxSize, valSize; //序列块大小,值域块大小
int idxCnt, valCnt; //序列块数量,值域块数量
map<int, int> mp; //离散化,排序,找次数上界
hash2<int, int> mpVal; //记录值->下标
int n, q, mx; //mx为值域上界(权值为次数)
struct Mo
{
int l, r, id, k;
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];
inline int query(int k)
{
//遍历值域块
forn(id, 1, valCnt)
{
if (Sum_Cnt_cnt[id] >= k)
{
//单点遍历
forn(i, s[id], e[id])
{
if (Cnt_cnt[i] >= k)return i;
k -= Cnt_cnt[i];
}
}
else k -= Sum_Cnt_cnt[id];
}
return -1;
}
inline void add(const int val)
{
int& oldCnt = cnt[val];
Cnt_cnt[oldCnt]--;
Sum_Cnt_cnt[valPos[oldCnt]]--;
oldCnt++;
Cnt_cnt[oldCnt]++;
Sum_Cnt_cnt[valPos[oldCnt]]++;
}
inline void del(const int val)
{
int& oldCnt = cnt[val];
Cnt_cnt[oldCnt]--;
Sum_Cnt_cnt[valPos[oldCnt]]--;
oldCnt--;
Cnt_cnt[oldCnt]++;
Sum_Cnt_cnt[valPos[oldCnt]]++;
}
int a[N];
int idx;
int ans[N];
inline void solve()
{
cin >> n >> q;
idxSize = sqrt(n);
idxCnt = (n + idxSize - 1) / idxSize;
forn(i, 1, n)cin >> a[i], mp[a[i]]++, uMax(mx, mp[a[i]]), pos[i] = (i - 1) / idxSize + 1; //顺便找到权值上界(次数最多为多少)
for (const auto val : mp | views::keys)mpVal[val] = ++idx;
valSize = sqrt(mx);
valCnt = (mx + valSize - 1) / valSize;
forn(i, 1, mx)valPos[i] = (i - 1) / valSize + 1;
forn(i, 1, valCnt)s[i] = (i - 1) * valSize + 1, e[i] = i * valSize;
e[valCnt] = mx;
forn(i, 1, n)a[i] = mpVal[a[i]]; //离散化
forn(i, 1, q)
{
auto& [l,r,id,k] = node[i];
cin >> l >> r >> k, id = i;
}
sortArr(node, q);
int l = 1, r = 0;
forn(i, 1, q)
{
auto [L,R,id,k] = node[i];
while (l > L)add(a[--l]);
while (r < R)add(a[++r]);
while (l < L)del(a[l++]);
while (r > R)del(a[r--]);
ans[id] = query(k);
}
forn(i, 1, q)cout << ans[i] << endl;
}
signed int main()
{
Spider
//------------------------------------------------------
int test = 1;
// read(test);
// cin >> test;
forn(i, 1, test)solve();
// while (cin >> n, n)solve();
// while (cin >> test)solve();
}
\[时间复杂度显然为离散化+查询+修改:\ O(n\log{n}+q\sqrt{Cnt_{max}}+q\sqrt{n})
\]