bzoj1180OTOCI 动态树(Linked-Cut-Tree)

1180: [CROATIAN2009]OTOCI

Time Limit: 50 Sec   Memory Limit: 162 MB
Submit: 1157   Solved: 715
[ Submit][ Status][ Discuss]

Description

给出n个结点以及每个点初始时对应的权值wi。起始时点与点之间没有连边。有3类操作: 1、bridge A B:询问结点A与结点B是否连通。如果是则输出“no”。否则输出“yes”,并且在结点A和结点B之间连一条无向边。 2、penguins A X:将结点A对应的权值wA修改为X。 3、excursion A B:如果结点A和结点B不连通,则输出“impossible”。否则输出结点A到结点B的路径上的点对应的权值的和。给出q个操作,要求在线处理所有操作。数据范围:1<=n<=30000, 1<=q<=300000, 0<=wi<=1000。

Input

第一行包含一个整数n(1<=n<=30000),表示节点的数目。第二行包含n个整数,第i个整数表示第i个节点初始时对应的权值。第三行包含一个整数q(1<=n<=300000),表示操作的数目。以下q行,每行包含一个操作,操作的类别见题目描述。任意时刻每个节点对应的权值都是1到1000的整数。

Output

输出所有bridge操作和excursion操作对应的输出,每个一行。

Sample Input

5
4 2 4 5 6
10
excursion 1 1
excursion 1 2
bridge 1 2
excursion 1 2
bridge 3 4
bridge 3 5
excursion 4 5
bridge 1 3
excursion 2 4
excursion 2 5

Sample Output

4
impossible
yes
6
yes
yes
15
yes
15
16

HINT

Source

题目大意:给个森林,要求写种数据结构支持树的拆边,连边,权值求和

动态树裸题

动态树是啥?详见这里:戳这里

#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
#include<cmath>
#define maxn 330000
#define ls t[p].ch[0]
#define rs t[p].ch[1]
#define pa t[p].f
using namespace std;
int read() {
    char ch = getchar(); int x = 0, f = 1;
    while(ch < '0' || ch > '9') {if(ch == '-') f = -1; ch = getchar();}
    while(ch >= '0' && ch <= '9') {x = x * 10 - '0' + ch; ch = getchar();}
    return x * f;
}
int readchar() {
	char ch = getchar();
	while(ch < 'a' || ch > 'z') ch = getchar();
	if(ch == 'b') return 1;
	if(ch == 'p') return 2;
	if(ch == 'e') return 3;
}
 
struct node {
    int f, ch[2];
    int rev, v, s;
}t[maxn]; int n, m, q[maxn], top;
bool wh(int p) {return t[pa].ch[1] == p;}
bool Isroot(int p) {return t[pa].ch[0] != p && t[pa].ch[1] != p;} 
 
void push_down(int p) {
	if(t[p].rev) {
		t[ls].rev ^= 1; t[rs].rev ^= 1;
		t[p].rev = 0; swap(ls, rs);
	}
}
void push_up(int p) {
	q[top = 1] = p;
	for(int i = p; !Isroot(i); i = t[i].f)
		q[++top] = t[i].f;
	for(int i = top; i; --i) push_down(q[i]);
}
void update(int p) {t[p].s = t[ls].s + t[rs].s + t[p].v;}
 
void Rotate(int p) {
	int f = pa, g = t[f].f, c = wh(p);
	if(!Isroot(f)) t[g].ch[wh(f)] = p; t[p].f = g;
	t[f].ch[c] = t[p].ch[c ^ 1]; if(t[f].ch[c]) t[t[f].ch[c]].f = f;
	t[p].ch[c ^ 1] = f; t[f].f = p;
	update(f);
}
 
void Splay(int p) {
	push_up(p);
	for(; !Isroot(p); Rotate(p)) 
		if(!Isroot(pa)) Rotate(wh(p) == wh(pa) ? pa : p);
	update(p);
}
 
void Access(int p) {
	for(int pre = 0; p; pre = p, p = pa) {
		Splay(p);
		t[p].ch[1] = pre;
		update(p);
	}
}
 
void makeroot(int p) {Access(p); Splay(p); t[p].rev ^= 1;}
int find(int p) {
	Access(p); Splay(p);
	while(t[p].ch[0]) p = t[p].ch[0];
	return p;
}
 
void Link(int u, int v) {
	makeroot(u); 
	t[u].f = v;
}
 
void Change(int x, int y) {Access(x); Splay(x); t[x].v = y; update(x);}
void Query(int x, int y) {makeroot(x); Access(y); Splay(y); printf("%d\n", t[y].s);}
 
int main()
{
    n = read();
    for(int i = 1;i <= n; ++i) t[i].v = t[i].s = read(); 
    m = read();
    for(int i = 1;i <= m; ++i) {
        int opt = readchar(), x = read(), y = read();
        if(opt == 1) {
        	if(find(x) == find(y)) puts("no");
        	else {
        		Link(x, y);
        		puts("yes");
			}
        }
        if(opt == 2) Change(x, y);
        if(opt == 3) {
        	if(find(x) == find(y)) Query(x, y);
        	else puts("impossible");
        }
    }
    return 0;
}


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值