This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/**
____ ____ ____ ____ ____ ____
||l |||e |||i |||n |||a |||d ||
||__|||__|||__|||__|||__|||__||
|/__\|/__\|/__\|/__\|/__\|/__\|
**/
#include<bits/stdc++.h>
#define endl '\n'
using namespace std;
typedef long long ll;
void speed()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
}
const int maxn = 1e5 + 10;
struct query
{
int l, r;
}task[maxn];
int n, m, c[maxn], q;
vector < int > adj[maxn];
void input()
{
cin >> n >> m >> q;
for (int i = 1; i < n; i ++)
{
int v, u;
cin >> v >> u;
adj[v].push_back(u);
adj[u].push_back(v);
}
for (int i = 1; i <= m; i ++)
{
cin >> c[i];
}
for (int i = 1; i <= q; i ++)
{
cin >> task[i].l >> task[i].r;
}
}
int depth[maxn], tin[maxn], tout[maxn];
int occ[2 * maxn], timer;
void euler(int v = 1, int p = - 1)
{
occ[++ timer] = v;
tin[v] = timer;
for (int u : adj[v])
{
if (u == p)
continue;
depth[u] = depth[v] + 1;
euler(u, v);
occ[++ timer] = v;
}
tout[v] = timer;
}
const int maxlog = 20;
int lg[2 * maxn], dp[maxlog][2 * maxn];
void sparse_table()
{
for (int i = 1; i <= timer; i ++)
{
lg[i] = lg[i / 2] + 1;
dp[0][i] = occ[i];
}
for (int j = 1; j < lg[timer]; j ++)
for (int i = 1; i <= timer - (1 << j); i ++)
{
dp[j][i] = dp[j - 1][i + (1 << (j - 1))];
if (depth[dp[j - 1][i]] < depth[dp[j][i]])
dp[j][i] = dp[j - 1][i];
}
}
int get_lca(int v, int u)
{
int l = tin[v], r = tin[u];
if (l > r)
swap(l, r);
int len = lg[r - l + 1] - 1;
int lca = dp[len][r - (1 << len) + 1];
if (depth[dp[len][l]] < depth[lca])
lca = dp[len][l];
return lca;
}
int get_distance(int v, int u)
{
return depth[v] + depth[u] - 2 * depth[get_lca(v, u)];
}
bool cmp_tin(int v, int u)
{
return tin[v] < tin[u];
}
void solve_query(int left, int right)
{
vector < int > ord;
int global_lca = c[left];
for (int i = left; i <= right; i ++)
{
ord.push_back(c[i]);
global_lca = get_lca(global_lca, c[i]);
}
sort(ord.begin(), ord.end(), cmp_tin);
int ans = 0;
for (int i = 0; i < ord.size(); i ++)
{
ans = ans + depth[ord[i]] - depth[global_lca];
if (i > 0)
{
ans = ans - (depth[get_lca(ord[i - 1], ord[i])] - depth[global_lca]);
}
}
cout << ans + 1 << endl;
}
void queries()
{
for (int i = 1; i <= q; i ++)
solve_query(task[i].l, task[i].r);
}
void solve()
{
input();
euler();
sparse_table();
queries();
}
int main()
{
speed();
solve();
return 0;
}
Compilation message (stderr)
tourism.cpp: In function 'void solve_query(int, int)':
tourism.cpp:128:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
128 | for (int i = 0; i < ord.size(); i ++)
| ~~^~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |