在本地的结果和你谷上的不一样……
#include <bits/stdc++.h>
#define int long long
#define PII pair<int, int>
#define x first
#define y second
#define FH signed
using namespace std;
const int mod1 = 998244353, mod2 = 1e9 + 7, INF = 0x3f3f3f3f3f3f3f3f;
const int N = 1e5 + 10;
int n, m, root, idx;
int pos[N];
struct node
{
int l, r;
int key, val;
int num;
int size;
} tr[N];
int newnode(int key, int num)
{
idx ++;
tr[idx].l = 0;
tr[idx].r = 0;
tr[idx].size = 1;
tr[idx].key = key;
tr[idx].num = num;
tr[idx].val = rand();
return idx;
}
void updata(int u)
{
tr[u].size = tr[tr[u].l].size + tr[tr[u].r].size + 1;
}
void split_key(int root, int key, int& x, int& y)
{
if(root == 0)
{
x = y = 0;
return ;
}
if(tr[root].key <= key)
x = root,
split_key(tr[root].r, key, tr[root].r, y);
else
y = root,
split_key(tr[root].l, key, x, tr[root].l);
updata(root);
}
void split_size(int root, int size, int& x, int& y)
{
if(root == 0)
{
x = y = 0;
return ;
}
if(tr[tr[root].l].size + 1 <= size)
x = root,
split_size(tr[root].r, size - tr[tr[root].l].size - 1, tr[root].r, y);
else
y = root,
split_size(tr[root].l, size, x, tr[root].l);
updata(root);
}
int merge(int x, int y)
{
if(!x || !y) return x + y;
if(tr[x].val < tr[y].val)
{
tr[x].r = merge(tr[x].r, y);
updata(x);
return x;
}
else
{
tr[y].l = merge(x, tr[y].l);
updata(y);
return y;
}
}
FH main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
for(int i = 1; i <= n; i ++)
{
int a;
cin >> a;
root = merge(root, newnode(i, a));
pos[a] = i;
}
int minn = 1, maxx = n;
while(m --)
{
string str;
cin >> str;
if(str[0] == 'T')
{
int s;
cin >> s;
int t1, t2, t3;
split_key(root, pos[s], t1, t2);
split_size(t1, tr[t1].size - 1, t1, t3);
tr[t3].key = -- minn;
pos[tr[t3].num] = minn;
root = merge(t3, merge(t1, t2));
}
if(str[0] == 'B')
{
int s;
cin >> s;
int t1, t2, t3;
split_key(root, pos[s], t1, t2);
split_size(t1, s - 1, t1, t3);
tr[t3].key = ++ maxx;
pos[tr[t3].num] = maxx;
root = merge(merge(t1, t2), t3);
}
if(str[0] == 'I')
{
int s, t;
cin >> s >> t;
if(t == 0) continue;
int t1, t2, n1, n2;
if(t > 0)
{
int t3;
split_key(root, pos[s], t1, t2);
split_size(t2, 1, t3, t2);
t1 = merge(t1, t3);
split_size(t1, tr[t1].size - 2, t1, n1);
split_size(n1, 1, n1, n2);
swap(tr[n1].key, tr[n2].key);
swap(pos[tr[n1].num], pos[tr[n2].num]);
root = merge(merge(t1, merge(n2, n1)), t2);
}
else
{
split_key(root, pos[s], t1, t2);
split_size(t1, tr[t1].size - 2, t1, n1);
split_size(n1, 1, n1, n2);
swap(tr[n1].key, tr[n2].key);
swap(pos[tr[n1].num], pos[tr[n2].num]);
root = merge(merge(t1, merge(n2, n1)), t2);
}
}
if(str[0] == 'A')
{
int s;
cin >> s;
int t1, t2;
split_key(root, pos[s], t1, t2);
cout << tr[t1].size - 1 << "\n";
root = merge(t1, t2);
}
if(str[0] == 'Q')
{
int s;
cin >> s;
int t1, t2, t3;
split_size(root, s, t1, t2);
split_size(t1, s - 1, t1, t3);
cout << tr[t3].num << "\n";
root = merge(merge(t1, t3), t2);
}
}
return 0;
}