P3320 [SDOI2015]寻宝游戏
生活随笔
收集整理的這篇文章主要介紹了
P3320 [SDOI2015]寻宝游戏
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
P3320 [SDOI2015]尋寶游戲
若當前點集S, s[i]的dfn為a[i], 且a單調.
則答案即:d(a[1], a[2]) + d(a[2], a[3]) +… + d(a[n - 1], a[n]) + d(a[n], a[1]).
set維護.(set用法見藍書.)
代碼1如下:
代碼2如下:
#include <cstdio> #include <cctype> #include <algorithm> #include <cmath> #include <set>using namespace std; #define ll long longinline int read() {int x = 0, f = 0; char ch = getchar(); while (!isdigit(ch)) f = ch == '-', ch = getchar(); while (isdigit(ch)) x = (x << 3) + (x << 1) + (ch ^ 48), ch = getchar(); return f ? -x : x; }inline void print(ll x) {if (x < 0) x = -x; if (x < 10) putchar(x + '0'); else {print(x / 10); putchar(x % 10 + '0'); } }const int N = 1e5 + 5; int n, m; int head[N], nex[N << 1], ver[N << 1], val[N << 1], tot; int dfn[N], idf[N], num; int fa[N][20], dep[N]; set<int> s; set<int>::iterator it; ll dist[N], ans; int vis[N]; void Addedge(int x, int y, int z) {ver[++tot] = y; val[tot] = z; nex[tot] = head[x]; head[x] = tot; }void dfs(int x, int fat) {dfn[x] = ++num; idf[num] = x; fa[x][0] = fat; dep[x] = dep[fat] + 1; for (int i = 1; i < 20; ++i) {fa[x][i] = fa[fa[x][i - 1]][i - 1]; }for (int i = head[x]; i; i = nex[i]) {int y = ver[i], z = val[i]; if (y == fat) continue; dist[y] = dist[x] + 1ll * z; dfs(y, x); } }int lca(int x, int y) {if (dep[x] > dep[y]) swap(x, y); while (dep[x] < dep[y]) y = fa[y][(int)log2(dep[y] - dep[x])]; if (x == y) return x; for (int i = 19; i >= 0; --i) {if (fa[x][i] != fa[y][i]) {x = fa[x][i]; y = fa[y][i]; }}return fa[x][0]; }ll Getd(int x, int y) {int f = lca(x, y); return dist[x] + dist[y] - (dist[f] << 1); }int main() {n = read(); m = read(); for (int i = 1; i < n; ++i) {int uu = read(), vv = read(), ww = read(); Addedge(uu, vv, ww); Addedge(vv, uu, ww); }dfs(1, 0); while (m--) {int t = read(), x = dfn[t]; if (!vis[t]) {vis[t] = 1; s.insert(x); int pre = idf[(it = s.lower_bound(x)) == s.begin() ? *--s.end() : *--it], nxt = idf[(it = s.lower_bound(x)) == --s.end() ? *s.begin() : *++it];ans += Getd(t, pre) + Getd(t, nxt) - Getd(pre, nxt); } else {vis[t] = 0;int pre = idf[(it = s.lower_bound(x)) == s.begin() ? *--s.end() : *--it], nxt = idf[(it = s.lower_bound(x)) == --s.end() ? *s.begin() : *++it];ans -= Getd(t, pre) + Getd(t, nxt) - Getd(pre, nxt); s.erase(x); }print(ans); putchar('\n'); }return 0; }經實測,代碼2優于代碼1.
迭代器:
set<int>::iterator it;
++it; --it; it++; it--;
s.begin() --s.end() O(1)
s.insert(x); s.erase(x); s.find(x); //s.find(x)不存在x返回x.end()
s.lower_bound(x) s.upper_bound(x) // >= >
總結
以上是生活随笔為你收集整理的P3320 [SDOI2015]寻宝游戏的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 拥有多丽体质特膳 你也能和女神般光彩照人
- 下一篇: 世界银行为孟加拉国建设数据中心提供贷款