经过一晚的学习,我尝试着自己写了这道题,结果
想知道自己为什么TLE了,可能还是我哪里理解不太行
#include<bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 20;
int n,m,t,root,tot,q;
int fa[maxn],son[maxn][2],size[maxn],va[maxn];
char in[maxn][20];
void push_up(int x)
{
size[x] = 1;
if(son[x][0]) size[x] += size[son[x][0]];
if(son[x][1]) size[x] += size[son[x][1]];
}
void rotate(int x,int &k)
{
int y = fa[x] ;
int z = fa[y] ;
int temp = (son[y][0]==x) ;
if(y==k) k = x;
else
if(son[z][0]==y) son[z][0] = x;
else son[z][1] = x;
son[y][temp^1] = son[x][temp];
fa[son[y][temp^1]] = y;
son[x][temp] = y;
fa[y] = x;fa[x] = z;
push_up(x);push_up(y);
}
void Splay(int x,int &k)
{
while(x!=k)
{
int y = fa[x] ;
int z = fa[y] ;
if(y!=k)
{
if((son[y][0]==x)^(son[z][0]==y)) rotate(x,k);
else rotate(y,k);
}
rotate(x,k);
}
}
void push(int x)
{
if(!root)
{
tot++;root = tot;
son[tot][0]=son[tot][1]=fa[tot]=0;
va[tot]=x;size[tot]++;
return ;
}
int now=root,father=0;
while(1)
{
father = now;
now = son[now][va[now]<x];
if(!now)
{
tot++;fa[tot] = father;
son[tot][0] = son[tot][1] = 0;
size[tot] = 1;va[tot] = x;
son[father][va[father]<x] = tot;
push_up(father);Splay(tot,root);
break;
}
}
}
int find_value(int x)
{
int now = root;
while(1)
{
if(son[now][0]&&x<=size[son[now][0]])
now=son[now][0];
else
{
int temp = (son[now][0]?size[son[now][0]]:0) + 1;
if(x<=temp)
return now;
x -= temp;
now = son[now][1];
}
}
}
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%s",in[i]);
push(i);
}
scanf("%d",&m);
for(int i=1;i<=m;i++)
{
int pos;
scanf("%s %d",in[i+n],&pos);pos++;
int k = find_value(pos);
Splay(k,root);
int k2 = find_value(pos-1);
Splay(k2,son[root][0]);
son[k2][1] = ++tot;
va[tot] = i+n;size[tot] = 1;
fa[tot] = k2;size[k2]++;
}
scanf("%d",&q);
for(int i=1;i<=q;i++)
{
int pos;
scanf("%d",&pos);
int x = find_value(++pos);
Splay(x,root);
printf("%s\n",in[x]);
}
return 0;
}