题目链接:CF 或者 洛谷
不是很难的题,但在这里提供一个动态开点线段树怎么卡空间卡过去的极致空间处理技巧
全局 \(mex\) 问题,常见的做法就是维护权值树,然后找第一个没有权值的点,可以维护 \(\min\),但本题存在第三个操作,所以不能再去传统地维护 \(\min 或者 \max\) 去辅助二分了。观察到本题的并不是加操作,而是一个数是否存在的一个信息记录,那么很显然可以用一个权值的 \(0/1\) 数组表示集合。那么操作:
-
这个 \(0/1\) 数组的 \([l,r]\) 上的数被覆盖为 \(1\),都存在。
-
这个 \(0/1\) 数组的 \([l,r]\) 上的数被覆盖为 \(0\),都消失。
-
这个 \(0/1\) 数组的 \([l,r]\) 上的数进行翻转,\(0\) 变为 \(1\),\(1\) 变为 \(0\)。
关于区间翻转问题,我们需要一个辅助信息帮助我们二分出这个 \(mex\),而显然之前用的 \(\min\) 或者 \(\max\) 都没法跟第三个操作产生联系。注意到这个是 \(0/1\) 数组,我们可以考虑维护区间和来和区间翻转产生联系。\(区间和=区间长度\) 显然 \(mex\) 不在这个区间内,在在这个区间的右边,否则就继续往这个区间里找。
到此,你会发现特别简单的这题,这几个东西的都很好写。但是这题有限制,\(256mb\),还有值域范围 \(1e18\) 的限制。如果纯粹的动态开点,还带区改,每次大概增加 \(3 \log{1e18}\) 左右的点,那么空间是装不下的。离散化啥的就随便做了,甚至这题 \(ODT\) 也能过。动态开点的文艺平衡树?也能过。但是我就想动态开点的线段树卡过去,怎么卡?
观察到本题由于有 \(0\) 覆盖操作,你不觉得这玩意和 \(ODT\) 的那个颜色均摊很像吗?我们可以把这次操作以后导致变为空节点的点删除了,并且回收标记,用于新节点的动态开点使用?这玩意咋写?写过可持久化文艺平衡树或者线段树分裂与合并的对这个概念一定不陌生,但这里放到修改当中我们该如何删?
注意到线段树的特殊:
父亲节点的信息是包含儿子信息的,如果 \([l,r]\) 没有数,那么 \([l,mid]\) 与 \([mid+1,r]\) 上也一定是空的,当然我们不可能直接 \(dfs\) 线段树去删除,我们可以只删除根,然后当这个根要作为新节点的空间分配时,顺便把 \(left\) 和 \(right\) 也丢到删除栈中即可------延迟删除。
删除时机:显然是在修改当中,如果最终的节点或者往上 \(pushUp\) 后为空的节点都应该删除。但很显然,删除上面的节点的根是包含下面节点的子树的,所以我们可以开一个布尔数组,防止重复入删除栈以便回收利用。最后关于一些空间卡常,像线段树树上二分还带 \(pushDown\) 开空间这种,我们就最好不要二分到单点了,如果当前的区间上没有数,那么显然答案就是 \(l\) 了,这样可以防止开很多无用的空节点。当然,我们能用布尔变量的地方尽量就别用 \(int\) 了。
参照代码
#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;
}