CF145E Lucky Queries 题解
题目链接:CF 或者 洛谷
前置知识点:序列操作
本文关键词 约定俗称:因为频繁敲最长不下降子序列 和最长不上升子序列 太麻烦了,下文将 这种最长不降子序列用 描述, 这种最长不升子序列用 描述。
这里面只有
难点讲解
如何在线段树上的 pushUp 中去进行计算
如图所示,具体数字数值不做参考,如果我们已经知道了两个节点区间表示的
证明:
先考虑一个特殊情况是否也满足,假如它的
全
考虑全局
那么我们来考虑
-
放在了某个 上,那么将原串分割为 与 ,前者为左区间的串,显然最优为 时,应该取 的数量,后面要求 串这种连续串最长,显而易见是右区间的 。符合我们说的其中一个情况: 。 -
放在某个 上,那么同上分析分割为了 与 ,显然最优的 时为: 。
综上所述,
0-1串 LIS 带翻转操作
现在这玩意带上翻转操作咋整,看到前置知识点那题没,其实
参照代码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; 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 = 1e6 + 10; struct Node { int sum4, sum7, LIS, LDS; int rev, len; } node[N << 2]; #define sum4(x) node[x].sum4 #define sum7(x) node[x].sum7 #define LIS(x) node[x].LIS #define LDS(x) node[x].LDS #define len(x) node[x].len #define rev(x) node[x].rev inline void push_up(const int curr) { sum4(curr) = sum4(ls(curr)) + sum4(rs(curr)); sum7(curr) = sum7(ls(curr)) + sum7(rs(curr)); LIS(curr) = max(LIS(ls(curr)) + sum7(rs(curr)),sum4(ls(curr)) + LIS(rs(curr))); LDS(curr) = max(LDS(ls(curr)) + sum4(rs(curr)),sum7(ls(curr)) + LDS(rs(curr))); } inline void RevTag(const int curr) { sum4(curr) = len(curr) - sum4(curr); sum7(curr) = len(curr) - sum7(curr); swap(LIS(curr),LDS(curr)); rev(curr) ^= 1; } inline void push_down(const int curr) { if (rev(curr)) { RevTag(ls(curr)), RevTag(rs(curr)); rev(curr) = 0; } } int n, q; char a[N]; inline void build(const int curr = 1, const int l = 1, const int r = n) { len(curr) = r - l + 1; const int mid = l + r >> 1; if (l == r) { LIS(curr) = LDS(curr) = 1; sum4(curr) = a[l] == '4'; sum7(curr) = a[l] == '7'; return; } build(ls(curr), l, mid); build(rs(curr), mid + 1, r); push_up(curr); } inline void Rev(const int curr, const int l, const int r, const int s = 1, const int e = n) { if (l <= s and e <= r) { RevTag(curr); return; } const int mid = s + e >> 1; push_down(curr); if (l <= mid)Rev(ls(curr), l, r, s, mid); if (r > mid)Rev(rs(curr), l, r, mid + 1, e); push_up(curr); } string s; inline void solve() { cin >> n >> q; forn(i, 1, n)cin >> a[i]; build(); while (q--) { cin >> s; if (s == "count")cout << LIS(1) << endl; else { int l, r; cin >> l >> r; Rev(1, l, r); } } } 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; }
参照代码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; #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 = 1e6 + 10; struct Node { int cnt0, cnt1; int LIS, LDS; bool rev; } node[N << 2]; #define cnt0(x) node[x].cnt0 #define cnt1(x) node[x].cnt1 #define LIS(x) node[x].LIS #define LDS(x) node[x].LDS #define rev(x) node[x].rev inline void pushUp(const int curr) { cnt0(curr) = cnt0(ls(curr)) + cnt0(rs(curr)); cnt1(curr) = cnt1(ls(curr)) + cnt1(rs(curr)); LIS(curr) = max(cnt0(ls(curr)) + LIS(rs(curr)),LIS(ls(curr)) + cnt1(rs(curr))); LDS(curr) = max(cnt1(ls(curr)) + LDS(rs(curr)),LDS(ls(curr)) + cnt0(rs(curr))); } inline void RevTag(const int curr) { swap(cnt0(curr),cnt1(curr)); swap(LIS(curr),LDS(curr)); rev(curr) = !rev(curr); } inline void pushDown(const int curr) { if (rev(curr)) { RevTag(ls(curr)), RevTag(rs(curr)); rev(curr) = false; } } int n, m; char s[N]; inline void build(const int curr = 1, const int l = 1, const int r = n) { const int mid = l + r >> 1; if (l == r) { cnt0(curr) = s[l] == '4'; cnt1(curr) = s[l] == '7'; LIS(curr) = LDS(curr) = 1; return; } build(ls(curr), l, mid), build(rs(curr), mid + 1, r); pushUp(curr); } inline void Rev(const int curr, const int l, const int r, const int s = 1, const int e = n) { if (l <= s and e <= r) { RevTag(curr); return; } pushDown(curr); const int mid = s + e >> 1; if (l <= mid)Rev(ls(curr), l, r, s, mid); if (r > mid)Rev(rs(curr), l, r, mid + 1, e); pushUp(curr); } string op; inline void solve() { cin >> n >> m; forn(i, 1, n)cin >> s[i]; build(); while (m--) { cin >> op; if (op == "count")cout << LIS(1) << endl; else { int l, r; cin >> l >> r; Rev(1, l, r); } } } 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 IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统