RE求助
查看原帖
RE求助
767099
WEXI7111楼主2023/3/5 12:49
#include<bits/stdc++.h>
using namespace std;
const int N = 200010;
struct Node
{
    int s[2], v, sz, flag, p; string str;
    void init(int _p, int _v, string _str)
    {
        p = _p, v = _v, str = _str; sz = 1;
    }
}a[N];
int rt, idx;

void pushup(int p)
{
    a[p].sz = a[a[p].s[0]].sz + a[a[p].s[1]].sz + 1;
}

void rotate(int x)
{
    int y = a[x].p, z = a[y].p; bool k = (a[y].s[1] == x);
    a[x].p = z, a[z].s[a[z].s[1] == y] = x;
    a[a[x].s[k ^ 1]].p = y, a[y].s[k] = a[x].s[k ^ 1];
    a[y].p = x, a[x].s[k ^ 1] = y;
    pushup(y), pushup(x);
}

void splay(int x, int t)
{
    while(a[x].p != t)
    {
        int y = a[x].p, z = a[y].p;
        if(z != t) 
            if((a[y].s[1] == x) ^ (a[z].s[1] == y)) rotate(x);
            else rotate(y);
        rotate(x);
    }
    if(!t) rt = x;
}

int getk(int x)
{
    int u = rt; if(a[u].sz < x) return 0;
    while(1)
    {
        if(a[a[u].s[0]].sz >= x) u = a[u].s[0];
        else if(a[a[u].s[0]].sz + 1 == x) {splay(u, 0); return u;}
        else x -= a[a[u].s[0]].sz + 1, u = a[u].s[1];
    }
}

void insert(int x, string str)
{
    int u = rt, p = 0;
    while(u && a[u].v != x) 
        p = u, u = a[u].s[a[u].v < x];
    u = ++ idx;
    if(p) a[p].s[a[p].v < x] = u; a[u].init(p, x, str);
    // cout << u << ' ' << a[u].v << ' ' << a[u].str << endl;
    splay(u, 0);
}

int put(int x, string str)
{
    int u = ++ idx;
    int l = getk(x + 1), r = getk(x + 2);
    splay(r, 0), splay(l, r);
    a[l].s[1] = ++ u;
    a[u].init(l, x, str); splay(u, 0);
}

int main()
{
    int n, m, q; scanf("%d", &n); insert(-1e9, " "); insert(1e9, " ");
    for(int i = 1; i <= n; i ++) 
    {
        string a; cin >> a; 
        insert(i, a);
    }
    scanf("%d", &m);
    while(m --)
    {
        string a; int op; cin >> a; scanf("%d", &op);
        put(op, a);
    }
    scanf("%d", &q);
    while(q --)
    {
        int x; scanf("%d", &x);
        cout << a[getk(x + 2)].str << "\n";
    }
    return 0;
}
2023/3/5 12:49
加载中...