CF817F MEX Queries 题解
题目链接: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 = 1e5 + 10; struct Node { int left, right; bool tag[3]; ull cnt; } node[N << 6]; #define left(x) node[x].left #define right(x) node[x].right #define cnt(x) node[x].cnt #define cov0(x) node[x].tag[0] #define cov1(x) node[x].tag[1] #define rev(x) node[x].tag[2] int cnt; int mx; stack<int> del; bool vis[N << 6]; constexpr ull MEX = 1e18; inline void Del(int& curr) { cov0(curr) = cov1(curr) = rev(curr) = false; cnt(curr) = 0; if (vis[curr]) { curr = 0; return; } del.push(curr), vis[curr] = true; curr = 0; } inline void newNode(int& curr) { if (!curr) { if (del.empty())curr = ++cnt; else { curr = del.top(), del.pop(); vis[curr] = false; if (left(curr))Del(left(curr)); if (right(curr))Del(right(curr)); } } } struct { static void cover(const int curr, const ull len, const int val) { cnt(curr) = val * len; val ? cov0(curr) = false, cov1(curr) = true : cov1(curr) = false, cov0(curr) = true; rev(curr) = false; } static void reverse(const int curr, const ull len) { cnt(curr) = len - cnt(curr); rev(curr) ^= 1; } } tag; inline void pushUp(int& curr) { cnt(curr) = cnt(left(curr)) + cnt(right(curr)); if (!cnt(curr))Del(curr); } inline void pushDown(const int curr, const ull l, const ull r) { newNode(left(curr)), newNode(right(curr)); const ull mid = l + r >> 1; const ull leftLen = mid - l + 1, rightLen = r - mid; if (cov0(curr) or cov1(curr)) { const int val = cov1(curr) ? 1 : 0; tag.cover(left(curr), leftLen, val), tag.cover(right(curr), rightLen, val); cov0(curr) = cov1(curr) = false; } if (rev(curr))tag.reverse(left(curr), leftLen), tag.reverse(right(curr), rightLen); rev(curr) = false; } inline void Update(int& curr, const ull l, const ull r, const bool isCover, const short val = 0, const ull s = 1, const ull e = MEX + 1) { newNode(curr); const ull len = e - s + 1; if (l <= s and e <= r) { isCover ? tag.cover(curr, len, val) : tag.reverse(curr, len); if (!cnt(curr))Del(curr); return; } pushDown(curr, s, e); const ull mid = s + e >> 1; if (l <= mid)Update(left(curr), l, r, isCover, val, s, mid); if (r > mid)Update(right(curr), l, r, isCover, val, mid + 1, e); pushUp(curr); } inline ull Query(const int curr, const ull l = 1, const ull r = MEX + 1) { if (!cnt(curr) or l == r)return l; pushDown(curr, l, r); if (!cnt(left(curr)))Del(left(curr)); if (!cnt(right(curr)))Del(right(curr)); const ull mid = l + r >> 1; if (const ull leftLen = mid - l + 1; cnt(left(curr)) != leftLen)return Query(left(curr), l, mid); return Query(right(curr), mid + 1, r); } int n; int root; ull a[N]; inline void solve() { cin >> n; while (n--) { int op; ull l, r; cin >> op >> l >> r; if (op == 1 or op == 2)Update(root, l, r, true, op == 1); else Update(root, l, r, false); cout << Query(root) << endl; } } 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 接口并集成到在线客服系统