【BZOJ】3319: 黑白树(并查集+特殊的技巧/-树链剖分+线段树)

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

以为是模板题就复习了下hld。。。。。。。。。。。。。。。。。。。。。。。。。。。。。

然后nlg^2n被tle成翔了。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。

然后看题解QAQ,,,这。。。

神题做法。。。待会再写。。。(upd:【BZOJ】3319: 黑白树

tle的hld:

#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; }

const int N=1e6+10;
int ihead[N], cnt;
struct dat { int next, to, id; }e[N<<1];
void add(int u, int v, int id) {
	e[++cnt].next=ihead[u]; ihead[u]=cnt; e[cnt].to=v; e[cnt].id=id;
	e[++cnt].next=ihead[v]; ihead[v]=cnt; e[cnt].to=u; e[cnt].id=id;
}

int n, son[N], top[N], fa[N], sz[N], root, id[N], tot, ed[N], dep[N];
void dfs1(int x, int f) {
	sz[x]=1; int y; fa[x]=f; dep[x]=dep[f]+1;
	rdm(x, i) if(f!=(y=e[i].to)) {
		dfs1(y, x);
		sz[x]+=sz[y];
		if(sz[y]>sz[son[x]]) son[x]=y;
	}
}
void dfs2(int x, int tp) {
	top[x]=tp; id[x]=++tot;
	if(son[x]) dfs2(son[x], tp);
	rdm(x, i) if(e[i].to!=fa[x] && e[i].to!=son[x]) dfs2(e[i].to, e[i].to);
}
void geted(int x, int t) {
	ed[id[x]]=t;
	rdm(x, i) if(e[i].to!=fa[x]) geted(e[i].to, e[i].id);
}

#define lc x<<1
#define rc x<<1|1
#define lson l, mid, lc
#define rson mid+1, r, rc
#define MID (l+r)>>1
struct T { int mx; bool tag; }t[N<<2];
void upd(int x, int r) {
	t[x].tag=1;
	t[x].mx=max(t[x].mx, r);
}
void pushdown(int x, int mid, int r) {
	if(t[x].tag) t[x].tag=0, upd(lc, mid), upd(rc, r);
}
void pushup(int x) { t[x].mx=max(t[lc].mx, t[rc].mx); }
void update(int L, int R, int l=1, int r=tot, int x=1) {
	if(L<=l && r<=R) { upd(x, r); return; }
	int mid=MID;
	pushdown(x, mid, r);
	if(L<=mid) update(L, R, lson);
	if(mid<R) update(L, R, rson);
	pushup(x);
}
int ask(int L, int R, int l=1, int r=tot, int x=1) {
	if(L<=l && r<=R) return t[x].mx;
	int mid=MID, ret=0;
	pushdown(x, mid, r);
	if(L<=mid) ret=max(ret, ask(L, R, lson));
	if(mid<R) ret=max(ret, ask(L, R, rson));
	return ret;
}
void ask(int x) {
	int fx=top[x], pos=0;
	while(fx!=root) {
		pos=ask(id[fx], id[x]);
		if(pos!=0) break;
		x=fa[fx]; fx=top[x];
	}
	if(pos==0 && x!=root) pos=ask(id[top[x]], id[x]);
	printf("%d\n", ed[pos]);
}
void change(int x, int y) {
	int fx=top[x], fy=top[y];
	while(fx!=fy) {
		if(dep[fx]<dep[fy]) { swap(fx, fy); swap(x, y); }
		update(id[fx], id[x]);
		x=fa[fx]; fx=top[x];
	}
	if(x==y) return;
	if(dep[x]<dep[y]) swap(x, y);
	update(id[y]+1, id[x]);
}

int main() {
	read(n); int m=getint();
	for1(i, 1, n-1) { int u=getint(), v=getint(); add(u, v, i); }
	root=1;
	dfs1(root, 0); dfs2(root, root); geted(root, 0);
	while(m--) {
		int cc=getint();
		if(cc==1) ask(getint());
		else { int u=getint(), v=getint(); change(u, v); }
	}
	return 0;
}

  

 


 

 

Description

给定一棵树,边的颜色为黑或白,初始时全部为白色。维护两个操作:
 
 
1.查询u到根路径上的第一条黑色边的标号。
2.将u到v    路径上的所有边的颜色设为黑色。
 
Notice:这棵树的根节点为1

Input


第一行两个数n,m分别表示点数和操作数。
接下来n-?    1行,每行2个数u,v.表示一条u到v的边。
接下来m行,每行为以下格式:
 
 
1 v 表示第一个操作
 
 
2 v u 表示第二种操作
 
 

Output

对于每个询问,输出相应答案。如果不存在,输出0。

Sample Input

5 4
1 2
1 3
2 4
2 5
1 2
2 2 3
1 3
1 4

Sample Output

0
2
1

HINT

 



 

对于    100%    的数据:n,m<=10^6

 

Source

 

 
posted @ 2014-12-19 17:19  iwtwiioi  阅读(728)  评论(0编辑  收藏  举报