明天省选复习一下板子。
lct板子题…操作都是基本要求…
不过一遍ac还是超出了我的预料QWQ
c++代码如下:
#include<bits/stdc++.h>
#define rep(i,x,y) for(register int i = x; i <= y; ++ i)
#define repd(i,x,y) for(register int i = x; i >= y; -- i)
typedef long long ll;
using namespace std;
template<typename T>inline void read(T&x)
{
x = 0;char c;int sign = 1;
do { c = getchar(); if(c == '-') sign = -1; }while(!isdigit(c));
do { x = x * 10 + c - '0'; c = getchar(); }while(isdigit(c));
x *= sign;
}
const int N = 3e4 +50;
int n,m;
struct Link_Cut_Tree
{
int val[N],w[N],f[N],rev[N];
int ch[N][2];
#define lc ch[x][0]
#define rc ch[x][1]
inline bool get(int x) { return ch[f[x]][1] == x; }
inline bool is_root(int x) { return (ch[f[x]][1] != x) && (ch[f[x]][0] != x); }
inline void update(int x) { val[x] = val[lc] + val[rc] + w[x]; }
inline void put_down(int x)
{
if(!is_root(x)) put_down(f[x]);
if(rev[x])
{
rev[lc] ^= 1;rev[rc] ^= 1;
rev[x] = 0; swap(lc,rc);
}
}
inline void rotate(int x)
{
int fa = f[x],t = get(x);
if(!is_root(fa)) ch[f[fa]][get(fa)] = x;
f[x] = f[fa]; ch[fa][t] = ch[x][t^1];
if(ch[fa][t]) f[ch[fa][t]] = fa;
ch[x][t^1] = fa; f[fa] = x;
update(fa); update(x);
}
inline void splay(int x)
{
put_down(x);
while(!is_root(x))
{
if(!is_root(f[x])) rotate(get(x) == get(f[x]) ? f[x] : x);
rotate(x);
}
}
void access(int x)
{
int t = 0;
while(x)
{
splay(x);
ch[x][1] = t;
update(x);
t = x;
x = f[x];
}
}
inline void mkrt(int x) { access(x);splay(x); rev[x] ^= 1; }
inline void Link(int x,int y)
{
if(find(x) == find(y)) return void(puts("no"));
mkrt(x); access(y); splay(y);
f[x] = y; puts("yes");
}
inline void Modify(int x,int val)
{
splay(x); w[x] = val;
update(x);
}
inline int find(int x)
{
access(x); splay(x);
while(lc) x = lc;
return x;
}
inline void query(int x,int y)
{
if(find(x) != find(y)) return void(puts("impossible"));
mkrt(x); access(y); splay(y);
printf("%d\n",val[y]);
}
}lct;
char s[100];
int main()
{
read(n);
rep(i,1,n) read(lct.w[i]),lct.val[i] = lct.w[i];
read(m);
int a,b;
rep(i,1,m)
{
scanf("%s",s);
read(a); read(b);
if(s[0] == 'e')
{
lct.query(a,b);
}else if(s[0] == 'p')
{
lct.Modify(a,b);
}else if(s[0] == 'b')
{
lct.Link(a,b);
}
}
return 0;
}