# [BZOJ 2594] [Wc2006]水管局长数据加强版 【LCT】

### 代码

#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>

using namespace std;

{
char c = getchar();
while (c < '0' || c > '9') c = getchar();
Num = c - '0'; c = getchar();
while (c >= '0' && c <= '9')
{
Num = Num * 10 + c - '0';
c = getchar();
}
}

const int MaxN = 100000 + 5, MaxM = 1000000 + 5, MaxT = 1100000 + 5, MaxQ = 100000 + 5;

int n, m, q, Top, Tot;
int Father[MaxT], Son[MaxT][2], V[MaxT], T[MaxT], f[MaxN], Size[MaxN], Ans[MaxQ];

bool isRoot[MaxT], Rev[MaxT];

struct ES
{
int u, v, w, Idx;
bool Del;
} E[MaxM];

struct QR
{
int f, x, y, Pos;
} Q[MaxQ];

inline bool CmpW(ES e1, ES e2)
{
return e1.w < e2.w;
}

inline bool CmpIdx(ES e1, ES e2)
{
return e1.Idx < e2.Idx;
}

inline bool CmpUV(ES e1, ES e2)
{
return (e1.u < e2.u) || (e1.u == e2.u && e1.v < e2.v);
}

int FindIdx(int x, int y)
{
if (x > y) swap(x, y);
int l, r, mid;
l = 1; r = m;
while (l <= r)
{
mid = (l + r) >> 1;
if (E[mid].u == x && E[mid].v == y) break;
if ((E[mid].u < x) || (E[mid].u == x && E[mid].v < y)) l = mid + 1;
else r = mid - 1;
}
return mid;
}

inline int Find(int x)
{
int i, j, k;
j = x;
while (j != f[j]) j = f[j];
i = x;
while (i != j)
{
k = i;
i = f[i];
f[k] = j;
}
return j;
}

inline void UN(int x, int y)
{
if (Size[x] == Size[y]) ++Size[x];
if (Size[x] > Size[y]) f[y] = x;
else f[x] = y;
}

/********************* LCT Begin *********************/

inline int gmax(int a, int b) {return V[a] > V[b] ? a : b;}

inline void Update(int x)
{
T[x] = gmax(x, gmax(T[Son[x][0]], T[Son[x][1]]));
}

inline void Reverse(int x)
{
Rev[x] = !Rev[x];
swap(Son[x][0], Son[x][1]);
}

inline void PushDown(int x)
{
if (!Rev[x]) return;
Rev[x] = false;
if (Son[x][0]) Reverse(Son[x][0]);
if (Son[x][1]) Reverse(Son[x][1]);
}

inline int GetDir(int x)
{
if (x == Son[Father[x]][0]) return 0;
else return 1;
}

void Rotate(int x)
{
int y = Father[x], f;
PushDown(y); PushDown(x);
if (x == Son[y][0]) f = 1;
else f = 0;
if (isRoot[y])
{
isRoot[y] = false;
isRoot[x] = true;
}
else
{
if (y == Son[Father[y]][0]) Son[Father[y]][0] = x;
else Son[Father[y]][1] = x;
}
Father[x] = Father[y];
Son[y][f ^ 1] = Son[x][f];
if (Son[x][f]) Father[Son[x][f]] = y;
Son[x][f] = y;
Father[y] = x;
Update(y); Update(x);
}

void Splay(int x)
{
int y;
while (!isRoot[x])
{
y = Father[x];
if (isRoot[y])
{
Rotate(x);
break;
}
if (GetDir(y) == GetDir(x)) Rotate(y);
else Rotate(x);
Rotate(x);
}
}

int Access(int x)
{
int y = 0;
while (x != 0)
{
Splay(x);
PushDown(x);
if (Son[x][1]) isRoot[Son[x][1]] = true;
Son[x][1] = y;
if (y) isRoot[y] = false;
Update(x);
y = x;
x = Father[x];
}
return y;
}

inline void Make_Root(int x)
{
int t = Access(x);
Reverse(t);
}

{
Make_Root(x);
Splay(x);
Father[x] = y;
}

void Cut(int x, int y)
{
Make_Root(x);
Access(y);
Splay(y);
PushDown(y);
isRoot[Son[y][0]] = true;
Father[Son[y][0]] = 0;
Son[y][0] = 0;
Update(y);
}

/********************* LCT End *********************/

int main()
{
scanf("%d%d%d", &n, &m, &q);
for (int i = 1; i <= m; ++i)
{
if (E[i].u > E[i].v) swap(E[i].u, E[i].v);
E[i].Del = false;
}
sort(E + 1, E + m + 1, CmpW); // by ES.w
for (int i = 1; i <= m; ++i)
{
E[i].Idx = i;
V[n + i] = E[i].w;
}
for (int i = 1; i <= n + m; ++i)
{
isRoot[i] = true;
Father[i] = 0;
T[i] = i;
}
sort(E + 1, E + m + 1, CmpUV); // by ES.u && ES.v
int t;
for (int i = 1; i <= q; ++i)
{
if (Q[i].f == 2)
{
t = FindIdx(Q[i].x, Q[i].y);
E[t].Del = true;
Q[i].Pos = E[t].Idx;
}
else ++Top;
}
Tot = Top;
for (int i = 1; i <= n; ++i)
{
f[i] = i;
Size[i] = 1;
}
sort(E + 1, E + m + 1, CmpIdx); // by ES.Idx
int Cnt = 0, fx, fy;
for (int i = 1; i <= m; ++i)
{
if (E[i].Del) continue;
fx = Find(E[i].u); fy = Find(E[i].v);
if (fx == fy) continue;
UN(fx, fy);
if (++Cnt == n - 1) break;
}
int CutE;
for (int i = q; i >= 1; --i)
{
Make_Root(Q[i].x);
t = Access(Q[i].y);
if (Q[i].f == 1) Ans[Top--] = V[T[t]];
else
{
if (E[Q[i].Pos].w >= V[T[t]]) continue;
CutE = T[t];
Cut(CutE, E[CutE - n].u); Cut(CutE, E[CutE - n].v);