[BZOJ3991][SDOI2015]寻宝游戏

[BZOJ3991][SDOI2015]寻宝游戏

试题描述

B最近正在玩一个寻宝游戏,这个游戏的地图中有N个村庄和N-1条道路,并且任何两个村庄之间有且仅有一条路径可达。游戏开始时,玩家可以任意选择一个村庄,瞬间转移到这个村庄,然后可以任意在地图的道路上行走,若走到某个村庄中有宝物,则视为找到该村庄内的宝物,直到找到所有宝物并返回到最初转移到的村庄为止。小B希望评测一下这个游戏的难度,因此他需要知道玩家找到所有宝物需要行走的最短路程。但是这个游戏中宝物经常变化,有时某个村庄中会突然出现宝物,有时某个村庄内的宝物会突然消失,因此小B需要不断地更新数据,但是小B太懒了,不愿意自己计算,因此他向你求助。为了简化问题,我们认为最开始时所有村庄内均没有宝物

输入

第一行,两个整数N、M,其中M为宝物的变动次数。

接下来的N-1行,每行三个整数x、y、z,表示村庄x、y之间有一条长度为z的道路。
接下来的M行,每行一个整数t,表示一个宝物变动的操作。若该操作前村庄t内没有宝物,则操作后村庄内有宝物;若该操作前村庄t内有宝物,则操作后村庄内没有宝物。

输出

M行,每行一个整数,其中第i行的整数表示第i次操作之后玩家找到所有宝物需要行走的最短路程。若只有一个村庄内有宝物,或者所有村庄内都没有宝物,则输出0。

输入示例

4 5
1 2 30
2 3 50
2 4 60
2
3
4
2
1

输出示例

0
100
220
220
280

数据规模及约定

1<=N<=100000

1<=M<=100000

对于全部的数据,1<=z<=10^9

题解

见“[OpenJudge0054]特务会议召开”。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cctype>
#include <algorithm>
#include <set>
using namespace std;

int read() {
	int x = 0, f = 1; char c = getchar();
	while(!isdigit(c)){ if(c == '-') f = -1; c = getchar(); }
	while(isdigit(c)){ x = x * 10 + c - '0'; c = getchar(); }
	return x * f;
}

#define maxn 100010
#define maxm 200010
#define maxlog 20
#define LL long long
int n, m, head[maxn], next[maxm], to[maxm], dist[maxm];

void AddEdge(int a, int b, int c) {
	to[++m] = b; dist[m] = c; next[m] = head[a]; head[a] = m;
	swap(a, b);
	to[++m] = b; dist[m] = c; next[m] = head[a]; head[a] = m;
	return ;
}

int ord[maxn], Id[maxn], clo, dep[maxn], fa[maxlog][maxn];
LL Dep[maxn];
void build(int u) {
	ord[u] = ++clo; Id[clo] = u;
	for(int i = 1; i < maxlog; i++) fa[i][u] = fa[i-1][fa[i-1][u]];
	for(int e = head[u]; e; e = next[e]) if(to[e] != fa[0][u]) {
		fa[0][to[e]] = u;
		dep[to[e]] = dep[u] + 1;
		Dep[to[e]] = Dep[u] + dist[e];
		build(to[e]);
	}
	return ;
}
int lca(int a, int b) {
	if(dep[a] < dep[b]) swap(a, b);
	for(int i = maxlog - 1; i >= 0; i--) if(dep[a] - (1 << i) >= dep[b]) a = fa[i][a];
	for(int i = maxlog - 1; i >= 0; i--) if(fa[i][a] != fa[i][b]) a = fa[i][a], b = fa[i][b];
	return a == b ? a : fa[0][b];
}

bool sta[maxn];
set <int> S;
std :: set <int> :: iterator l, r;

int main() {
	n = read(); int q = read();
	for(int i = 1; i < n; i++) {
		int a = read(), b = read(), c = read();
		AddEdge(a, b, c);
	}
	build(1);
	LL ans = 0;
	while(q--) {
		int x = read();
		if(!sta[x]) {
			sta[x] = 1;
			if(S.size()) {
				l = S.lower_bound(ord[x]);
				if(l == S.begin()) l = S.end();
				l--;
				r = S.upper_bound(ord[x]);
				if(r == S.end()) r = S.begin();
				int c = lca(Id[*l], Id[*r]);
				LL d = Dep[Id[*l]] + Dep[Id[*r]] - (Dep[c] << 1);
				ans -= d;
				c = lca(Id[*l], x);
				d = Dep[Id[*l]] + Dep[x] - (Dep[c] << 1);
				ans += d;
				c = lca(x, Id[*r]);
				d = Dep[x] + Dep[Id[*r]] - (Dep[c] << 1);
				ans += d;
			}
			S.insert(ord[x]);
		}
		else {
			sta[x] = 0;
			S.erase(ord[x]);
			if(S.size()) {
				l = S.lower_bound(ord[x]);
				if(l == S.begin()) l = S.end();
				l--;
				r = S.upper_bound(ord[x]);
				if(r == S.end()) r = S.begin();
				int c = lca(Id[*l], Id[*r]);
//				printf("points: %d %d %d
", Id[*r], Id[*r], c);
				LL d = Dep[Id[*l]] + Dep[Id[*r]] - (Dep[c] << 1);
				ans += d;
//				printf("+ %lld
", d);
				c = lca(Id[*l], x);
				d = Dep[Id[*l]] + Dep[x] - (Dep[c] << 1);
				ans -= d;
//				printf("- %lld
", d);
				c = lca(x, Id[*r]);
				d = Dep[x] + Dep[Id[*r]] - (Dep[c] << 1);
				ans -= d;
//				printf("- %lld
", d);
			}
		}
		printf("%lld
", ans);
	}
	
	return 0;
}
原文地址:https://www.cnblogs.com/xiao-ju-ruo-xjr/p/5972929.html