#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define int long long
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define FORD(i, a, b) for (int i = (b); i >= (a); i --)
#define REP(i, a) for (int i = 0; i < (a); ++i)
#define REPD(i, a) for (int i = (a) - 1; i >= 0; --i)
#define MASK(i) (1LL << (i))
#define BIT(x, i) (((x) >> (i)) & 1)
constexpr ll LINF = (1ll << 60);
constexpr int INF = (1ll << 30);
constexpr int Mod = 1e9 + 7;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
/*
Phu Trong from Nguyen Tat Thanh High School for gifted student
*/
template <class X, class Y>
bool minimize(X &x, const Y &y){
X eps = 1e-9;
if (x > y + eps) {x = y; return 1;}
return 0;
}
template <class X, class Y>
bool maximize(X &x, const Y &y){
X eps = 1e-9;
if (x + eps < y) {x = y; return 1;}
return 0;
}
#define MIN_HIGH(u, v) (depth[u] < depth[v] ? (u) : (v))
#define LOG 20
#define MAX 50005
int numNode, numQuery;
struct Edge{
int u, v, w;
Edge(){}
Edge(int _u, int _v, int _w): u(_u), v(_v), w(_w){}
int other(int x){
return (x ^ u ^ v);
}
} edge[MAX];
vector<int> G[MAX];
namespace DistanceOnTree{
int tin[MAX], timeDfs = 0, rev[MAX];
int cnt = 0, st[MAX << 1][20], pos[MAX << 1], d[MAX], depth[MAX];
void pre_dfs(int u, int p = -1){
tin[u] = ++timeDfs; rev[timeDfs] = u;
st[++cnt][0] = u; pos[u] = cnt;
for (int&i : G[u]){
int v = edge[i].other(u);
if(v ^ p){
depth[v] = depth[u] + 1;
d[v] = d[u] + edge[i].w;
pre_dfs(v, u);
st[++cnt][0] = u;
}
}
}
void build(void){
pre_dfs(1);
for (int i = 1; MASK(i) <= cnt; ++i){
for (int u = 1; u + MASK(i) - 1 <= cnt; ++u){
st[u][i] = MIN_HIGH(st[u][i - 1], st[u + MASK(i - 1)][i - 1]);
}
}
}
int lca(int u, int v){
int l = pos[u], r = pos[v];
if(l > r) swap(l, r);
int k = 31 - __builtin_clz(r - l + 1);
return MIN_HIGH(st[l][k], st[r - MASK(k) + 1][k]);
}
set<int> mySet;
int sum = 0;
int dis(int u, int v){
return d[u] + d[v] - 2 * d[lca(u, v)];
}
void ins(int x){
x = tin[x];
if(mySet.empty()){
mySet.insert(x);
return;
}
if(mySet.size() == 1){
sum += 2 * dis(rev[*mySet.begin()], rev[x]);
mySet.insert(x);
return;
}
auto it = mySet.lower_bound(x);
int l = -1, r = -1;
if(it == mySet.begin() || it == mySet.end()){
l = rev[*mySet.begin()];
r = rev[*prev(mySet.end())];
}
else{
l = rev[*prev(it)];
r = rev[*it];
}
assert(l != -1); assert(r != -1);
mySet.insert(x);
sum += dis(l, rev[x]);
sum += dis(r, rev[x]);
sum -= dis(l, r);
}
void eras(int x){
x = tin[x];
mySet.erase(x);
if(mySet.size() == 0){
sum = 0;
return;
}
if(mySet.size() == 1){
sum -= 2 * dis(rev[x], rev[*mySet.begin()]);
return;
}
int l = -1, r = -1;
auto it = mySet.lower_bound(x);
if(it == mySet.begin() || it == mySet.end()){
l = rev[*mySet.begin()];
r = rev[*prev(mySet.end())];
}
else{
l = rev[*prev(it)];
r = rev[*it];
}
sum += dis(l, r);
sum -= dis(l, rev[x]);
sum -= dis(r, rev[x]);
}
int ans(void){
return sum / 2;
}
}
#define T DistanceOnTree
void process(void){
cin >> numNode;
for (int i = 1; i < numNode; ++i){
cin >> edge[i].u >> edge[i].v >> edge[i].w;
++edge[i].u, ++edge[i].v;
G[edge[i].u].emplace_back(i);
G[edge[i].v].emplace_back(i);
}
T :: build();
cin >> numQuery;
for (int i = 1; i <= numQuery; ++i){
int node[5];
REP(j, 5) {
cin >> node[j];
++node[j];
T :: ins(node[j]);
}
cout << T :: ans() << '\n';
REP(j, 5){
T :: eras(node[j]);
}
}
}
signed main(){
#define name "Whisper"
cin.tie(nullptr) -> sync_with_stdio(false);
//freopen(name".inp", "r", stdin);
//freopen(name".out", "w", stdout);
process();
return (0 ^ 0);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4956 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
35 ms |
24924 KB |
Output is correct |
2 |
Correct |
38 ms |
26452 KB |
Output is correct |
3 |
Correct |
38 ms |
26508 KB |
Output is correct |
4 |
Correct |
38 ms |
26508 KB |
Output is correct |
5 |
Correct |
50 ms |
26708 KB |
Output is correct |
6 |
Correct |
40 ms |
26456 KB |
Output is correct |
7 |
Correct |
36 ms |
26448 KB |
Output is correct |
8 |
Correct |
40 ms |
26460 KB |
Output is correct |
9 |
Correct |
42 ms |
26500 KB |
Output is correct |
10 |
Correct |
38 ms |
26448 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
27 ms |
23376 KB |
Output is correct |
2 |
Correct |
27 ms |
25680 KB |
Output is correct |
3 |
Correct |
30 ms |
25692 KB |
Output is correct |
4 |
Correct |
38 ms |
25680 KB |
Output is correct |
5 |
Correct |
27 ms |
25680 KB |
Output is correct |
6 |
Correct |
35 ms |
25536 KB |
Output is correct |
7 |
Correct |
29 ms |
25692 KB |
Output is correct |
8 |
Correct |
31 ms |
25760 KB |
Output is correct |
9 |
Correct |
31 ms |
25692 KB |
Output is correct |
10 |
Correct |
33 ms |
25756 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4956 KB |
Output is correct |
2 |
Correct |
35 ms |
24924 KB |
Output is correct |
3 |
Correct |
38 ms |
26452 KB |
Output is correct |
4 |
Correct |
38 ms |
26508 KB |
Output is correct |
5 |
Correct |
38 ms |
26508 KB |
Output is correct |
6 |
Correct |
50 ms |
26708 KB |
Output is correct |
7 |
Correct |
40 ms |
26456 KB |
Output is correct |
8 |
Correct |
36 ms |
26448 KB |
Output is correct |
9 |
Correct |
40 ms |
26460 KB |
Output is correct |
10 |
Correct |
42 ms |
26500 KB |
Output is correct |
11 |
Correct |
38 ms |
26448 KB |
Output is correct |
12 |
Correct |
27 ms |
23376 KB |
Output is correct |
13 |
Correct |
27 ms |
25680 KB |
Output is correct |
14 |
Correct |
30 ms |
25692 KB |
Output is correct |
15 |
Correct |
38 ms |
25680 KB |
Output is correct |
16 |
Correct |
27 ms |
25680 KB |
Output is correct |
17 |
Correct |
35 ms |
25536 KB |
Output is correct |
18 |
Correct |
29 ms |
25692 KB |
Output is correct |
19 |
Correct |
31 ms |
25760 KB |
Output is correct |
20 |
Correct |
31 ms |
25692 KB |
Output is correct |
21 |
Correct |
33 ms |
25756 KB |
Output is correct |
22 |
Correct |
43 ms |
24912 KB |
Output is correct |
23 |
Correct |
30 ms |
23636 KB |
Output is correct |
24 |
Correct |
40 ms |
25936 KB |
Output is correct |
25 |
Correct |
50 ms |
25940 KB |
Output is correct |
26 |
Correct |
35 ms |
26076 KB |
Output is correct |
27 |
Correct |
35 ms |
26144 KB |
Output is correct |
28 |
Correct |
46 ms |
26028 KB |
Output is correct |
29 |
Correct |
39 ms |
25944 KB |
Output is correct |
30 |
Correct |
36 ms |
25944 KB |
Output is correct |
31 |
Correct |
37 ms |
25940 KB |
Output is correct |