CF1982F Sorting Problem Again 题解

题目链接:CF 或者 洛谷

解题思路

这题放 F 虚高了。做法很多,讲一个比较直观的前提想法:

我们定义个辅助数组 v,满足 v[i]=a[i]a[i1],那么一个直观的想法:

v 数组可能长这样:11110010110001111。我们需要做的很显然就是找到一对 [l,r],使得这个范围内的排序以后,v 变为了 111111111111

一个非常直观的想法就是先找出 L=left0R=right0,即最左边的 0 与 最右边的 0

这里稍微注意一下,v[left0]<v[left01],所以其实我们这个时候要修改的范围其实应该是需要包括 left01 的,否则无论怎么排序,a[left0] 一定在 a[left01] 的右侧不满足题意。所以不妨让:L=left01,R=right0

那么我们难道翻转下 [L,R] 就一定能让中间的所有 01 吗?这次样例给的很充分,观察样例 2 你就会发现一个事实:

当你将 [L,R] 内排序以后,[L+1,R] 一定都是 1,满足 a[i]a[i1],但你观察一下,a[L]a[L1]a[R+1]a[R],这两个是不一定成立的,这取决于以下事实:

不妨设:Min=mina[lr],Max=maxa[lr]。我们很容易知道一个事实,假设答案为 [ansL,ansR],那么一定有

a[1ansL1]Mina[ansR+1,n]Max 成立。所以真正的 ansLansR 还需要往左右两边去确定。

实现方式

关于问题 1,我们可以预处理出 v 数组,而每次修改 a[pos]=val,我们观察到 v 数组至多变化两个位置:v[pos],v[pos+1],那么其实修改就仅仅只是单点修改问题。关于查询 left0right0,这是一个很经典的问题,本身这个 01 并无单调性,但我们可以增加 minVal 属性,或者 sum 属性,这样即具备单调性。

对于 sum 属性,由于都是非负数,那么和区间长度呈正相关,你二分的 check 仅仅是 len?=sum 即可。

对于 min 属性,如果区间出现过 0,就往区间里找(此时 min=0),没有就往外找 0。从左往右或者从右往左二分。

上述过程的实现方式:由于涉及到单修和前后缀上二分。所以常见的我们有这几种思路:

  1. 二分+线段树/树状数组

  2. 线段树/文艺平衡树上二分

  3. 树状数组上倍增

关于第二个问题,我们需要找到前后缀中满足 Min 的最大位置 l,还有 Max 的最小位置 r,而 l+1r1 才是所求,所以可以查第一个不满足点即可。

做法很多:

  1. 二分+区间线段树或者文艺平衡树维护区间最值

  2. 区间线段树或者文艺平衡树维护区间最值,树上二分

  3. 树状数组上倍增

  4. 值域作轴,权值为下标,维护区间最小和最大值,直接做值域上的限制查询,可以用:权值树状数组、权值线段树、普通平衡树等等

其中注意一点,就是可能会出现不存在的 lr,或者 presuf 完全满足。我们可以为了减少特判增加哨兵节点:a[0]=INF,a[n+1]=INFv[0]=v[n+1]=1 即可。

这里使用的是线段树上二分的做法,具体参见代码实现。

参照代码
#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;
#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;
constexpr int INF = 1e9 + 7;
int n, q;
int a[N], v[N]; //v[i]=a[i]>=a[i-1]
//ordMin 为v的区间最小值
//valMin与valMax为a数组的区间最值
struct Node
{
int ordMin;
int valMin, valMax;
} node[N << 2];
#define ordMin(x) node[x].ordMin
#define valMin(x) node[x].valMin
#define valMax(x) node[x].valMax
inline void pushUp(const int curr)
{
valMin(curr) = min(valMin(ls(curr)),valMin(rs(curr)));
valMax(curr) = max(valMax(ls(curr)),valMax(rs(curr)));
ordMin(curr) = min(ordMin(ls(curr)),ordMin(rs(curr)));
}
inline void update(const int curr, const int pos, const int ordVal, const int val, const int l = 0, const int r = n + 1)
{
if (l == r)
{
valMin(curr) = valMax(curr) = val;
ordMin(curr) = ordVal;
return;
}
const int mid = l + r >> 1;
if (pos <= mid) update(ls(curr), pos, ordVal, val, l, mid);
else update(rs(curr), pos, ordVal, val, mid + 1, r);
pushUp(curr);
}
inline void build(const int curr = 1, const int l = 0, const int r = n + 1)
{
if (l == r)
{
valMax(curr) = valMin(curr) = a[l];
ordMin(curr) = v[l];
return;
}
const int mid = l + r >> 1;
build(ls(curr), l, mid);
build(rs(curr), mid + 1, r);
pushUp(curr);
}
//二分出left0
inline int ordLeft(const int curr = 1, const int l = 0, const int r = n + 1)
{
if (l == r) return l;
const int mid = l + r >> 1;
if (ordMin(ls(curr)) != 1) return ordLeft(ls(curr), l, mid);
return ordLeft(rs(curr), mid + 1, r);
}
//二分出right0
inline int ordRight(const int curr = 1, const int l = 0, const int r = n + 1)
{
if (l == r) return l;
const int mid = l + r >> 1;
if (ordMin(rs(curr)) != 1) return ordRight(rs(curr), mid + 1, r);
return ordRight(ls(curr), l, mid);
}
//查找r<=R,且pre>val的第一个下标
inline int valPre(const int curr, const int R, const int val, const int l = 0, const int r = n + 1)
{
const int mid = l + r >> 1;
if (r <= R)
{
if (l == r) return valMax(curr) > val ? l : -1;
if (valMax(ls(curr)) > val) return valPre(ls(curr), R, val, l, mid);
return valPre(rs(curr), R, val, mid + 1, r);
}
const int ansL = valPre(ls(curr), R, val, l, mid);
return ansL != -1 ? ansL : valPre(rs(curr), R, val, mid + 1, r);
}
//查找l>=L,且suf<val的第一个下标
inline int valSuf(const int curr, const int L, const int val, const int l = 0, const int r = n + 1)
{
const int mid = l + r >> 1;
if (L <= l)
{
if (l == r) return valMin(curr) < val ? l : -1;
if (valMin(rs(curr)) < val) return valSuf(rs(curr), L, val, mid + 1, r);
return valSuf(ls(curr), L, val, l, mid);
}
const int ansR = valSuf(rs(curr), L, val, mid + 1, r);
return ansR != -1 ? ansR : valSuf(ls(curr), L, val, l, mid);
}
pii operator+(const pii& L, const pii& R)
{
return pii(min(L.first, R.first), max(L.second, R.second));
}
inline pii query(const int curr, const int l, const int r, const int s = 0, const int e = n + 1)
{
if (l == s and e == r) return pii(valMin(curr),valMax(curr));
const int mid = s + e >> 1;
if (r <= mid) return query(ls(curr), l, r, s, mid);
if (l > mid) return query(rs(curr), l, r, mid + 1, e);
return query(ls(curr), l, mid, s, mid) + query(rs(curr), mid + 1, r, mid + 1, e);
}
inline void getAns()
{
const int L = ordLeft() - 1; //left0-1
if (L == n)
{
cout << -1 << ' ' << -1 << endl;
return;
}
const int R = ordRight(); //right0
const auto [mi,mx] = query(1, L, R); //[L,R]上的Min,Max
const int l = valPre(1, L, mi); //pre>val的第一个下标,也可以不用坐标域限制,直接查找和L取min
const int r = valSuf(1, R, mx); //suf<val的第一个下标,也可以不用坐标域限制,直接查找和R取max
cout << (l == -1 ? L : l) << ' ' << (r == -1 ? R : r) << endl; //不存在就是全部满足
}
inline void solve()
{
cin >> n;
a[0] = -INF, a[n + 1] = INF;
v[0] = v[n + 1] = 1;
forn(i, 1, n) cin >> a[i], v[i] = a[i] >= a[i - 1];
build();
getAns();
cin >> q;
while (q--)
{
int pos, val;
cin >> pos >> val;
a[pos] = val;
v[pos] = a[pos] >= a[pos - 1];
v[pos + 1] = a[pos + 1] >= a[pos];
update(1, pos, v[pos], a[pos]);
update(1, pos + 1, v[pos + 1], a[pos + 1]);
getAns();
}
}
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(qlogn)

posted @   Athanasy  阅读(80)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· 清华大学推出第四讲使用 DeepSeek + DeepResearch 让科研像聊天一样简单!
· 实操Deepseek接入个人知识库
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
点击右上角即可分享
微信分享提示