#include <bits/stdc++.h>
#include "factories.h"
using namespace std;
const int maxn = 5e5 + 5;
const int base = 32;
const long long inf = 3e18 + 5;
vector<pair<int, long long>> tr[maxn];
int tin[maxn],tout[maxn];
int tour[2*maxn + 5];
int timer = 0;
vector<pair<int, long long>> virtual_tr[maxn];
int ty[maxn];
long long road0[maxn], road1[maxn], d[maxn];
int lg[2 * maxn + 5];
int custom_min(int a, int b){
if(d[a] < d[b]) return a;
return b;
}
struct SpareTable{
int table[base][2*maxn + 5];
void assign(){
build();
init_lg();
}
void build(){
for(int i = 1; i <= timer; i++){
table[0][i] = tour[i];
}
for(int i = 1; i < base; i++){
for(int j = 1; j + (1LL << i) - 1 <= timer; j++){
table[i][j] = custom_min(table[i - 1][j], table[i - 1][j + (1 << (i - 1))]);
}
}
}
void init_lg(){
for(int i = 2; i <= timer; i++){
lg[i] = lg[i / 2] + 1;
}
}
int get(int l, int r){
int len = r - l + 1;
int layer = lg[len];
return custom_min(table[layer][l], table[layer][r - (1 << layer) + 1]);
}
};
SpareTable sp;
void dfs(int u, int p){
tin[u] = ++timer;
tour[timer] = u;
for(pair<int, long long> node : tr[u]){
int v = node.first;
long long w = node.second;
if(v == p) continue;
d[v] = d[u] + w;
dfs(v, u);
tour[++timer] = u;
}
tout[u] = timer;
}
bool isparent(int u, int v){ // u is parent of v
if(tin[u] < tin[v] && tout[v] < tout[u]) return true;
return false;
}
int lca(int u, int v){
if(isparent(u, v)) return u;
else if(isparent(v, u)) return v;
else{
if(tin[u] > tout[v]) return sp.get(tout[v], tin[u]);
else return sp.get(tout[u], tin[v]);
}
}
long long dist(int u, int v){
int c = lca(u, v);
return (d[u] - d[c]) + (d[v] - d[c]);
}
void Init(int N, int A[], int B[], int D[]){
for(int i = 0; i < N - 1; i++){
int a = A[i];
int b = B[i];
int d = D[i];
tr[a].push_back({b, d});
tr[b].push_back({a, d});
}
dfs(0, -1);
sp.assign();
memset(ty, -1, sizeof(ty));
}
void solve(int u, int p){
for(auto node : virtual_tr[u]){
int v = node.first;
long long w = node.second;
if(v == p) continue;
solve(v, u);
road0[u] = min(road0[u], road0[v] + w);
road1[u] = min(road1[u], road1[v] + w);
}
}
long long Query(int S, int X[], int T, int Y[]){
vector<int> temp;
int sz_temp = S + T;
for(int i = 0; i < S; i++){
ty[X[i]] = 0;
road0[X[i]] = inf;
road1[X[i]] = 0;
temp.push_back(X[i]);
}
for(int i = 0; i < T; i++){
if(ty[Y[i]] == 0) return 0;
ty[Y[i]] = 1;
road0[Y[i]] = 0;
road1[Y[i]] = inf;
temp.push_back(Y[i]);
}
sort(temp.begin(), temp.end(), [&](int a, int b){
return tin[a] < tin[b];
});
for(int i = 1; i < sz_temp; i++){
int c = lca(temp[i], temp[i - 1]);
if(ty[c] == -1){
ty[c] = 2;
road0[c] = road1[c] = inf;
temp.push_back(c);
}
}
temp.resize(unique(temp.begin(), temp.end()) - temp.begin());
sort(temp.begin(), temp.end(), [&](int a, int b){
return tin[a] < tin[b];
});
stack<int> stk;
for(int i = 0; i < temp.size(); i++){
if(stk.empty()){
stk.push(temp[i]);
continue;
}
while(!stk.empty() && !isparent(stk.top(), temp[i])){
stk.pop();
}
if(!stk.empty()){
virtual_tr[stk.top()].push_back({temp[i], dist(stk.top(), temp[i])});
}
stk.push(temp[i]);
}
solve(temp[0], -1);
long long result = inf;
for(int i = 0; i < temp.size(); i++){
result = min(result, road0[temp[i]] + road1[temp[i]]);
ty[temp[i]] = -1;
virtual_tr[temp[i]].clear();
}
return result;
}
Compilation message
factories.cpp: In function 'long long int Query(int, int*, int, int*)':
factories.cpp:156:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
156 | for(int i = 0; i < temp.size(); i++){
| ~~^~~~~~~~~~~~~
factories.cpp:175:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
175 | for(int i = 0; i < temp.size(); i++){
| ~~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
26260 KB |
Output is correct |
2 |
Correct |
501 ms |
35036 KB |
Output is correct |
3 |
Correct |
480 ms |
35164 KB |
Output is correct |
4 |
Correct |
467 ms |
35096 KB |
Output is correct |
5 |
Correct |
391 ms |
35468 KB |
Output is correct |
6 |
Correct |
374 ms |
34960 KB |
Output is correct |
7 |
Correct |
481 ms |
35244 KB |
Output is correct |
8 |
Correct |
472 ms |
35152 KB |
Output is correct |
9 |
Correct |
395 ms |
35400 KB |
Output is correct |
10 |
Correct |
376 ms |
34992 KB |
Output is correct |
11 |
Correct |
486 ms |
35012 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
26068 KB |
Output is correct |
2 |
Correct |
960 ms |
161972 KB |
Output is correct |
3 |
Correct |
1089 ms |
167396 KB |
Output is correct |
4 |
Correct |
724 ms |
159576 KB |
Output is correct |
5 |
Correct |
981 ms |
207532 KB |
Output is correct |
6 |
Correct |
1217 ms |
168612 KB |
Output is correct |
7 |
Correct |
819 ms |
60716 KB |
Output is correct |
8 |
Correct |
485 ms |
59872 KB |
Output is correct |
9 |
Correct |
469 ms |
67516 KB |
Output is correct |
10 |
Correct |
745 ms |
61944 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
26260 KB |
Output is correct |
2 |
Correct |
501 ms |
35036 KB |
Output is correct |
3 |
Correct |
480 ms |
35164 KB |
Output is correct |
4 |
Correct |
467 ms |
35096 KB |
Output is correct |
5 |
Correct |
391 ms |
35468 KB |
Output is correct |
6 |
Correct |
374 ms |
34960 KB |
Output is correct |
7 |
Correct |
481 ms |
35244 KB |
Output is correct |
8 |
Correct |
472 ms |
35152 KB |
Output is correct |
9 |
Correct |
395 ms |
35400 KB |
Output is correct |
10 |
Correct |
376 ms |
34992 KB |
Output is correct |
11 |
Correct |
486 ms |
35012 KB |
Output is correct |
12 |
Correct |
15 ms |
26068 KB |
Output is correct |
13 |
Correct |
960 ms |
161972 KB |
Output is correct |
14 |
Correct |
1089 ms |
167396 KB |
Output is correct |
15 |
Correct |
724 ms |
159576 KB |
Output is correct |
16 |
Correct |
981 ms |
207532 KB |
Output is correct |
17 |
Correct |
1217 ms |
168612 KB |
Output is correct |
18 |
Correct |
819 ms |
60716 KB |
Output is correct |
19 |
Correct |
485 ms |
59872 KB |
Output is correct |
20 |
Correct |
469 ms |
67516 KB |
Output is correct |
21 |
Correct |
745 ms |
61944 KB |
Output is correct |
22 |
Correct |
1757 ms |
172436 KB |
Output is correct |
23 |
Correct |
1593 ms |
173300 KB |
Output is correct |
24 |
Correct |
2011 ms |
177776 KB |
Output is correct |
25 |
Correct |
1960 ms |
180808 KB |
Output is correct |
26 |
Correct |
1867 ms |
171224 KB |
Output is correct |
27 |
Correct |
1761 ms |
207252 KB |
Output is correct |
28 |
Correct |
1210 ms |
167456 KB |
Output is correct |
29 |
Correct |
1724 ms |
169928 KB |
Output is correct |
30 |
Correct |
1723 ms |
169076 KB |
Output is correct |
31 |
Correct |
1727 ms |
169824 KB |
Output is correct |
32 |
Correct |
633 ms |
69336 KB |
Output is correct |
33 |
Correct |
625 ms |
62528 KB |
Output is correct |
34 |
Correct |
695 ms |
59604 KB |
Output is correct |
35 |
Correct |
675 ms |
59472 KB |
Output is correct |
36 |
Correct |
821 ms |
60584 KB |
Output is correct |
37 |
Correct |
805 ms |
60376 KB |
Output is correct |