# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
898424 |
2024-01-04T16:10:51 Z |
vjudge1 |
Tourism (JOI23_tourism) |
C++17 |
|
61 ms |
17396 KB |
#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){
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));
}
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].mn = 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 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 'int main()':
tourism.cpp:172:10: warning: variable 'start' set but not used [-Wunused-but-set-variable]
172 | auto start = chrono::high_resolution_clock::now();
| ^~~~~
tourism.cpp:175:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
175 | freopen((fileio + ".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
tourism.cpp:176:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
176 | freopen((fileio + ".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
404 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
488 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
508 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
404 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
488 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
508 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
26 ms |
604 KB |
Output is correct |
31 |
Correct |
33 ms |
560 KB |
Output is correct |
32 |
Correct |
61 ms |
604 KB |
Output is correct |
33 |
Correct |
56 ms |
604 KB |
Output is correct |
34 |
Correct |
55 ms |
600 KB |
Output is correct |
35 |
Correct |
53 ms |
600 KB |
Output is correct |
36 |
Correct |
53 ms |
604 KB |
Output is correct |
37 |
Correct |
52 ms |
604 KB |
Output is correct |
38 |
Correct |
41 ms |
600 KB |
Output is correct |
39 |
Correct |
41 ms |
820 KB |
Output is correct |
40 |
Correct |
42 ms |
604 KB |
Output is correct |
41 |
Correct |
44 ms |
604 KB |
Output is correct |
42 |
Correct |
44 ms |
600 KB |
Output is correct |
43 |
Correct |
43 ms |
600 KB |
Output is correct |
44 |
Correct |
41 ms |
600 KB |
Output is correct |
45 |
Correct |
42 ms |
604 KB |
Output is correct |
46 |
Correct |
41 ms |
604 KB |
Output is correct |
47 |
Correct |
43 ms |
600 KB |
Output is correct |
48 |
Correct |
43 ms |
600 KB |
Output is correct |
49 |
Correct |
43 ms |
604 KB |
Output is correct |
50 |
Correct |
24 ms |
604 KB |
Output is correct |
51 |
Correct |
27 ms |
860 KB |
Output is correct |
52 |
Correct |
20 ms |
604 KB |
Output is correct |
53 |
Correct |
20 ms |
800 KB |
Output is correct |
54 |
Correct |
20 ms |
604 KB |
Output is correct |
55 |
Correct |
20 ms |
796 KB |
Output is correct |
56 |
Correct |
2 ms |
600 KB |
Output is correct |
57 |
Correct |
25 ms |
604 KB |
Output is correct |
58 |
Correct |
41 ms |
600 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
3 ms |
604 KB |
Output is correct |
4 |
Incorrect |
48 ms |
16892 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Incorrect |
19 ms |
10816 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
2 ms |
600 KB |
Output is correct |
4 |
Incorrect |
41 ms |
17396 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
404 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
488 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
508 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
26 ms |
604 KB |
Output is correct |
31 |
Correct |
33 ms |
560 KB |
Output is correct |
32 |
Correct |
61 ms |
604 KB |
Output is correct |
33 |
Correct |
56 ms |
604 KB |
Output is correct |
34 |
Correct |
55 ms |
600 KB |
Output is correct |
35 |
Correct |
53 ms |
600 KB |
Output is correct |
36 |
Correct |
53 ms |
604 KB |
Output is correct |
37 |
Correct |
52 ms |
604 KB |
Output is correct |
38 |
Correct |
41 ms |
600 KB |
Output is correct |
39 |
Correct |
41 ms |
820 KB |
Output is correct |
40 |
Correct |
42 ms |
604 KB |
Output is correct |
41 |
Correct |
44 ms |
604 KB |
Output is correct |
42 |
Correct |
44 ms |
600 KB |
Output is correct |
43 |
Correct |
43 ms |
600 KB |
Output is correct |
44 |
Correct |
41 ms |
600 KB |
Output is correct |
45 |
Correct |
42 ms |
604 KB |
Output is correct |
46 |
Correct |
41 ms |
604 KB |
Output is correct |
47 |
Correct |
43 ms |
600 KB |
Output is correct |
48 |
Correct |
43 ms |
600 KB |
Output is correct |
49 |
Correct |
43 ms |
604 KB |
Output is correct |
50 |
Correct |
24 ms |
604 KB |
Output is correct |
51 |
Correct |
27 ms |
860 KB |
Output is correct |
52 |
Correct |
20 ms |
604 KB |
Output is correct |
53 |
Correct |
20 ms |
800 KB |
Output is correct |
54 |
Correct |
20 ms |
604 KB |
Output is correct |
55 |
Correct |
20 ms |
796 KB |
Output is correct |
56 |
Correct |
2 ms |
600 KB |
Output is correct |
57 |
Correct |
25 ms |
604 KB |
Output is correct |
58 |
Correct |
41 ms |
600 KB |
Output is correct |
59 |
Correct |
0 ms |
344 KB |
Output is correct |
60 |
Correct |
0 ms |
348 KB |
Output is correct |
61 |
Correct |
3 ms |
604 KB |
Output is correct |
62 |
Incorrect |
48 ms |
16892 KB |
Output isn't correct |
63 |
Halted |
0 ms |
0 KB |
- |