您的位置:首页 > 其它

【BZOJ3991】【SDOI2015】寻宝游戏

2018-03-14 15:56 309 查看
【题目链接】
点击打开链接

【思路要点】
补档博客,无题解。
【代码】
#include<bits/stdc++.h>
using namespace std;
#define MAXN	100005
#define MAXLOG	20
struct edge {int dest; long long len; };
struct info {int value; };
vector <edge> a[MAXN];
set <info> BBT;
long long dist[MAXN];
int timer, dfn[MAXN], depth[MAXN], father[MAXN][MAXLOG];
void init(int pos, int fa, int dep, long long length) {
depth[pos] = dep;
father[pos][0] = fa;
dist[pos] = length;
dfn[pos] = ++timer;
for (int i = 1; i < MAXLOG; i++)
father[pos][i] = father[father[pos][i - 1]][i - 1];
for (unsigned i = 0; i < a[pos].size(); i++)
if (a[pos][i].dest != fa) init(a[pos][i].dest, pos, dep + 1, length + a[pos][i].len);
}
int lca(int x, int y) {
if (depth[x] < depth[y]) swap(x, y);
for (int i = MAXLOG - 1; i >= 0; i--)
if (depth[father[x][i]] >= depth[y]) x = father[x][i];
if (x == y) return x;
for (int i = MAXLOG - 1; i >= 0; i--)
if (father[x][i] != father[y][i]) {
x = father[x][i];
y = father[y][i];
}
return father[x][0];
}
long long length(int x, int y) {
return dist[x] + dist[y] - dist[lca(x, y)] * 2;
}
bool operator < (info x, info y) {
return dfn[x.value] < dfn[y.value];
}
bool operator > (info x, info y) {
return dfn[x.value] > dfn[y.value];
}
int func(info tmp) {
set <info> :: iterator tnp;
tnp = BBT.end(); tnp--;
if (*tnp < tmp) return (*tnp).value;
tnp = BBT.lower_bound(tmp); tnp--;
return (*tnp).value;
}
int main() {
int n, m;
scanf("%d%d", &n, &m);
for (int i = 1; i < n; i++) {
int x, y, z;
scanf("%d%d%d", &x, &y, &z);
a[x].push_back((edge){y, z});
a[y].push_back((edge){x, z});
}
init(1, 0, 1, 0);
long long ans = 0;
for (int i = 1; i <= m; i++) {
info tmp;
scanf("%d", &tmp.value);
if (BBT.empty()) {
BBT.insert(tmp);
printf("0\n");
continue;
}
if (BBT.count(tmp)) {
BBT.erase(tmp);
if (BBT.empty()) {
printf("0\n");
continue;
}
set <info> :: iterator tnp;
tnp = BBT.end(); tnp--;
if (*BBT.begin() < tmp) ans -= length(func(tmp), tmp.value);
else ans -= length((*tnp).value, tmp.value);
if (*tnp > tmp) ans -= length((*BBT.lower_bound(tmp)).value, tmp.value);
else ans -= length((*BBT.begin()).value, tmp.value);
if (*BBT.begin() < tmp && *tnp > tmp) ans += length(func(tmp), (*BBT.lower_bound(tmp)).value);
else if (*BBT.begin() < tmp) ans += length(func(tmp), (*BBT.begin()).value);
else ans += length((*tnp).value, (*BBT.lower_bound(tmp)).value);
} else {
if (BBT.empty()) {
BBT.insert(tmp);
printf("0\n");
continue;
}
set <info> :: iterator tnp;
tnp = BBT.end(); tnp--;
if (*BBT.begin() < tmp) ans += length(func(tmp), tmp.value);
else ans += length((*tnp).value, tmp.value);
if (*tnp > tmp) ans += length((*BBT.lower_bound(tmp)).value, tmp.value);
else ans += length((*BBT.begin()).value, tmp.value);
if (*BBT.begin() < tmp && *tnp > tmp) ans -= length(func(tmp), (*BBT.lower_bound(tmp)).value);
else if (*BBT.begin() < tmp) ans -= length(func(tmp), (*BBT.begin()).value);
else ans -= length((*tnp).value, (*BBT.lower_bound(tmp)).value);
BBT.insert(tmp);
}
printf("%lld\n", ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: