#include "factories.h"
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#ifdef DEBUG
#include <time.h>
#endif
#define all(a) (a).begin(), (a).end()
#define rev(a) (a).rbegin(), (a).rend()
#define F first
#define S second
int recur_depth = 0;
#ifdef DEBUG
#define dbg(x) \
{ \
++recur_depth; \
auto x_ = x; \
--recur_depth; \
cerr << string(recur_depth, '\t') << "\e[91m" << __func__ << ":" \
<< __LINE__ << "\t" << #x << " = " << x_ << "\e[39m" << endl; \
}
#else
#define dbg(x)
#endif
using namespace std;
using namespace __gnu_pbds;
typedef pair<int, int> ii;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> llll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pair<int, int>> vii;
typedef vector<vii> vvii;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<pair<ll, ll>> vll;
typedef vector<vll> vvll;
typedef vector<bool> vb;
template <class type1>
using ordered_set = tree<type1, null_type, less<type1>, rb_tree_tag,
tree_order_statistics_node_update>;
template <typename A, typename B>
ostream &operator<<(ostream &os, const pair<A, B> &p) {
return os << '(' << p.first << ", " << p.second << ')';
}
template <typename T_container, typename T = typename enable_if<
!is_same<T_container, string>::value,
typename T_container::value_type>::type>
ostream &operator<<(ostream &os, const T_container &v) {
os << '{';
string sep;
for (const T &x : v)
os << sep << x, sep = ", ";
return os << '}';
}
const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
const ll INF = 1e18;
const ld EPS = 1e-9;
struct ST {
const int MAXLG = 22;
int n;
vvll st;
vl lg2;
void init(int n_, vll a) {
n = n_;
st = vvll(MAXLG, vll(n));
st[0] = a;
for (int j = 1; j < MAXLG; j++)
for (int i = 0; i + (1 << j) <= n; i++)
st[j][i] = min(st[j - 1][i], st[j - 1][i + (1 << (j - 1))]);
lg2 = vl(n + 10);
lg2[1] = 0;
for (int i = 2; i < lg2.size(); i++)
lg2[i] = lg2[i / 2] + 1;
}
llll query(int l, int r) {
if (l > r)
swap(l, r);
int k = lg2[r - l + 1];
return min(st[k][l], st[k][r - (1 << k) + 1]);
}
};
struct CD {
int n;
vvll adj;
vl sub_sz;
vb rm;
vvl cchild;
vl cpar;
vl dist_0;
vl depth;
vll euler;
vl first_occ;
ST st;
vl val;
void init(vvll a) {
adj = a;
n = adj.size();
sub_sz = vl(n);
rm = vb(n);
cchild = vvl(n);
cpar = vl(n, -1);
dist_0 = vl(n, INF);
depth = vl(n, -1);
first_occ = vl(n);
val = vl(n, INF);
dfs(0);
decompose();
st.init(euler.size(), euler);
}
void dfs(int u, int p = -1, ll d = 0, ll de = 0) {
dist_0[u] = d;
depth[u] = de;
first_occ[u] = euler.size();
euler.push_back({de, u});
for (auto [v, w] : adj[u])
if (v != p) {
dfs(v, u, d + w, de + 1);
euler.push_back({de, u});
}
}
ll lca(ll u, ll v) { return st.query(first_occ[u], first_occ[v]).second; }
ll dist(ll u, ll v) { return dist_0[u] + dist_0[v] - 2 * dist_0[lca(u, v)]; }
ll get_sub_sz(int u, int p = -1) {
sub_sz[u] = 1;
for (auto [v, _] : adj[u])
if (v != p && !rm[v])
sub_sz[u] += get_sub_sz(v, u);
return sub_sz[u];
}
ll get_centroid(int u, ll ssz, int p = -1) {
for (auto [v, _] : adj[u])
if (v != p && !rm[v] && sub_sz[v] * 2 > ssz)
return get_centroid(v, ssz, u);
return u;
}
void decompose(int u = 0, int par = -1) {
ll c = get_centroid(u, get_sub_sz(u));
rm[c] = 1;
if (par != -1)
cchild[par].push_back(c), cpar[c] = par;
for (auto [v, _] : adj[c])
if (!rm[v])
decompose(v, c);
}
void upd(int u, bool flag) {
for (int v = u; v != -1; v = cpar[v]) {
if (!flag)
val[v] = min(val[v], dist(u, v));
else
val[v] = INF;
}
}
ll query(int u) {
ll res = INF;
for (int v = u; v != -1; v = cpar[v])
res = min(res, val[v] + dist(u, v));
return res;
}
} cd;
void Init(int N, int A[], int B[], int D[]) {
vvll adj(N);
for (int i = 0; i < N - 1; i++)
adj[A[i]].push_back({B[i], D[i]}), adj[B[i]].push_back({A[i], D[i]});
cd.init(adj);
}
long long Query(int S, int X[], int T, int Y[]) {
ll res = INF;
for (int i = 0; i < S; i++)
cd.upd(X[i], 0);
for (int i = 0; i < T; i++)
res = min(res, cd.query(Y[i]));
for (int i = 0; i < S; i++)
cd.upd(X[i], 1);
return res;
}
Compilation message
factories.cpp: In member function 'void ST::init(int, vll)':
factories.cpp:110:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
110 | for (int i = 2; i < lg2.size(); i++)
| ~~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
1236 KB |
Output is correct |
2 |
Correct |
341 ms |
22560 KB |
Output is correct |
3 |
Correct |
475 ms |
20552 KB |
Output is correct |
4 |
Correct |
443 ms |
20548 KB |
Output is correct |
5 |
Correct |
416 ms |
20792 KB |
Output is correct |
6 |
Correct |
206 ms |
20696 KB |
Output is correct |
7 |
Correct |
440 ms |
20636 KB |
Output is correct |
8 |
Correct |
461 ms |
20764 KB |
Output is correct |
9 |
Correct |
404 ms |
20768 KB |
Output is correct |
10 |
Correct |
180 ms |
20432 KB |
Output is correct |
11 |
Correct |
435 ms |
20500 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
980 KB |
Output is correct |
2 |
Runtime error |
1235 ms |
524288 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
1236 KB |
Output is correct |
2 |
Correct |
341 ms |
22560 KB |
Output is correct |
3 |
Correct |
475 ms |
20552 KB |
Output is correct |
4 |
Correct |
443 ms |
20548 KB |
Output is correct |
5 |
Correct |
416 ms |
20792 KB |
Output is correct |
6 |
Correct |
206 ms |
20696 KB |
Output is correct |
7 |
Correct |
440 ms |
20636 KB |
Output is correct |
8 |
Correct |
461 ms |
20764 KB |
Output is correct |
9 |
Correct |
404 ms |
20768 KB |
Output is correct |
10 |
Correct |
180 ms |
20432 KB |
Output is correct |
11 |
Correct |
435 ms |
20500 KB |
Output is correct |
12 |
Correct |
2 ms |
980 KB |
Output is correct |
13 |
Runtime error |
1235 ms |
524288 KB |
Execution killed with signal 9 |
14 |
Halted |
0 ms |
0 KB |
- |