WC模拟(1.6) T2 送你一棵圣诞树

送你一棵圣诞树

题目背景:

1.6 WC模拟T2

分析:树状数组 + 线段树 + lca

 

考场上没有想到nlog2n的做法······只想到nlog3n,然后自己并不想写,然后写了应该也是T到飞起······显然因为是子树操作,很简单的想法就是直接求出dfs序,然后转化为序列查询,然后我们发现,我们有两种可行的方法,第一种用主席树,外层颜色,内层区间,然后区间上在加上权值线段树,对于每一个i记录上一个和它颜色相同的点pre[i],那么对于一个询问就是求某一段颜色中,某一段区间中pre[i] < dfn[u]的个数。然后发现,它还需要进行修改,那么最外层还有一个树状数组,然后华丽丽的三个log,还有一种方法是分块,考虑首先按照区间分块,块内按照pre排序,然后对于每一个块针对颜色区间建主席树,然后对于一个块的询问,直接二分找到pre = l - 1的位置,在主席树里面直接查询颜色个数就可以了,(块中只记录每一个颜色的第一个出现位置),而对于修改,直接暴力重建所在块就可以了,这个复杂度是O(n * sqrt(n) * logn),这两种方法都是基于你只想到了在序列上处理,而忽略了本身一棵树的性质,我们先把相同的颜色的位置的dfs序提出来排序,然后在相同颜色的相邻dfs序的lca处减一,这样就可以直接变成序列求和了,主席树就可以解决了,因为带修改,那么变成树状数组套线段树就可以了,复杂度O(nlog2n)

 

Source:

/*
	created by scarlyw
*/
#include <cstdio>
#include <string>
#include <algorithm>
#include <cstring>
#include <iostream>
#include <cmath>
#include <cctype>
#include <vector>
#include <set>
#include <queue>
#include <ctime>
#include <bitset>

inline char read() {
	static const int IN_LEN = 1024 * 1024;
	static char buf[IN_LEN], *s, *t;
	if (s == t) {
		t = (s = buf) + fread(buf, 1, IN_LEN, stdin);
		if (s == t) return -1;
	}
	return *s++;
}

///*
template<class T>
inline void R(T &x) {
	static char c;
	static bool iosig;
	for (c = read(), iosig = false; !isdigit(c); c = read()) {
		if (c == -1) return ;
		if (c == '-') iosig = true;	
	}
	for (x = 0; isdigit(c); c = read()) 
		x = ((x << 2) + x << 1) + (c ^ '0');
	if (iosig) x = -x;
}
//*/

const int OUT_LEN = 1024 * 1024;
char obuf[OUT_LEN], *oh = obuf;
inline void write_char(char c) {
	if (oh == obuf + OUT_LEN) fwrite(obuf, 1, OUT_LEN, stdout), oh = obuf;
	*oh++ = c;
}

template<class T>
inline void W(T x) {
	static int buf[30], cnt;
	if (x == 0) write_char('0');
	else {
		if (x < 0) write_char('-'), x = -x;
		for (cnt = 0; x; x /= 10) buf[++cnt] = x % 10 + 48;
		while (cnt) write_char(buf[cnt--]);
	}
}

inline void flush() {
	fwrite(obuf, 1, oh - obuf, stdout);
}

/*
template<class T>
inline void R(T &x) {
	static char c;
	static bool iosig;
	for (c = getchar(), iosig = false; !isdigit(c); c = getchar())
		if (c == '-') iosig = true;	
	for (x = 0; isdigit(c); c = getchar()) 
		x = ((x << 2) + x << 1) + (c ^ '0');
	if (iosig) x = -x;
}
//*/

const int MAXN = 100000 + 10;

int cnt, n, q, t, x, y, ind;
std::set<int> col[MAXN];
std::vector<int> edge[MAXN];
int c[MAXN], pos[MAXN], root[MAXN], in[MAXN], out[MAXN], size[MAXN], dep[MAXN];
int father[MAXN], son[MAXN], top[MAXN], last[MAXN];
std::set<int>::iterator it, it1, it2;

struct node {
	int left, right, sum;
} tree[MAXN * 500];

inline void insert(int &cur, int l, int r, int pos, int x) {
	if (cur == 0) cur = ++cnt;
	tree[cur].sum += x;
	if (l == r) return ;
	int mid = l + r >> 1;
	if (pos <= mid) insert(tree[cur].left, l, mid, pos, x);
	else insert(tree[cur].right, mid + 1, r, pos, x);
}

inline int query(int cur, int l, int r, int ql, int qr) {
	if (ql <= l && r <= qr) return tree[cur].sum;
	int mid = l + r >> 1, ret = 0;
	if (ql <= mid) ret += query(tree[cur].left, l, mid, ql, qr);
	if (qr > mid) ret += query(tree[cur].right, mid + 1, r, ql, qr);
	return ret;
}

inline void insert(int i, int c, int x) {
	for (; i <= n; i += (i & -i)) insert(root[i], 1, n, c, x);
}

inline void add_edge(int x, int y) {
	edge[x].push_back(y), edge[y].push_back(x);
}

inline void dfs1(int cur, int fa) {
	in[cur] = ++ind, size[cur] = 1, dep[cur] = dep[fa] + 1, father[cur] = fa;
	for (int p = 0; p < edge[cur].size(); ++p) {
		int v = edge[cur][p];
		if (v != fa) {
			dfs1(v, cur), size[cur] += size[v];
			if (size[v] > size[son[cur]]) son[cur] = v;
		}
	}
	out[cur] = ind, pos[in[cur]] = cur;
}

inline void dfs2(int cur, int tp) {
	top[cur] = tp;
	if (son[cur]) dfs2(son[cur], tp);
	for (int p = 0; p < edge[cur].size(); ++p) {
		int v = edge[cur][p];
		if (top[v] == 0) dfs2(v, v);
	}
}

inline int query_lca(int u, int v) {
	while (top[u] != top[v]) 
		(dep[top[u]] > dep[top[v]]) ? u = father[top[u]] : v = father[top[v]];
	return (dep[u] > dep[v]) ? v : u;
}

inline void solve_tree() {
	R(n), R(q), R(t);
	for (int i = 1; i <= n; ++i) R(c[i]);
	for (int i = 1; i < n; ++i) R(x), R(y), add_edge(x, y);
	dfs1(1, 0), dfs2(1, 1);
}

inline solve_struct() {
	for (int i = 1; i <= n; ++i) col[i].insert(0);
	for (int i = 1; i <= n; ++i) col[c[pos[i]]].insert(i);
	for (int i = 1; i <= n; ++i) {
		x = c[pos[i]], insert(i, x, 1);
		if (last[x] != 0) insert(in[query_lca(last[x], pos[i])], x, -1);
		last[x] = pos[i];
	}
}

inline void solve_modify(int cur, int c) {
	int pos = in[cur], x = ::c[cur];
	if (c == x) return ;
	it = it1 = it2 = col[x].lower_bound(pos), it1--, it2++;
	if (*it1 != 0) insert(in[query_lca(::pos[*it1], cur)], x, 1);
	if (it2 != col[x].end()) insert(in[query_lca(::pos[*it2], cur)], x, 1);
	if (*it1 != 0 && it2 != col[x].end()) 
		insert(in[query_lca(::pos[*it1], ::pos[*it2])], x, -1);
	insert(pos, x, -1), col[x].erase(it), x = ::c[cur] = c, insert(pos, x, 1);
	col[x].insert(pos), it1 = it2 = col[x].lower_bound(pos), it1--, it2++;
	if (*it1 != 0) insert(in[query_lca(::pos[*it1], cur)], x, -1);
	if (it2 != col[x].end()) insert(in[query_lca(::pos[*it2], cur)], x, -1);
	if (*it1 != 0 && it2 != col[x].end()) 
		insert(in[query_lca(::pos[*it1], ::pos[*it2])], x, 1);
}

inline int solve_query(int l, int r, int ql, int qr) {
	int ans = 0;
	for (int i = r; i; i -= (i & -i)) ans += query(root[i], 1, n, ql, qr);
	for (int i = l - 1; i; i -= (i & -i)) ans -= query(root[i], 1, n, ql, qr);
	return ans;
}

inline void solve() {
	static int type, u, l, r, c, ans = 0;
	solve_tree(), solve_struct();
	while (q--) {
		R(type);
		if (type == 1) {
			R(u), R(l), R(r), (t ? (u ^= ans, l ^= ans, r ^= ans) : 0);
			W(ans = solve_query(in[u], out[u], l, r)), write_char('\n');
		} else {
			R(u), R(c), (t ? (u ^= ans, c ^= ans) : 0);
			solve_modify(u, c);
		}
	}
}

int main() {
	freopen("xmastree1.in", "r", stdin);
	freopen("xmastree1.out", "w", stdout);
	solve();
	flush();
	return 0;
}


阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/scar_lyw/article/details/79035054
文章标签: NOI 数据结构
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭