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();
}
\]
P3730 曼哈顿交易 题解的更多相关文章
- 洛谷 P3730 曼哈顿交易
https://www.luogu.org/problem/show?pid=3730 题目背景 will在曼哈顿开了一家交易所,每天,前来买卖股票的人络绎不绝. 现在,will想要了解持股的情况.由 ...
- AC日记——曼哈顿交易 洛谷 P3730
曼哈顿交易 思路: 都是套路: 代码: #include <cmath> #include <cstdio> #include <cstring> #include ...
- [SCOI2013]摩托车交易 题解
思路分析 为了让交易额尽量大,显然我们需要尽量多地买入.对于每个城市,到达这个城市时携带的黄金受到几个条件的影响:之前卖出的黄金,之前能买入的最多的黄金,前一个城市到当前城市的路径上的最小边权.既然不 ...
- POJ 1984 Navigation Nightmare 带全并查集
Navigation Nightmare Description Farmer John's pastoral neighborhood has N farms (2 <= N <= ...
- Codeforces Round #345 (Div. 2) C (multiset+pair )
C. Watchmen time limit per test 3 seconds memory limit per test 256 megabytes input standard input o ...
- Codeforces Round #345 (Div. 1) A - Watchmen 容斥
C. Watchmen 题目连接: http://www.codeforces.com/contest/651/problem/C Description Watchmen are in a dang ...
- D-Distance_2019牛客暑期多校训练营(第八场)
题目链接 Distance 题意 1<=nmh,q<=1e5 q个操作 1 x y z往坐标里加入一个点 2 x y z查询距离该点最近的点的距离(曼哈顿距离) 题解 做法一 将要插入的点 ...
- HDU3085(双向BFS+曼哈顿距离)题解
Nightmare Ⅱ Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- Codeforces 1093G题解(线段树维护k维空间最大曼哈顿距离)
题意是,给出n个k维空间下的点,然后q次操作,每次操作要么修改其中一个点的坐标,要么查询下标为[l,r]区间中所有点中两点的最大曼哈顿距离. 思路:参考blog:https://blog.csdn.n ...
- toodifficult 题解
名字听起来十分厉害啊...一道lzz的提交答案题. 提答题,我们看看题目,给出一个解密程序,叫你加密. 每个点有一个加密的sample和一些要加密的文本. 从题目中我们可以得到一些信息: 加密后一般为 ...
随机推荐
- CodeCraft-21 and Codeforces Round #711 (Div. 2) A~C 个人题解
补题链接:Here 1498A. GCD Sum 题意:给定一个 gcdSum 操作:\(gcdSum(762) = gcd(762,7 + 6 + 2) = gcd(762,15) = 3\) 请问 ...
- IDEA用上这十大插件绝对舒服
本文翻译自国外论坛 medium,原文地址:https://medium.com/@xjpp22/top-10-plugins-for-intellij-idea-you-dont-want-to-m ...
- Mathpix:屏幕截图 ➡ latex 公式,一键转换
安利一天能免费使用 10 次且好用的工具 Mathpix.
- Blazor开发小游戏?趁热打铁上!!!
大家好,我是沙漠尽头的狼. 网站使用Blazor重构上线一天了,用Blazor开发是真便捷,空闲时间查查gpt和github,又上线一个 正则表达式在线验证工具 和几个在线小游戏,比如 井字棋游戏.扫 ...
- [java] - 获取上传到服务器上的文件路径
request.getSession().getServletContext().getRealPath("upload/" );
- Go-值传递&引用传递
值类型和引用类型 值类型关注其值 引用类型关注其指针 值类型和引用类型区别在于传递值的时候拷贝的对象不同,值传递拷贝是变量的值,引用传递拷贝的是变量的指针 拷贝 -- 传递值 赋值 函数调用 初始化 ...
- [转帖]nginx 剖析 request_time和upstream_response_time的误区、区别
https://cloud.tencent.com/developer/article/1767981 首先,澄清一个误区 upstream_response_time必须在upstream配置时才能 ...
- [转帖]《Linux性能优化实战》笔记(八)—— 内存是怎么工作的
一. 内存映射 我们通常所说的内存容量,指的是物理内存.物理内存也称为主存,大多数计算机用的主存都是动态随机访问内存(DRAM).只有内核才可以直接访问物理内存.那么,进程要访问内存时,该怎么办呢? ...
- [转帖]What is Pstate
https://www.jianshu.com/p/342480d917e3 When someone refers to a P-state, generally only the frequenc ...
- CPU算力提升与实际性能提升的关系
关于SPEC2006CPU和RedisBenchmark的理解 最近研究过硬件CPU的性能和Redis这样单线程重IO服务 突然想对比一下CPU算力提升占Redis性能提升的比率情况 性能很大程度由C ...