#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
struct Treap { int val, ch[2], sz, p; };
Treap t[N];
int root, tot = 0;
void renew(int x) { t[x].sz = t[t[x].ch[0]].sz + t[t[x].ch[1]].sz + 1; }
int New(int val)
{
tot++;
t[tot] = {val, {0, 0}, 1, rand()};
return tot;
}
void split(int tree, int k, int &x, int &y)
{
if(!tree) { x = y = 0; return; }
if(t[tree].val <= k)
{
x = tree;
split(t[tree].ch[1], k, t[tree].ch[1], y);
}
else
{
y = tree;
split(t[tree].ch[0], k, x, t[tree].ch[0]);
}
renew(tree);
}
int merge(int x, int y)
{
if(!x) return y;
if(!y) return x;
if(t[x].p <= t[y].p)
{
t[x].ch[1] = merge(t[x].ch[1], y);
renew(x);
return x;
}
else
{
t[y].ch[0] = merge(x, t[y].ch[0]);
renew(y);
return y;
}
}
void op1(int val)
{
int x, y;
split(root, val, x, y);
root = merge(merge(x, New(val)), y);
}
void print(int tree)
{
if(!tree) return ;
print(t[tree].ch[0]);
cout << t[tree].val << " ";
print(t[tree].ch[1]);
}
void op2(int val)
{
int x, y, z;
split(root, val, x, z);
split(x, val-1, x, y);
if(y != 0)
y = merge(t[y].ch[0], t[y].ch[1]);
root = merge(merge(x, y), z);
}
int op3(int val)
{
int x, y, ans = 0;
split(root, val-1, x, y);
ans = t[x].sz+1;
root = merge(x, y);
return ans;
}
int op4(int tree, int x)
{
int ls = t[t[x].ch[0]].sz;
if(x == ls+1)
return t[tree].val;
else if(x < ls+1)
return op4(t[tree].ch[0], x);
else
return op4(t[tree].ch[1], x-(ls+1));
}
int op5(int a)
{
int x, y;
split(root, a-1, x, y);
int i = x;
while(t[i].ch[1]) i = t[i].ch[1];
int ans = t[i].val;
root = merge(x, y);
return ans;
}
int op6(int a)
{
int x, y;
split(root, a, x, y);
int i = y;
while(t[i].ch[0]) i = t[i].ch[0];
int ans = t[i].val;
root = merge(x, y);
return ans;
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
srand(time(0));
int n;
cin >> n;
for(int i=1;i<=n;i++)
{
int op, x;
cin >> op >> x;
if(op == 1) op1(x);
if(op == 2) op2(x);
if(op == 3) cout << op3(x) << '\n';
if(op == 4) cout << op4(root, x) << '\n';
if(op == 5) cout << op5(x) << '\n';
if(op == 6) cout << op6(x) << '\n';
}
return 0;
}
[记录](https: