# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
793769 |
2023-07-26T06:26:58 Z |
vjudge1 |
Tourism (JOI23_tourism) |
C++17 |
|
391 ms |
34380 KB |
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 1e5 + 10;
const int M = (1 << 18);
// segment Sum + update in pos
int segSum[2 * M];
void update(int pos, int val) {
for(segSum[pos += M] += val; pos > 1; pos >>= 1) {
segSum[pos >> 1] = segSum[pos] + segSum[(pos ^ 1)];
}
}
int get(int ql, int qr) {
int res = 0;
for(ql += M, qr += M + 1; ql < qr; ql >>= 1, qr >>= 1) {
if(ql & 1) res += segSum[ql++];
if(qr & 1) res += segSum[--qr];
}
return res;
}
template<typename T>
struct SparseTable {
vector<vector<T>> st;
T (*F) (T, T);
int len;
void init(vector<T> &a, T(*f)(T, T)) {
len = (int)a.size();
int mxpow = 32 - __builtin_clz(len);
F = f;
st.resize(mxpow);
st[0] = a;
for(int k = 1; k < mxpow; k++) {
st[k].resize(len - (1 << k) + 1);
for(int i = 0; i < (int)st[k].size(); i++)
st[k][i] = F(st[k - 1][i], st[k - 1][i + (1 << (k - 1))]);
}
}
T get(int l, int r) {
if(l > r || l < 0 || r >= len) return -1;
int k = 31 - __builtin_clz(r - l + 1);
return F(st[k][l], st[k][r - (1 << k) + 1]);
}
};
int n, m, q;
vector<int> g[N];
vector<pair<int, int>> e;
int sm[N];
int tin[N], tout[N], anc[N][17], depth[N], T;
void calc(int s, int p) {
for(int to : g[s]) {
if(to == p) continue;
calc(to, s);
sm[s] += sm[to];
}
}
void precalc(int s, int p) {
tin[s] = T++;
anc[s][0] = p;
for(int i = 1; i < 17; i++)
anc[s][i] = anc[anc[s][i - 1]][i - 1];
for(int to : g[s]) {
if(to == p) continue;
depth[to] = depth[s] + 1;
precalc(to, s);
}
tout[s] = T++;
}
bool up(int u, int v) {
return tin[u] <= tin[v] && tout[u] >= tout[v];
}
int lca(int u, int v) {
if(up(u, v)) return u;
if(up(v, u)) return v;
for(int i = 16; i >= 0; i--) {
if(!up(anc[u][i], v)) u = anc[u][i];
}
return anc[u][0];
}
struct query {
int l, r, i;
};
template<typename T>
struct LCA {
int len;
vector<vector<T>> st;
void init(vector<T> &a) {
len = (int)a.size();
int mxpw = 32 - __builtin_clz(len);
st.resize(mxpw);
st[0] = a;
for(int k = 1; k < mxpw; k++) {
st[k].resize(len - (1 << k) + 1);
for(int i = 0; i < (int)st[k].size(); i++) {
st[k][i] = lca(st[k - 1][i], st[k - 1][i + (1 << (k - 1))]);
}
}
}
T get(int l, int r) {
int mxpw = 31 - __builtin_clz(r - l + 1);
return lca(st[mxpw][l], st[mxpw][r - (1 << mxpw) + 1]);
}
};
int curAns;
void add(int ver) {
if(get(tin[ver], tout[ver]) == 0) curAns++;
int u = ver;
for(int i = 16; i >= 0; i--) {
if(get(tin[ anc[u][i] ], tout[ anc[u][i] ]) == 0)
u = anc[u][i];
}
curAns += depth[ver] - depth[u];
update(tin[ver], +1);
}
void del(int ver) {
update(tin[ver], -1);
int u = ver;
for(int i = 16; i >= 0; i--) {
if(get(tin[ anc[u][i] ], tout[ anc[u][i] ]) == 0)
u = anc[u][i];
}
curAns -= depth[ver] - depth[u];
if(get(tin[ver], tout[ver]) == 0) curAns--;
}
vector<query> qr;
int ans[N];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
cin >> n >> m >> q;
for(int i = 0; i < n - 1; i++) {
int u, v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
e.push_back({u, v});
}
vector<int> ver(m + 1);
for(int i = 1; i <= m; i++)
cin >> ver[i];
if(n <= 2000 && q <= 2000 && m <= 2000) {
while(q --> 0) {
int l, r;
cin >> l >> r;
for(int i = l; i <= r; i++)
sm[ver[i]]++;
calc(1, 1);
int res = 1;
for(int i = 1; i <= n; i++) {
res += (sm[i] && sm[i] < r - l + 1);
sm[i] = 0;
}
cout << res << '\n';
}
return 0;
}
bool subtask = 1;
for(int i = 0; i < n - 1; i++) {
if(e[i].first != i + 1 || e[i].second != i + 2)
subtask = 0;
}
precalc(1, 1);
if(subtask) {
vector<int> ti(m + 1);
for(int i = 1; i <= m; i++)
ti[i] = depth[ver[i]];
SparseTable<int> mn, mx;
mn.init(ti, [](int a, int b) {return (a < b ? a : b);});
mx.init(ti, [](int a, int b) {return (a > b ? a : b);});
while(q --> 0) {
int l, r;
cin >> l >> r;
cout << mx.get(l, r) - mn.get(l, r) + 1 << '\n';
}
return 0;
}
for(int i = 0; i < q; i++) {
query c;
cin >> c.l >> c.r; c.i = i;
qr.push_back(c);
}
LCA<int> lc;
lc.init(ver);
// sort(qr.begin(), qr.end(), [](query a, query b) {return (a.l < b.l);});
int curL = 1, curR = 0;
for(query c : qr) {
while(curR < c.r) {
add(ver[++curR]);
}
while(curL < c.l) {
del(ver[curL++]);
}
// cout << lc.get(c.l, c.r) << '\n';
ans[c.i] = curAns - depth[lc.get(c.l, c.r)];
}
for(int i = 0; i < q; i++)
cout << ans[i] << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
1 ms |
2644 KB |
Output is correct |
3 |
Correct |
1 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2644 KB |
Output is correct |
5 |
Correct |
2 ms |
2644 KB |
Output is correct |
6 |
Correct |
2 ms |
2644 KB |
Output is correct |
7 |
Correct |
2 ms |
2644 KB |
Output is correct |
8 |
Correct |
2 ms |
2644 KB |
Output is correct |
9 |
Correct |
3 ms |
2644 KB |
Output is correct |
10 |
Correct |
2 ms |
2644 KB |
Output is correct |
11 |
Correct |
2 ms |
2644 KB |
Output is correct |
12 |
Correct |
2 ms |
2644 KB |
Output is correct |
13 |
Correct |
2 ms |
2716 KB |
Output is correct |
14 |
Correct |
2 ms |
2644 KB |
Output is correct |
15 |
Correct |
2 ms |
2720 KB |
Output is correct |
16 |
Correct |
2 ms |
2644 KB |
Output is correct |
17 |
Correct |
2 ms |
2644 KB |
Output is correct |
18 |
Correct |
2 ms |
2644 KB |
Output is correct |
19 |
Correct |
2 ms |
2644 KB |
Output is correct |
20 |
Correct |
2 ms |
2724 KB |
Output is correct |
21 |
Correct |
2 ms |
2644 KB |
Output is correct |
22 |
Correct |
2 ms |
2644 KB |
Output is correct |
23 |
Correct |
2 ms |
2644 KB |
Output is correct |
24 |
Correct |
2 ms |
2644 KB |
Output is correct |
25 |
Correct |
2 ms |
2644 KB |
Output is correct |
26 |
Correct |
2 ms |
2644 KB |
Output is correct |
27 |
Correct |
2 ms |
2644 KB |
Output is correct |
28 |
Correct |
2 ms |
2644 KB |
Output is correct |
29 |
Correct |
2 ms |
2644 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
1 ms |
2644 KB |
Output is correct |
3 |
Correct |
1 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2644 KB |
Output is correct |
5 |
Correct |
2 ms |
2644 KB |
Output is correct |
6 |
Correct |
2 ms |
2644 KB |
Output is correct |
7 |
Correct |
2 ms |
2644 KB |
Output is correct |
8 |
Correct |
2 ms |
2644 KB |
Output is correct |
9 |
Correct |
3 ms |
2644 KB |
Output is correct |
10 |
Correct |
2 ms |
2644 KB |
Output is correct |
11 |
Correct |
2 ms |
2644 KB |
Output is correct |
12 |
Correct |
2 ms |
2644 KB |
Output is correct |
13 |
Correct |
2 ms |
2716 KB |
Output is correct |
14 |
Correct |
2 ms |
2644 KB |
Output is correct |
15 |
Correct |
2 ms |
2720 KB |
Output is correct |
16 |
Correct |
2 ms |
2644 KB |
Output is correct |
17 |
Correct |
2 ms |
2644 KB |
Output is correct |
18 |
Correct |
2 ms |
2644 KB |
Output is correct |
19 |
Correct |
2 ms |
2644 KB |
Output is correct |
20 |
Correct |
2 ms |
2724 KB |
Output is correct |
21 |
Correct |
2 ms |
2644 KB |
Output is correct |
22 |
Correct |
2 ms |
2644 KB |
Output is correct |
23 |
Correct |
2 ms |
2644 KB |
Output is correct |
24 |
Correct |
2 ms |
2644 KB |
Output is correct |
25 |
Correct |
2 ms |
2644 KB |
Output is correct |
26 |
Correct |
2 ms |
2644 KB |
Output is correct |
27 |
Correct |
2 ms |
2644 KB |
Output is correct |
28 |
Correct |
2 ms |
2644 KB |
Output is correct |
29 |
Correct |
2 ms |
2644 KB |
Output is correct |
30 |
Correct |
25 ms |
2780 KB |
Output is correct |
31 |
Correct |
26 ms |
2772 KB |
Output is correct |
32 |
Correct |
41 ms |
2772 KB |
Output is correct |
33 |
Correct |
42 ms |
2800 KB |
Output is correct |
34 |
Correct |
42 ms |
2804 KB |
Output is correct |
35 |
Correct |
33 ms |
2772 KB |
Output is correct |
36 |
Correct |
33 ms |
2800 KB |
Output is correct |
37 |
Correct |
34 ms |
2772 KB |
Output is correct |
38 |
Correct |
43 ms |
2896 KB |
Output is correct |
39 |
Correct |
46 ms |
2904 KB |
Output is correct |
40 |
Correct |
46 ms |
2908 KB |
Output is correct |
41 |
Correct |
55 ms |
2932 KB |
Output is correct |
42 |
Correct |
51 ms |
2916 KB |
Output is correct |
43 |
Correct |
47 ms |
2868 KB |
Output is correct |
44 |
Correct |
46 ms |
2772 KB |
Output is correct |
45 |
Correct |
50 ms |
2828 KB |
Output is correct |
46 |
Correct |
47 ms |
2772 KB |
Output is correct |
47 |
Correct |
47 ms |
2840 KB |
Output is correct |
48 |
Correct |
47 ms |
2808 KB |
Output is correct |
49 |
Correct |
45 ms |
2840 KB |
Output is correct |
50 |
Correct |
30 ms |
2804 KB |
Output is correct |
51 |
Correct |
30 ms |
2812 KB |
Output is correct |
52 |
Correct |
33 ms |
2772 KB |
Output is correct |
53 |
Correct |
30 ms |
2772 KB |
Output is correct |
54 |
Correct |
30 ms |
2772 KB |
Output is correct |
55 |
Correct |
30 ms |
2772 KB |
Output is correct |
56 |
Correct |
5 ms |
2644 KB |
Output is correct |
57 |
Correct |
28 ms |
2796 KB |
Output is correct |
58 |
Correct |
29 ms |
2800 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
1 ms |
2644 KB |
Output is correct |
3 |
Correct |
4 ms |
2716 KB |
Output is correct |
4 |
Correct |
48 ms |
24560 KB |
Output is correct |
5 |
Correct |
46 ms |
24640 KB |
Output is correct |
6 |
Correct |
55 ms |
30016 KB |
Output is correct |
7 |
Correct |
76 ms |
34264 KB |
Output is correct |
8 |
Correct |
68 ms |
34360 KB |
Output is correct |
9 |
Correct |
69 ms |
34328 KB |
Output is correct |
10 |
Correct |
68 ms |
34296 KB |
Output is correct |
11 |
Correct |
70 ms |
34296 KB |
Output is correct |
12 |
Correct |
72 ms |
34336 KB |
Output is correct |
13 |
Correct |
68 ms |
34380 KB |
Output is correct |
14 |
Correct |
65 ms |
34248 KB |
Output is correct |
15 |
Correct |
44 ms |
20836 KB |
Output is correct |
16 |
Correct |
64 ms |
33904 KB |
Output is correct |
17 |
Correct |
35 ms |
15948 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
112 ms |
13476 KB |
Output is correct |
3 |
Correct |
161 ms |
16540 KB |
Output is correct |
4 |
Correct |
137 ms |
15980 KB |
Output is correct |
5 |
Correct |
137 ms |
22612 KB |
Output is correct |
6 |
Correct |
176 ms |
22604 KB |
Output is correct |
7 |
Correct |
189 ms |
22592 KB |
Output is correct |
8 |
Correct |
196 ms |
22648 KB |
Output is correct |
9 |
Correct |
200 ms |
22612 KB |
Output is correct |
10 |
Correct |
219 ms |
22556 KB |
Output is correct |
11 |
Correct |
215 ms |
22676 KB |
Output is correct |
12 |
Correct |
212 ms |
22736 KB |
Output is correct |
13 |
Correct |
264 ms |
22864 KB |
Output is correct |
14 |
Correct |
199 ms |
23172 KB |
Output is correct |
15 |
Correct |
236 ms |
24336 KB |
Output is correct |
16 |
Correct |
206 ms |
22876 KB |
Output is correct |
17 |
Correct |
228 ms |
22860 KB |
Output is correct |
18 |
Correct |
199 ms |
22864 KB |
Output is correct |
19 |
Correct |
200 ms |
22668 KB |
Output is correct |
20 |
Correct |
252 ms |
22652 KB |
Output is correct |
21 |
Correct |
271 ms |
22580 KB |
Output is correct |
22 |
Correct |
316 ms |
22664 KB |
Output is correct |
23 |
Correct |
330 ms |
22636 KB |
Output is correct |
24 |
Correct |
313 ms |
22652 KB |
Output is correct |
25 |
Correct |
317 ms |
22644 KB |
Output is correct |
26 |
Correct |
348 ms |
22692 KB |
Output is correct |
27 |
Correct |
342 ms |
22612 KB |
Output is correct |
28 |
Correct |
328 ms |
22660 KB |
Output is correct |
29 |
Correct |
391 ms |
22708 KB |
Output is correct |
30 |
Correct |
363 ms |
22868 KB |
Output is correct |
31 |
Correct |
328 ms |
22840 KB |
Output is correct |
32 |
Correct |
336 ms |
22980 KB |
Output is correct |
33 |
Correct |
323 ms |
23568 KB |
Output is correct |
34 |
Correct |
368 ms |
24452 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
1 ms |
2644 KB |
Output is correct |
3 |
Correct |
4 ms |
2616 KB |
Output is correct |
4 |
Incorrect |
134 ms |
17860 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
1 ms |
2644 KB |
Output is correct |
3 |
Correct |
1 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2644 KB |
Output is correct |
5 |
Correct |
2 ms |
2644 KB |
Output is correct |
6 |
Correct |
2 ms |
2644 KB |
Output is correct |
7 |
Correct |
2 ms |
2644 KB |
Output is correct |
8 |
Correct |
2 ms |
2644 KB |
Output is correct |
9 |
Correct |
3 ms |
2644 KB |
Output is correct |
10 |
Correct |
2 ms |
2644 KB |
Output is correct |
11 |
Correct |
2 ms |
2644 KB |
Output is correct |
12 |
Correct |
2 ms |
2644 KB |
Output is correct |
13 |
Correct |
2 ms |
2716 KB |
Output is correct |
14 |
Correct |
2 ms |
2644 KB |
Output is correct |
15 |
Correct |
2 ms |
2720 KB |
Output is correct |
16 |
Correct |
2 ms |
2644 KB |
Output is correct |
17 |
Correct |
2 ms |
2644 KB |
Output is correct |
18 |
Correct |
2 ms |
2644 KB |
Output is correct |
19 |
Correct |
2 ms |
2644 KB |
Output is correct |
20 |
Correct |
2 ms |
2724 KB |
Output is correct |
21 |
Correct |
2 ms |
2644 KB |
Output is correct |
22 |
Correct |
2 ms |
2644 KB |
Output is correct |
23 |
Correct |
2 ms |
2644 KB |
Output is correct |
24 |
Correct |
2 ms |
2644 KB |
Output is correct |
25 |
Correct |
2 ms |
2644 KB |
Output is correct |
26 |
Correct |
2 ms |
2644 KB |
Output is correct |
27 |
Correct |
2 ms |
2644 KB |
Output is correct |
28 |
Correct |
2 ms |
2644 KB |
Output is correct |
29 |
Correct |
2 ms |
2644 KB |
Output is correct |
30 |
Correct |
25 ms |
2780 KB |
Output is correct |
31 |
Correct |
26 ms |
2772 KB |
Output is correct |
32 |
Correct |
41 ms |
2772 KB |
Output is correct |
33 |
Correct |
42 ms |
2800 KB |
Output is correct |
34 |
Correct |
42 ms |
2804 KB |
Output is correct |
35 |
Correct |
33 ms |
2772 KB |
Output is correct |
36 |
Correct |
33 ms |
2800 KB |
Output is correct |
37 |
Correct |
34 ms |
2772 KB |
Output is correct |
38 |
Correct |
43 ms |
2896 KB |
Output is correct |
39 |
Correct |
46 ms |
2904 KB |
Output is correct |
40 |
Correct |
46 ms |
2908 KB |
Output is correct |
41 |
Correct |
55 ms |
2932 KB |
Output is correct |
42 |
Correct |
51 ms |
2916 KB |
Output is correct |
43 |
Correct |
47 ms |
2868 KB |
Output is correct |
44 |
Correct |
46 ms |
2772 KB |
Output is correct |
45 |
Correct |
50 ms |
2828 KB |
Output is correct |
46 |
Correct |
47 ms |
2772 KB |
Output is correct |
47 |
Correct |
47 ms |
2840 KB |
Output is correct |
48 |
Correct |
47 ms |
2808 KB |
Output is correct |
49 |
Correct |
45 ms |
2840 KB |
Output is correct |
50 |
Correct |
30 ms |
2804 KB |
Output is correct |
51 |
Correct |
30 ms |
2812 KB |
Output is correct |
52 |
Correct |
33 ms |
2772 KB |
Output is correct |
53 |
Correct |
30 ms |
2772 KB |
Output is correct |
54 |
Correct |
30 ms |
2772 KB |
Output is correct |
55 |
Correct |
30 ms |
2772 KB |
Output is correct |
56 |
Correct |
5 ms |
2644 KB |
Output is correct |
57 |
Correct |
28 ms |
2796 KB |
Output is correct |
58 |
Correct |
29 ms |
2800 KB |
Output is correct |
59 |
Correct |
2 ms |
2644 KB |
Output is correct |
60 |
Correct |
1 ms |
2644 KB |
Output is correct |
61 |
Correct |
4 ms |
2716 KB |
Output is correct |
62 |
Correct |
48 ms |
24560 KB |
Output is correct |
63 |
Correct |
46 ms |
24640 KB |
Output is correct |
64 |
Correct |
55 ms |
30016 KB |
Output is correct |
65 |
Correct |
76 ms |
34264 KB |
Output is correct |
66 |
Correct |
68 ms |
34360 KB |
Output is correct |
67 |
Correct |
69 ms |
34328 KB |
Output is correct |
68 |
Correct |
68 ms |
34296 KB |
Output is correct |
69 |
Correct |
70 ms |
34296 KB |
Output is correct |
70 |
Correct |
72 ms |
34336 KB |
Output is correct |
71 |
Correct |
68 ms |
34380 KB |
Output is correct |
72 |
Correct |
65 ms |
34248 KB |
Output is correct |
73 |
Correct |
44 ms |
20836 KB |
Output is correct |
74 |
Correct |
64 ms |
33904 KB |
Output is correct |
75 |
Correct |
35 ms |
15948 KB |
Output is correct |
76 |
Correct |
2 ms |
2644 KB |
Output is correct |
77 |
Correct |
112 ms |
13476 KB |
Output is correct |
78 |
Correct |
161 ms |
16540 KB |
Output is correct |
79 |
Correct |
137 ms |
15980 KB |
Output is correct |
80 |
Correct |
137 ms |
22612 KB |
Output is correct |
81 |
Correct |
176 ms |
22604 KB |
Output is correct |
82 |
Correct |
189 ms |
22592 KB |
Output is correct |
83 |
Correct |
196 ms |
22648 KB |
Output is correct |
84 |
Correct |
200 ms |
22612 KB |
Output is correct |
85 |
Correct |
219 ms |
22556 KB |
Output is correct |
86 |
Correct |
215 ms |
22676 KB |
Output is correct |
87 |
Correct |
212 ms |
22736 KB |
Output is correct |
88 |
Correct |
264 ms |
22864 KB |
Output is correct |
89 |
Correct |
199 ms |
23172 KB |
Output is correct |
90 |
Correct |
236 ms |
24336 KB |
Output is correct |
91 |
Correct |
206 ms |
22876 KB |
Output is correct |
92 |
Correct |
228 ms |
22860 KB |
Output is correct |
93 |
Correct |
199 ms |
22864 KB |
Output is correct |
94 |
Correct |
200 ms |
22668 KB |
Output is correct |
95 |
Correct |
252 ms |
22652 KB |
Output is correct |
96 |
Correct |
271 ms |
22580 KB |
Output is correct |
97 |
Correct |
316 ms |
22664 KB |
Output is correct |
98 |
Correct |
330 ms |
22636 KB |
Output is correct |
99 |
Correct |
313 ms |
22652 KB |
Output is correct |
100 |
Correct |
317 ms |
22644 KB |
Output is correct |
101 |
Correct |
348 ms |
22692 KB |
Output is correct |
102 |
Correct |
342 ms |
22612 KB |
Output is correct |
103 |
Correct |
328 ms |
22660 KB |
Output is correct |
104 |
Correct |
391 ms |
22708 KB |
Output is correct |
105 |
Correct |
363 ms |
22868 KB |
Output is correct |
106 |
Correct |
328 ms |
22840 KB |
Output is correct |
107 |
Correct |
336 ms |
22980 KB |
Output is correct |
108 |
Correct |
323 ms |
23568 KB |
Output is correct |
109 |
Correct |
368 ms |
24452 KB |
Output is correct |
110 |
Correct |
2 ms |
2644 KB |
Output is correct |
111 |
Correct |
1 ms |
2644 KB |
Output is correct |
112 |
Correct |
4 ms |
2616 KB |
Output is correct |
113 |
Incorrect |
134 ms |
17860 KB |
Output isn't correct |
114 |
Halted |
0 ms |
0 KB |
- |