MLE求助
查看原帖
MLE求助
416242
New_hope楼主2024/3/16 16:51
#include<bits/stdc++.h>
#define _ (int)(1e6 + 4)
using namespace std;

struct io { int rt, dt, lc, rc, val; };
int n, m;
bool del[_];
io a[_];


int merge(int x, int y)
{
    if (!x || !y) return x + y;
    if (a[x].val > a[y].val) swap(x, y);
    a[x].rc = merge(a[x].rc, y);
    if (a[a[x].lc].dt < a[a[x].rc].dt) swap(a[x].lc, a[x].rc);
    a[x].dt = a[a[x].rc].dt + 1;
    return x;
}
void erase(int p)
{
    a[p].rt = a[a[p].lc].rt = a[a[p].rc].rt = merge(a[p].lc, a[p].rc);
    a[p].lc = a[p].rc = a[p].dt = 0;
}
int find(int x)
{
    if (a[x].rt == x) return x;
    return a[x].rt = find(a[x].rt);
}
int main()
{
    cin >> n;
    for (int i = 1; i <= n; i ++) {
        cin >> a[i].val;
        a[i].rt = i;
    }
    cin >> m;
    for (int i = 1; i <= m; i ++) {
        char op; int x, y, px, py;
        cin >> op;
        if (op == 'M') {
            cin >> x >> y;
            if (del[x] || del[y]) continue;
            px = find(x), py = find(y);
            if (px != py) a[px].rt = a[py].rt = merge(x, y);
        }
        else {
            cin >> x; px = find(x);
            if (del[x]) { 
                cout << "0\n";
                continue;
            }
            del[px] = 1;
            cout << a[px].val << "\n";
            erase(px);
        }
    }


    return 0;
}
2024/3/16 16:51
加载中...