[SYZOI Round1] 滑稽♂树

题面

传送门

Sol

我也不知道哪里来的题目哪里来的\(OJ\)

子树变成\(DFS\)序后就是裸的树套树

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

IL int Input(){
	RG int 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 w[_], n, tmp1[20], tmp2[20], first[_], cnt, L[_], R[_], idx, num, rt[_];
struct Edge{
	int to, next;
} edge[_ << 1];
struct Segment{
	int ls, rs, sz;
} T[_ * 200];

IL void Add_Graph(RG int u, RG int v){
	edge[cnt] = (Edge){v, first[u]}, first[u] = cnt++;
}

IL void Dfs(RG int u, RG int ff){
	L[u] = ++idx;
	for(RG int e = first[u]; e != -1; e = edge[e].next)
		if(edge[e].to != ff) Dfs(edge[e].to, u);
	R[u] = idx;
}

IL void Modify(RG int &x, RG int l, RG int r, RG int p, RG int v){
	if(!x) x = ++num; T[x].sz += v;
	if(l == r) return;
	RG int mid = (l + r) >> 1;
	if(p <= mid) Modify(T[x].ls, l, mid, p, v);
	else Modify(T[x].rs, mid + 1, r, p, v);
}

IL void Add(RG int x, RG int v, RG int d){
	for(; x <= n; x += x & -x) Modify(rt[x], 1, 10000, v, d);
}

IL int QueryKth(RG int l, RG int r, RG int k){
	if(l == r) return l;
	RG int sum = 0, mid = (l + r) >> 1;
	for(RG int i = 1; i <= tmp1[0]; ++i) sum -= T[T[tmp1[i]].ls].sz;
	for(RG int i = 1; i <= tmp2[0]; ++i) sum += T[T[tmp2[i]].ls].sz;
	if(k <= sum){
		for(RG int i = 1; i <= tmp1[0]; ++i) tmp1[i] = T[tmp1[i]].ls;
		for(RG int i = 1; i <= tmp2[0]; ++i) tmp2[i] = T[tmp2[i]].ls;
		return QueryKth(l, mid, k);
	}
	for(RG int i = 1; i <= tmp1[0]; ++i) tmp1[i] = T[tmp1[i]].rs;
	for(RG int i = 1; i <= tmp2[0]; ++i) tmp2[i] = T[tmp2[i]].rs;
	return QueryKth(mid + 1, r, k - sum);
}

IL int Kth(RG int l, RG int r, RG int k){
	tmp1[0] = tmp2[0] = 0;
	for(RG int i = l - 1; i; i -= i & -i) tmp1[++tmp1[0]] = rt[i];
	for(RG int i = r; i; i -= i & -i) tmp2[++tmp2[0]] = rt[i];
	return QueryKth(1, 10000, k);
}

IL int QuerySum(RG int x, RG int l, RG int r, RG int ql, RG int qr){
	if(!x) return 0;
	if(ql <= l && qr >= r) return T[x].sz;
	RG int mid = (l + r) >> 1, ret = 0;
	if(ql <= mid) ret = QuerySum(T[x].ls, l, mid, ql, qr);
	if(qr > mid) ret += QuerySum(T[x].rs, mid + 1, r, ql, qr);
	return ret;
}

IL int Sum(RG int x, RG int l, RG int r){
	RG int ret = 0;
	for(; x; x -= x & -x) ret += QuerySum(rt[x], 1, 10000, l, r);
	return ret;
}

int main(RG int argc, RG char* argv[]){
	n = Input();
	for(RG int i = 1; i <= n; ++i) first[i] = -1, w[i] = Input();
	for(RG int i = 1; i < n; ++i){
		RG int u = Input(), v = Input();
		Add_Graph(u, v), Add_Graph(v, u);
	}
	Dfs(1, 0);
	for(RG int i = 1; i <= n; ++i) Add(L[i], w[i], 1);
	for(RG int q = Input(); q; --q){
		RG int op = Input(), u = Input(), x = Input(), y;
		if(op == 1) printf("%d\n", Kth(L[u], R[u], x));
		else if(op == 2) y = Input(), printf("%d\n", Sum(R[u], x, y) - Sum(L[u] - 1, x, y));
		else Add(L[u], w[u], -1), w[u] = x, Add(L[u], w[u], 1);
	}
    return 0;
}

posted @ 2018-04-04 16:57  Cyhlnj  阅读(181)  评论(0编辑  收藏  举报