#include <bits/stdc++.h>
using namespace std;
template<class T>
int len(T &a){
return a.size();
}
using ll = long long;
using pii = pair<int,int>;
#define all(a) (a).begin(), (a).end()
#define ff first
#define ss second
string fileio = "";
mt19937 mt(time(nullptr));
const int mod = 1e9 + 7;
const int inf = 1e9;
const ll infl = 1e18;
const int maxn = 1e5 + 1;
struct Node{
int mn;
};
struct Sparsemin{
vector<vector<Node>> t;
int n, logn;
void init(int _n){
n = _n;
logn = 32 - __builtin_clz(n);
t.assign(logn, vector<Node> (n));
}
Node merge(Node a, Node b){
if(a.mn <= b.mn) return a;
return b;
}
void build(vector<int> &l){
init(len(l));
for(int i = 0; i < n; i ++){
t[0][i] = {l[i]};
}
for(int j = 1; j < logn; j ++){
for(int i = 0; i + (1 << j) - 1 < n; i ++){
t[j][i] = merge(t[j - 1][i], t[j - 1][i + (1 << (j - 1))]);
}
}
}
int get(int l, int r){
//return 0;
int k = 31 - __builtin_clz(r - l + 1);
return merge(t[k][l], t[k][l + (1 << k)]).mn;
}
};
struct Sparsemax{
vector<vector<Node>> t;
int n, logn;
void init(int _n){
n = _n;
logn = 32 - __builtin_clz(n);
t.assign(logn, vector<Node> (n + 5));
}
Node merge(Node a, Node b){
if(a.mn >= b.mn) return a;
return b;
}
void build(vector<int> &l){
init(len(l));
for(int i = 0; i < n; i ++){
t[0][i] = {l[i]};
}
for(int j = 1; j < logn; j ++){
for(int i = 0; i + (1 << j) - 1 < n; i ++){
t[j][i] = merge(t[j - 1][i], t[j - 1][i + (1 << (j - 1))]);
}
}
}
int get(int l, int r){
int k = 31 - __builtin_clz(r - l + 1);
//cout << l << ' ' << r << ' ' << k << endl;
//return 0;
return merge(t[k][l], t[k][r - (1 << k) + 1]).mn;
}
};
void solve(){
int n, m, q; cin >> n >> m >> q;
vector<int> sight(m);
vector<vector<int>> g(n);
for(int i = 1; i < n; i ++){
int u, v; cin >> u >> v;
u --; v --;
g[u].push_back(v); g[v].push_back(u);
}
for(int i = 0; i < m; i ++){
cin >> sight[i]; sight[i] --;
}
vector<pair<int,int>> qu(q);
for(int i = 0; i < q; i ++){
cin >> qu[i].ff >> qu[i].ss;
qu[i].ff --; qu[i].ss --;
}
auto brute = [&]{
vector<int> par(n);
auto dfs = [&](auto &dfs, int i, int p = -1)->void{
par[i] = p;
for(auto u : g[i]){
if(u == p) continue;
dfs(dfs, u, i);
}
};
for(auto [l, r] : qu){
dfs(dfs, sight[l]);
int cnt = 1;
vector<int> vis(n);
vis[sight[l]] = 1;
for(int j = l + 1; j <= r; j ++){
int cur = sight[j];
while(vis[cur] == 0){
vis[cur] = 1;
//cout << "cur " << cur + 1 << endl;
cnt ++; cur = par[cur];
}
}
cout << cnt << '\n';
}
};
Sparsemax tmx;
Sparsemin tmn;
tmx.build(sight); tmn.build(sight);
// #ifdef LOCAL
// brute(); cout << "check: " << endl;
// #else
// if(n <= 2000) {brute(); return;}
// #endif
for(auto [l, r] : qu){
cout << tmx.get(l, r) - tmn.get(l, r) + 1 << '\n';
}
}
int main(){
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
auto start = chrono::high_resolution_clock::now();
#ifndef LOCAL
if(fileio.size()){
freopen((fileio + ".in").c_str(), "r", stdin);
freopen((fileio + ".out").c_str(), "w", stdout);
}
#endif
int testcases = 1;
#ifdef Tests
cin >> testcases;
#endif
while(testcases --){
solve();
cout << '\n';
#ifdef LOCAL
cout << "_________________________" << endl;
#endif
}
#ifdef LOCAL
auto duration = chrono::duration_cast<chrono::microseconds>(chrono::high_resolution_clock::now() - start);
cout << "time: " << (double)duration.count()/1000.0 << " milliseconds" << endl;
#endif
return 0;
}
Compilation message
tourism.cpp: In function 'void solve()':
tourism.cpp:131:10: warning: variable 'brute' set but not used [-Wunused-but-set-variable]
131 | auto brute = [&]{
| ^~~~~
tourism.cpp: In function 'int main()':
tourism.cpp:175:10: warning: variable 'start' set but not used [-Wunused-but-set-variable]
175 | auto start = chrono::high_resolution_clock::now();
| ^~~~~
tourism.cpp:178:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
178 | freopen((fileio + ".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
tourism.cpp:179:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
179 | freopen((fileio + ".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |