【BZOJ】1180: [CROATIAN2009]OTOCI & 2843: 极地旅行社(lct)

http://www.lydsy.com/JudgeOnline/problem.php?id=1180

今天状态怎么这么不好。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。

又是调了好久。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。

我竟然忘记更改值那里要先splay后再更改,而且还要pushup先!!!!!

QAQ

太弱了。。

记住!!!更改信息一定要先变成了根再修改,而且还要pushup!

(蒟蒻问了下神犇,总算弄懂了:“原树里面的父亲 在splay中可以是儿子”,所以在更新了父亲的点时,由于在splay中是儿子,所以没更新到在原树中是儿子的点。因此就错了。

#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
using namespace std;
typedef long long ll;
#define rep(i, n) for(int i=0; i<(n); ++i)
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
#define for2(i,a,n) for(int i=(a);i<(n);++i)
#define for3(i,a,n) for(int i=(a);i>=(n);--i)
#define for4(i,a,n) for(int i=(a);i>(n);--i)
#define CC(i,a) memset(i,a,sizeof(i))
#define read(a) a=getint()
#define print(a) printf("%d", a)
#define dbg(x) cout << (#x) << " = " << (x) << endl
#define error(x) (!(x)?puts("error"):0)
#define rdm(x, i) for(int i=ihead[x]; i; i=e[i].next)
inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; }

struct node* null;
struct node {
	node *f, *c[2];
	int w, s; bool rev;
	node(int _k=0) { w=s=_k; f=c[0]=c[1]=null; rev=0; }
	void setc(node *x, bool d) { c[d]=x; x->f=this; }
	bool check() { return f==null || (f->c[0]!=this && f->c[1]!=this); }
	bool d() { return f->c[1]==this; }
	void pushup() { s=w+c[0]->s+c[1]->s; }
	void upd() { if(this==null) return; rev=!rev; swap(c[0], c[1]); }
	void pushdown() {
		if(rev) {
			rev=0;
			c[0]->upd();
			c[1]->upd();
		}
	}
};
void rot(node *x) {
	node *f=x->f;
	f->pushdown(); x->pushdown(); bool d=x->d();
	if(f->check()) x->f=f->f;
	else f->f->setc(x, f->d());
	f->setc(x->c[!d], d);
	x->setc(f, !d);
	f->pushup();
}
void fix(node *x) { if(!x->check()) fix(x->f); x->pushdown(); }
void splay(node *x) {
	fix(x);
	while(!x->check())
		if(x->f->check()) rot(x);
		else x->d()==x->f->d()?(rot(x->f), rot(x)):(rot(x), rot(x));
	x->pushup();
}
node* access(node *x) {
	node *y=null;
	for(; x!=null; y=x, x=x->f) splay(x), x->c[1]=y;
	return y;
}
node* findroot(node *x) {
	access(x); splay(x);
	while(x->c[0]!=null) x=x->c[0];
	return x;
}
void mkroot(node *x) { access(x)->upd(); splay(x); }
void link(node *x, node *y) { mkroot(x); x->f=y; }

const int N=30005;
node *root[N];
int n;
int main() {
	null=new node; null->f=null->c[0]=null->c[1]=null;
	read(n);
	for1(i, 1, n) root[i]=new node(getint());
	int m=getint();
	while(m--) {
		char c=getchar(); while(c<'a'||c>'z') c=getchar();
		int a=getint(), b=getint();
		if(c=='b') {
			if(findroot(root[a])==findroot(root[b])) puts("no");
			else { puts("yes"); link(root[a], root[b]); };
		}
		else if(c=='p') { mkroot(root[a]); root[a]->w=b; root[a]->pushup(); }
		else {
			if(findroot(root[a])!=findroot(root[b])) { puts("impossible"); continue; }
			mkroot(root[a]); access(root[b]); splay(root[b]);
			printf("%d
", root[b]->s);
		}
	}
	return 0;
}

  


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

原文地址:https://www.cnblogs.com/iwtwiioi/p/4149468.html