CF1982F Sorting Problem Again 题解
题目链接:CF 或者 洛谷
解题思路
这题放
我们定义个辅助数组
一个非常直观的想法就是先找出
这里稍微注意一下,
那么我们难道翻转下
当你将
不妨设:
实现方式
关于问题
对于
对于
上述过程的实现方式:由于涉及到单修和前后缀上二分。所以常见的我们有这几种思路:
关于第二个问题,我们需要找到前后缀中满足
做法很多:
-
-
-
-
,可以用: 。
其中注意一点,就是可能会出现不存在的
这里使用的是线段树上二分的做法,具体参见代码实现。
参照代码
#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; }
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· 清华大学推出第四讲使用 DeepSeek + DeepResearch 让科研像聊天一样简单!
· 实操Deepseek接入个人知识库
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库