QTREE4

#pragma comment(linker, "/STACK:102400000,102400000")
//#include<bits/stdc++.h>
#include <ctime>
#include <iostream>
#include <assert.h>
#include <vector>
#include <queue>
#include <cstdio>
#include <algorithm>
#include <cstring>
#define fi first
#define se second
#define endl '\n'
#define o2(x) (x)*(x)
#define BASE_MAX 31
#define mk make_pair
#define eb push_back
#define SZ(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define clr(a, b) memset((a),(b),sizeof((a)))
#define iis std::ios::sync_with_stdio(false); cin.tie(0)
#define my_unique(x) sort(all(x)),x.erase(unique(all(x)),x.end())
using namespace std;
#pragma optimize("-O3")
typedef long long LL;
typedef unsigned long long uLL;
typedef pair<int, int> pii;
inline LL read() {
LL x = 0;int f = 0;
char ch = getchar();
while (ch < '0' || ch > '9') f |= (ch == '-'), ch = getchar();
while (ch >= '0' && ch <= '9') x = (x << 3) + (x << 1) + ch - '0', ch = getchar();
return x = f ? -x : x;
}
inline void write(LL x, bool f) {
if (x == 0) {putchar('0'); if(f)putchar('\n');else putchar(' ');return;}
if (x < 0) {putchar('-');x = -x;}
static char s[23];
int l = 0;
while (x != 0)s[l++] = x % 10 + 48, x /= 10;
while (l)putchar(s[--l]);
if(f)putchar('\n');else putchar(' ');
}
int lowbit(int x) { return x & (-x); }
template<class T>T big(const T &a1, const T &a2) { return a1 > a2 ? a1 : a2; }
template<class T>T sml(const T &a1, const T &a2) { return a1 < a2 ? a1 : a2; }
template<typename T, typename ...R>T big(const T &f, const R &...r) { return big(f, big(r...)); }
template<typename T, typename ...R>T sml(const T &f, const R &...r) { return sml(f, sml(r...)); }
void debug_out() { cerr << '\n'; }
template<typename T, typename ...R>void debug_out(const T &f, const R &...r) {cerr << f << " ";debug_out(r...);}
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]: ", debug_out(__VA_ARGS__); const LL INFLL = 0x3f3f3f3f3f3f3f3fLL;
const int HMOD[] = {1000000009, 1004535809};
const LL BASE[] = {1572872831, 1971536491};
const int mod = 1e9 + 7;//998244353
const int MOD = 1e9 + 7;
const int INF = 0x3f3f3f3f;
const int MXN = 2e5 + 7;
const int MXE = 4e5 + 7;
int n, m;
struct lp {
int v, nex;
int w;
} cw[MXE];
int tot, head[MXN]; namespace LCA {
int dis[MXN], up[MXN][20], lens[MXN];
void dfs(int u, int ba, int d) {
up[u][0] = ba; lens[u] = d;
for(int i = 1; i < 20; ++i) up[u][i] = up[up[u][i-1]][i-1];
for(int i = head[u], v; ~i; i = cw[i].nex) {
v = cw[i].v;
if(v == ba) continue;
dis[v] = dis[u] + cw[i].w;
dfs(v, u, d + 1);
}
}
int lca(int x, int y) {
if(lens[x] < lens[y]) swap(x, y);
for(int i = 19; i >= 0; --i) {
if(lens[up[x][i]] >= lens[y]) {
x = up[x][i];
}
}
if(x == y) return x;
for(int i = 19; i >= 0; --i) {
if(up[x][i] != up[y][i]) {
x = up[x][i], y = up[y][i];
}
}
return up[x][0];
}
int query(int i, int j) {
return dis[i] + dis[j] - 2 * dis[lca(i, j)];
}
} //namespace LCA {
// int dis[MXN], up[MXN][20], lg[MXN], DEP[MXN];
// void dfs(int u, int ba) {
// DEP[u] = DEP[ba] + 1;
// for(int i = head[u], v; ~i; i = cw[i].nex) {
// v = cw[i].v;
// if(v == ba) continue;
// dis[v] = dis[u] + cw[i].w, up[v][0] = u;
// dfs(v, u);
// }
// }
// void init() {
// for (int i = 2; i <= n; ++i) lg[i] = lg[i / 2] + 1;;
// dis[1] = 0;
// dfs(1, -1);
// for (int j = 1; j <= lg[n]; ++j)
// for (int i = 1; i <= n; ++i) up[i][j] = up[up[i][j - 1]][j - 1];
// }
// int lca(int x, int y) {
// if (DEP[x] > DEP[y]) swap(x, y);
// int k = DEP[y] - DEP[x];
// for (int i = 0; k; k = k / 2, ++i)
// if (k & 1) y = up[y][i];
// if (x == y) return x;
// k = DEP[x];
// for (int i = lg[k]; i >= 0; --i)
// if (up[x][i] != up[y][i]) x = up[x][i], y = up[y][i];
// return up[x][0];
// }
// int query(int i, int j) {
// return dis[i] + dis[j] - 2 * dis[lca(i, j)];
// }
//} //namespace LCA {
// int dis[MXN], up[MXN][20], lens[MXN];
// int cnt, dfn[MXN], en[MXN], LOG[MXN];
// void dfs(int u, int ba) {
// lens[u] = lens[ba] + 1;
// dfn[++cnt] = u;
// en[u] = cnt;
// for(int i = head[u], v; ~i; i = cw[i].nex) {
// v = cw[i].v;
// if(v == ba) continue;
// dis[v] = dis[u] + cw[i].w;
// dfs(v, u);
// dfn[++ cnt] = u;
// }
// }
// inline int cmp(int u, int v) {
// return lens[u] < lens[v] ? u: v;
// }
// void init() {
// cnt = 0;
// dis[0] = lens[0] = 0;
// dfs(1, 0);
// LOG[1] = 0;
// for(int i = 2; i <= cnt; ++i) LOG[i] = LOG[i-1] + ((1<<(LOG[i-1]+1))==i);
// for(int i = 1; i <= cnt; ++i) up[i][0] = dfn[i];
// for(int j = 1; (1<<j) <= cnt; ++j)
// for(int i = 1; i + (1<<j) -1 <= cnt; ++i)
// up[i][j] = cmp(up[i][j-1], up[i+(1<<(j-1))][j-1]);
// }
// inline int lca(int x, int y) {
// int l = en[x], r = en[y];
// if(l > r) swap(l, r);
// int k = LOG[r - l + 1];
// return cmp(up[l][k], up[r-(1<<k)+1][k]);
// }
// inline int query(int i, int j) {
// return dis[i] + dis[j] - 2 * dis[lca(i, j)];
// }
//} int siz[MXN], hvy, hvysiz, mxsz, vis[MXN];
int col[MXN], fa[MXN], dis[MXN][20];
int dep[MXN];
struct heap {
priority_queue<int> A, B; // heap=A-B
void insert(int x) { A.push(x); }
void erase(int x) { B.push(x); }
inline int size() { return A.size() - B.size(); }
inline int top() {
if(size() <= 0) return -INF;
while (!B.empty() && A.top() == B.top()) A.pop(), B.pop();
return A.top();
}
inline void pop() {
while (!B.empty() && A.top() == B.top()) A.pop(), B.pop();
A.pop();
}
inline int top2() {
int t = top(), ret;
pop();
ret = top();
A.push(t);
return ret;
}
inline int mx() {
if(size() < 2) return - INF;
int t = top(), ret;
pop();
ret = top();
A.push(t);
return ret + t;
}
} disf[MXN], dison[MXN], ans;
inline void add_edge(int a, int b, int c) {
cw[++ tot].v = b, cw[tot].nex = head[a], cw[tot].w = c;
head[a] = tot;
cw[++ tot].v = a, cw[tot].nex = head[b], cw[tot].w = c;
head[b] = tot;
}
inline int get_dis(int i, int j) {
if(i == j) return 0;
if(dis[i][dep[i] - dep[j]]) return dis[i][dep[i] - dep[j]];
dis[i][dep[i] - dep[j]] = LCA::query(i, j);
return dis[i][dep[i] - dep[j]];
}
void dfs_pre(int u, int ba) {
siz[u] = 1;
int mm = 0;
for(int i = head[u], v; ~i; i = cw[i].nex) {
v = cw[i].v;
if(v == ba || vis[v]) continue;
dfs_pre(v, u);
siz[u] += siz[v];
mm = max(mm, siz[v]);
}
mm = max(mm, mxsz - siz[u]);
if(hvy == -1 || mm < hvysiz) {
hvy = u;
hvysiz = mm;
}
}
void dfs_siz(int u, int ba) {
siz[u] = 1;
for(int i = head[u], v; ~i; i = cw[i].nex) {
v = cw[i].v;
if(v == ba || vis[v]) continue;
dfs_siz(v, u);
siz[u] += siz[v];
}
}
void dfs_dis(int u, int ba, int d, int rt) {
disf[rt].insert(d);
for(int i = head[u], v; ~i; i = cw[i].nex) {
v = cw[i].v;
if(v == ba || vis[v]) continue;
dfs_dis(v, u, d + cw[i].w, rt);
}
}
void dfs_get(int lstrt) {
// debug(u, _n, hvy)
// int lstrt = u;
vis[lstrt] = 1;
assert(lstrt == hvy);
dfs_siz(lstrt, -1);
dison[lstrt].insert(0);//response itself
for(int i = head[lstrt], v; ~i; i = cw[i].nex) {
v = cw[i].v;
if(vis[v]) continue;
hvy = -1;
hvysiz = mxsz = siz[v];
dfs_pre(v, lstrt);
dfs_dis(v, lstrt, cw[i].w, hvy);
fa[hvy] = lstrt;
dep[hvy] = dep[lstrt] + 1;
int tmp = disf[hvy].top();
if(tmp != - INF) dison[lstrt].insert(tmp);
dfs_get(hvy);
}
int tmp = dison[lstrt].mx();
if(tmp != - INF) ans.insert(tmp);
}
void change(int x, int open) {
int preV = dison[x].mx(), nowV;
if(open) dison[x].erase(0);
else dison[x].insert(0);
nowV = dison[x].mx();
if(preV != nowV) {
if(preV != -INF) ans.erase(preV);
if(nowV != -INF) ans.insert(nowV);
}
for(int u = x, ba; fa[u]; u = fa[u]) {
ba = fa[u];
// debug(x, i, fa[i])
int tmp = disf[u].top();
preV = dison[ba].mx();
if(open) disf[u].erase(get_dis(x, ba));
else disf[u].insert(get_dis(x, ba));
nowV = disf[u].top();
if(tmp != nowV) {
if(tmp != -INF) dison[ba].erase(tmp);
if(nowV != - INF) dison[ba].insert(nowV);
}
nowV = dison[ba].mx();
if(preV != nowV) {
if(preV != -INF) ans.erase(preV);
if(nowV != -INF) ans.insert(nowV);
}
}
}
int main() {
#ifndef ONLINE_JUDGE
freopen("/home/cwolf9/CLionProjects/ccc/in.txt", "r", stdin);
//freopen("/home/cwolf9/CLionProjects/ccc/out.txt", "w", stdout);
#endif
n = read();
for(int i = 1; i <= n; ++i) head[i] = -1, col[i] = 0;
tot = -1;
for(int i = 1, a, b, c; i < n; ++i) {
a = read(), b = read(), c = read();
add_edge(a, b, c);
}
// LCA::init();
LCA::dfs(1, 0, 0);
hvy = -1;
hvysiz = mxsz = n;
dfs_pre(1, -1);
dfs_get(hvy);
m = read();
// debug(m, get_dis(1, 3), dep[1], dep[3])
char op[3];
int x, cnt = n;
while(m --) {
scanf("%s", op);
if(op[0] == 'A') {
if(cnt == 0) printf("They have disappeared.\n");
else if(cnt == 1) printf("0\n");
else printf("%d\n", max(ans.top(), 0));
}else {
x = read();
if(col[x] == 0) {
change(x, 1);
-- cnt;
}else {
change(x, 0);
++ cnt;
}
col[x] = !col[x];
}
}
#ifndef ONLINE_JUDGE
cout << "time cost:" << 1.0*clock()/CLOCKS_PER_SEC << "ms" << endl;
#endif
return 0;
}

SPOJ_QTREE系列题解的更多相关文章

  1. IEEE Bigger系列题解

    Bigger系列题解 Bigger Python 坑点在于要高精度以及表达式求值,用java写可以很容易避免高精度问题 然后这道题就可以AC了 代码 import java.io.*; import ...

  2. QTREE系列题解

    打了快一星期的qtree终于打完了- - (其实还有两题改不出来弃疗了QAQ) orz神AK一星期前就虐完QTREE 避免忘记还是简单写下题解吧0 0 QTREE1 题意: 给出一颗带边权树 一个操作 ...

  3. 【题解】NOI 系列题解总集

    每次做一道 NOI 系列的估计都很激动吧,对于我这种萌新来说( P1731 [NOI1999]生日蛋糕 练习剪枝技巧,关于剪枝,欢迎看我的垃圾无意义笔记 这道题是有一定难度的,需要运用各种高科技剪枝( ...

  4. GSS 系列题解

    GSS GSS1 随便猫树或者线段树,就可以过了 猫树不说,线段树可以维护左边最大,右边最大,区间最大,区间值然后就做出来了. //Isaunoya #pragma GCC optimize(2) # ...

  5. GSS系列题解——最大子段和系列

    开坑啦! 2019 3/28 以前一直不知道怎么搞最大子段和,如今终于可以学习,其实真的很简单啊. 2019 3/29 树链剖分上最大子段和也OK啦 前置技能:线段树 题目大意:询问区间[l,r]的最 ...

  6. 纪中OJ 2019.02.15【NOIP提高组】模拟 B 组 梦回三国 比赛题解(第一个)

    声明 旁边的同学小 H(胡)对我说: “哟,比赛拿了 140,强!要知道,如果哥第三题 AC 了,哥就 230 了,你个废柴!!!(比赛实际分数 130 额呵)” 顿时,千万草泥马从我心中奔腾而过:你 ...

  7. [Leetcode] Sum 系列

    Sum 系列题解 Two Sum题解 题目来源:https://leetcode.com/problems/two-sum/description/ Description Given an arra ...

  8. [Leetcode] N-Queens 系列

    N-Queens 系列题解 题目来源: N-Queens N-Queens II N-Queens The n-queens puzzle is the problem of placing n qu ...

  9. [Leetcode] Search in Rotated Sorted Array 系列

    Search in Rotated Sorted Array 系列题解 题目来源: Search in Rotated Sorted Array Search in Rotated Sorted Ar ...

随机推荐

  1. [CSP-S模拟测试]:u(差分)

    题目背景 $\frac{1}{4}$遇到了一道水题,完全不会做,于是去请教小$D$.小$D$看了一眼就切掉了这题,嘲讽了$\frac{1}{4}$一番就离开了.于是,$\frac{1}{4}$只好来问 ...

  2. DCloud-MUI-JS:相关摘录

    ylbtech-DCloud-MUI-JS:相关摘录 1.返回顶部 1.JS initFun: function() { var oldBack = mui.back; mui.back = func ...

  3. JS 提取公式中的参数

    'A+B-C/D*E'.split(/[*/()+-]/)  => [A,B,C,D,E]

  4. 【JavaScript性能优化】------理解Script标签的加载和执行

    1.script标签是如何加载的?当浏览器遇到一个 < script>标签时,浏览器会停下来,运行JavaScript代码,然后再继续解析.翻译页面.同样的事情发生在使用 src 属性加载 ...

  5. python-javascript之dom

    DOM DOM:(document object mode)文档对象模型.DOM为文档提供了结构化表示,并定义了如何通过脚本来访问文档结构. 目的就是为了能让js操作html元素而制定的一个规范 DO ...

  6. mybatis关联查询之一对一查询

    一对一也就是 A 表的一条记录对应 B 表的一条记录,下面的测试数据中,从employee 表来看,一个员工对应一个部门,是一对一关系,如果从部门角度来看,则是一对多的关系,一个部门对应多个员工,本节 ...

  7. 这么简单的 Redis 面试题都不懂,怎么拿offer?

    来源:mp.weixin.qq.com/s/daBkliC8dAT_zYyoLiS7WA 随着系统访问量的提高,复杂度的提升,响应性能成为一个重点的关注点.而缓存的使用成为一个重点.redis 作为缓 ...

  8. 实用js片段

    算法 //加法 add(10,2) //12 function add(a, b) { var c, d, e; try { c = a.toString().split(".") ...

  9. 高精度求A*B(FFT)

    A * B Problem Plus 链接:http://acm.hdu.edu.cn/showproblem.php?pid=1402 Time Limit: 2000/1000 MS (Java/ ...

  10. k8s进行与容器交互时报错:unable to upgrade connection: Unauthorized在k8s实现kubectl exec -it pod_ID sh出错解决

    在创建pod时,进入pod失败 kubectl exec -it nginx-deployment-d55b94fd-xcrtg sh error检查问题,一直找不到答案,通过logs发现,同样不能实 ...