It is also a way to stack problems. Pairing a heap template problem.
#include <cctype> #include <algorithm> #include <cstring> #include <cstdlib> #include < Cstdio> #include <iostream> #define REP (i, L, R) for (int i=l; i<=r; i++) #define CLR (x, C) memset (x, C, sizeof (x) ) #define LL Long long #define MAXN 1000009using namespace Std;inline int read () {int x=0; char Ch=getchar (); while (!isdigit (CH)) Ch=getchar (); while (IsDigit (CH)) x=x*10+ch-' 0 ', Ch=getchar (); return x;} struct Node{int l, r, ch; node () {l=r=ch;}} h[maxn];int N, M, K[MAXN], ROOF[MAXN]; bool Die[maxn];inline int Join (int v, int u) {if (v==u) return v;if (K[v]<k[u]) swap (V, u); h[v].l=u, h[v].r=h[u].ch, h[h [U].ch].l=v, H[u].ch=v;roof[v]=u;return u;} int Top (int v) {return roof[v]==v v:top (Roof[v]);} int ST[MAXN], top;inline int Kill (int v) {die[v]=true; int ans=k[v];if (h[v].ch) {top=0; int t=h[v].ch; while (t) if (h[t]. R) {int k=h[h[t].r].r;int v=h[t].r; h[v].l=h[v].r=h[t].l=h[t].r=0; roof[t]=t, Roof[v]=v;st[++top]=join (t, v); t=k;} else{st[++top]=t; h[t].l=h[t].r=0; roof[t]=t; break;} T=st[top];rep (i, 1, top-1) t=join (T, St[i]);} return ans;} Char S[5];int main () {n=read (); Rep (i, 1, N) k[i]=read (), Roof[i]=i;m=read (), Rep (i, 1, m) {scanf ("%s", s); if (s[0]== ' m ') {i NT V=read (), U=read (); if (!die[v] &&!die[u]) Join (Top (v), Top (U));} else {int V=read (), if (!die[v]) printf ("%d\n", Kill (Top (v))), Else printf ("0\n");}} return 0;}
BZOJ-1455 Roma Games