Bzoj3514: Codechef MARCH14 GERALD07加强版

题面

传送门

Sol

首先每次加入边的两个点不联通,那么联通块的个数就要减\(1\)
那么考虑怎么做
莫名想到\(LCT\)
然后就不会了。。。
\(orz\)题解
维护一个每条边的数组,如果这个点加入后形成环,那么就把这个数组设为环内最先加入的边的编号,特判自环,然后替换这条边
没有替换为\(0\)
那么每次询问答案就是求\([l, r]\)内该数组小于\(l\)的边的个数,答案为\(n\)-个数(自己\(yy\)一下就好)
主席树来求这个东西
代码很丑

# include <bits/stdc++.h>
# define IL inline
# define RG register
# define Fill(a, b) memset(a, b, sizeof(a))
using namespace std;
typedef long long ll;
const int _(4e5 + 5);
const int __(4e6 + 5);

IL ll Input(){
	RG ll x = 0, z = 1; RG char c = getchar();
	for(; c < '0' || c > '9'; c = getchar()) z = c == '-' ? -1 : 1;
	for(; c >= '0' && c <= '9'; c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
	return x * z;
}

int n, m, k, type, rpl[_], fa[_], cnt, id[_];
struct Edge{
	int u, v;
} edge[_];
struct LCT{
	# define ls ch[0][x]
	# define rs ch[1][x]
	
	int ch[2][_], fa[_], mn[_], val[_], S[_], rev[_];

	IL LCT(){
		Fill(val, 127); Fill(mn, 127);
	}
	
	IL bool Son(RG int x){
		return ch[1][fa[x]] == x;
	}

	IL bool Isroot(RG int x){
		return ch[0][fa[x]] != x && ch[1][fa[x]] != x;
	}

	IL void Update(RG int x){
		mn[x] = min(val[x], min(mn[ls], mn[rs]));
	}

	IL void Reverse(RG int x){
		if(!x) return;
		swap(ls, rs); rev[x] ^= 1;
	}

	IL void Adjust(RG int x){
		if(!rev[x]) return;
		rev[x] = 0; Reverse(ls); Reverse(rs);
	}
	
	IL void Rotate(RG int x){
		RG int y = fa[x], z = fa[y], c = Son(x);
		if(!Isroot(y)) ch[Son(y)][z] = x; fa[x] = z;
		ch[c][y] = ch[!c][x]; fa[ch[c][y]] = y;
		ch[!c][x] = y; fa[y] = x;
		Update(y);
	}

	IL void Splay(RG int x){
		S[S[0] = 1] = x;
		for(RG int y = x; !Isroot(y); y = fa[y]) S[++S[0]] = fa[y];
		while(S[0]) Adjust(S[S[0]--]);
		for(RG int y = fa[x]; !Isroot(x); Rotate(x), y = fa[x])
			if(!Isroot(y)) Son(x) ^ Son(y) ? Rotate(x) : Rotate(y);
		Update(x);
	}

	IL void Access(RG int x){
		for(RG int y = 0; x; y = x, x = fa[x]) Splay(x), ch[1][x] = y, Update(x);
	}

	IL void Makeroot(RG int x){
		Access(x); Splay(x); Reverse(x);
	}

	IL void Link(RG int x, RG int y){
		Makeroot(x); fa[x] = y;
	}

	IL void Split(RG int x, RG int y){
		Makeroot(x); Access(y); Splay(y);
	}

	IL void Cut(RG int x, RG int y){
		Split(x, y); fa[x] = ch[0][y] = 0; Update(y);
	}
	
	# undef ls
	# undef rs
} T;

struct Segment{
	int ls[__], rs[__], size[__], rt[_], cnt;

	IL void Modify(RG int &x, RG int l, RG int r, RG int v){
		ls[++cnt] = ls[x]; rs[cnt] = rs[x]; size[cnt] = size[x] + 1; x = cnt;
		if(l == r) return;
		RG int mid = (l + r) >> 1;
		if(v <= mid) Modify(ls[x], l, mid, v);
		else Modify(rs[x], mid + 1, r, v);
	}

	IL int Query(RG int A, RG int B, RG int l, RG int r, RG int L, RG int R){
		if(L <= l && R >= r) return size[A] - size[B];
		RG int mid = (l + r) >> 1, ans = 0;
		if(L <= mid) ans = Query(ls[A], ls[B], l, mid, L, R);
		if(R > mid) ans += Query(rs[A], rs[B], mid + 1, r, L, R);
		return ans;
	}
} HJT;

IL int Find(RG int x){  return x == fa[x] ? x : fa[x] = Find(fa[x]);  }

int main(RG int argc, RG char* argv[]){
	cnt = n = Input(); m = Input(); k = Input(); type = Input();
	for(RG int i = 1; i <= n; ++i) fa[i] = i;
	for(RG int i = 1; i <= m; ++i){
		edge[i].u = Input(); edge[i].v = Input();
		if(edge[i].u == edge[i].v){
			rpl[i] = i;
			continue;
		}
		RG int fx = Find(edge[i].u), fy = Find(edge[i].v);
		if(fx != fy){
			T.val[++cnt] = i; id[i] = cnt;
			T.Link(cnt, edge[i].u), T.Link(cnt, edge[i].v);
			fa[fx] = fy;
		}
		else{
			T.Split(edge[i].u, edge[i].v);
			rpl[i] = T.mn[edge[i].v];
			T.Cut(id[rpl[i]], edge[rpl[i]].u); T.Cut(id[rpl[i]], edge[rpl[i]].v);
			T.val[++cnt] = i; id[i] = cnt;
			T.Link(cnt, edge[i].u), T.Link(cnt, edge[i].v);
		}
	}
	for(RG int i = 1; i <= m; ++i){
		HJT.rt[i] = HJT.rt[i - 1];
		HJT.Modify(HJT.rt[i], 0, m, rpl[i]);
	}
	for(RG int ans = 0, i = 1; i <= k; ++i){
		RG int l = Input(), r = Input();
		if(type) l ^= ans, r ^= ans;
		printf("%d\n", ans = n - HJT.Query(HJT.rt[r], HJT.rt[l - 1], 0, m, 0, l - 1));
	}
	return 0;
}

posted @ 2018-01-28 22:37  Cyhlnj  阅读(181)  评论(0编辑  收藏  举报