# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
1013725 |
2024-07-04T03:50:45 Z |
12345678 |
Tourism (JOI23_tourism) |
C++17 |
|
5000 ms |
19244 KB |
#include <bits/stdc++.h>
using namespace std;
const int nx=1e5+5, kx=17;
int n, m, q, u, v, in[nx], out[nx], pa[nx][kx], t, c[nx], l=1, r=1, a[nx], b[nx], ans, lvl[nx];
vector<int> d[nx];
void dfs(int u, int p)
{
pa[u][0]=p;
lvl[u]=lvl[p]+1;
for (int i=1; i<kx; i++) pa[u][i]=pa[pa[u][i-1]][i-1];
in[u]=++t;
for (auto v:d[u]) if (v!=p) dfs(v, u);
out[u]=t;
}
struct segtree
{
int d[4*nx];
void update(int l, int r, int i, int idx, int vl)
{
if (idx<l||r<idx) return;
if (l==r) return d[i]+=vl, void();
int md=(l+r)/2;
update(l, md, 2*i, idx, vl);
update(md+1, r, 2*i+1, idx, vl);
d[i]=d[2*i]+d[2*i+1];
}
int query(int l, int r, int i, int ql, int qr)
{
if (qr<l||r<ql) return 0;
if (ql<=l&&r<=qr) return d[i];
int md=(l+r)/2;
return query(l, md, 2*i, ql, qr)+query(md+1, r, 2*i+1, ql, qr);
}
} s;
int getlca()
{
if (s.query(1, n, 1, in[c[l]], in[c[l]])==1) u=c[l];
else u=c[l+1];
int cur=s.query(1, n, 1, in[1], out[1]);
if (s.query(1, n, 1, in[u], out[u])==cur) return u;
for (int i=kx-1; i>=0; i--) if (s.query(1, n, 1, in[pa[u][i]], out[pa[u][i]])!=cur) u=pa[u][i];
return pa[u][0];
}
void add(int u)
{
int v=u, lca=getlca();
if (s.query(1, n, 1, in[u], out[u])>0)
{
if (lvl[lca]<=lvl[u]) return s.update(1, n, 1, in[u], 1), void();
ans+=lvl[lca]-lvl[u];
s.update(1, n, 1, in[u], 1);
return;
}
for (int i=kx-1; i>=0; i--) if (s.query(1, n, 1, in[pa[v][i]], out[pa[v][i]])==0) v=pa[v][i];
v=pa[v][0];
if (lvl[lca]<=lvl[v]) ans+=lvl[u]-lvl[v], s.update(1, n, 1, in[u], 1);
else ans+=lvl[u]-lvl[v]+lvl[pa[lca][0]]-lvl[v]+1, s.update(1, n, 1, in[u], 1);
}
void rem(int u)
{
s.update(1, n, 1, in[u], -1);
int v=u, lca=getlca();
if (s.query(1, n, 1, in[u], out[u])>0)
{
if (lvl[lca]<=lvl[u]) return;
ans-=lvl[lca]-lvl[u];
return;
}
for (int i=kx-1; i>=0; i--) if (s.query(1, n, 1, in[pa[v][i]], out[pa[v][i]])==0) v=pa[v][i];
v=pa[v][0];
if (lvl[lca]<=lvl[v]) ans-=lvl[u]-lvl[v];
else ans-=lvl[u]-lvl[v]+lvl[pa[lca][0]]-lvl[v]+1;
}
int main()
{
cin.tie(NULL)->sync_with_stdio(false);
cin>>n>>m>>q;
for (int i=1; i<n; i++) cin>>u>>v, d[u].push_back(v), d[v].push_back(u);
dfs(1, 1);
for (int i=1; i<=m; i++) cin>>c[i];
ans=1;
s.update(1, n, 1, in[c[1]], 1);
for (int i=1; i<=q; i++)
{
cin>>a[i]>>b[i];
while (l>a[i]) add(c[--l]);
while (r<b[i]) add(c[++r]);
while (l<a[i]) rem(c[l++]);
while (r>b[i]) rem(c[r--]);
cout<<ans<<'\n';
}
}
/*
8 8 2
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 6 4 3 5 2 4 7
2 8
1 2
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2652 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
13 ms |
2880 KB |
Output is correct |
5 |
Correct |
9 ms |
2652 KB |
Output is correct |
6 |
Correct |
11 ms |
2652 KB |
Output is correct |
7 |
Correct |
16 ms |
2652 KB |
Output is correct |
8 |
Correct |
9 ms |
2652 KB |
Output is correct |
9 |
Correct |
22 ms |
2652 KB |
Output is correct |
10 |
Correct |
29 ms |
2648 KB |
Output is correct |
11 |
Correct |
22 ms |
2652 KB |
Output is correct |
12 |
Correct |
2 ms |
2652 KB |
Output is correct |
13 |
Correct |
2 ms |
2652 KB |
Output is correct |
14 |
Correct |
3 ms |
2652 KB |
Output is correct |
15 |
Correct |
17 ms |
2904 KB |
Output is correct |
16 |
Correct |
13 ms |
2904 KB |
Output is correct |
17 |
Correct |
21 ms |
2652 KB |
Output is correct |
18 |
Correct |
20 ms |
2908 KB |
Output is correct |
19 |
Correct |
22 ms |
2652 KB |
Output is correct |
20 |
Correct |
17 ms |
2904 KB |
Output is correct |
21 |
Correct |
16 ms |
2652 KB |
Output is correct |
22 |
Correct |
16 ms |
2904 KB |
Output is correct |
23 |
Correct |
15 ms |
2900 KB |
Output is correct |
24 |
Correct |
16 ms |
2904 KB |
Output is correct |
25 |
Correct |
14 ms |
2908 KB |
Output is correct |
26 |
Correct |
14 ms |
2652 KB |
Output is correct |
27 |
Correct |
1 ms |
2652 KB |
Output is correct |
28 |
Correct |
2 ms |
2652 KB |
Output is correct |
29 |
Correct |
31 ms |
2848 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2652 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
13 ms |
2880 KB |
Output is correct |
5 |
Correct |
9 ms |
2652 KB |
Output is correct |
6 |
Correct |
11 ms |
2652 KB |
Output is correct |
7 |
Correct |
16 ms |
2652 KB |
Output is correct |
8 |
Correct |
9 ms |
2652 KB |
Output is correct |
9 |
Correct |
22 ms |
2652 KB |
Output is correct |
10 |
Correct |
29 ms |
2648 KB |
Output is correct |
11 |
Correct |
22 ms |
2652 KB |
Output is correct |
12 |
Correct |
2 ms |
2652 KB |
Output is correct |
13 |
Correct |
2 ms |
2652 KB |
Output is correct |
14 |
Correct |
3 ms |
2652 KB |
Output is correct |
15 |
Correct |
17 ms |
2904 KB |
Output is correct |
16 |
Correct |
13 ms |
2904 KB |
Output is correct |
17 |
Correct |
21 ms |
2652 KB |
Output is correct |
18 |
Correct |
20 ms |
2908 KB |
Output is correct |
19 |
Correct |
22 ms |
2652 KB |
Output is correct |
20 |
Correct |
17 ms |
2904 KB |
Output is correct |
21 |
Correct |
16 ms |
2652 KB |
Output is correct |
22 |
Correct |
16 ms |
2904 KB |
Output is correct |
23 |
Correct |
15 ms |
2900 KB |
Output is correct |
24 |
Correct |
16 ms |
2904 KB |
Output is correct |
25 |
Correct |
14 ms |
2908 KB |
Output is correct |
26 |
Correct |
14 ms |
2652 KB |
Output is correct |
27 |
Correct |
1 ms |
2652 KB |
Output is correct |
28 |
Correct |
2 ms |
2652 KB |
Output is correct |
29 |
Correct |
31 ms |
2848 KB |
Output is correct |
30 |
Correct |
571 ms |
3160 KB |
Output is correct |
31 |
Correct |
750 ms |
2904 KB |
Output is correct |
32 |
Correct |
1172 ms |
3128 KB |
Output is correct |
33 |
Correct |
1332 ms |
3128 KB |
Output is correct |
34 |
Correct |
1074 ms |
3152 KB |
Output is correct |
35 |
Correct |
30 ms |
2908 KB |
Output is correct |
36 |
Correct |
30 ms |
3160 KB |
Output is correct |
37 |
Correct |
28 ms |
2908 KB |
Output is correct |
38 |
Correct |
1159 ms |
3196 KB |
Output is correct |
39 |
Correct |
1002 ms |
3164 KB |
Output is correct |
40 |
Correct |
1278 ms |
3460 KB |
Output is correct |
41 |
Correct |
38 ms |
3164 KB |
Output is correct |
42 |
Correct |
45 ms |
3208 KB |
Output is correct |
43 |
Correct |
26 ms |
3160 KB |
Output is correct |
44 |
Correct |
1300 ms |
3416 KB |
Output is correct |
45 |
Correct |
1089 ms |
3128 KB |
Output is correct |
46 |
Correct |
1329 ms |
3164 KB |
Output is correct |
47 |
Correct |
33 ms |
3164 KB |
Output is correct |
48 |
Correct |
32 ms |
3160 KB |
Output is correct |
49 |
Correct |
29 ms |
3164 KB |
Output is correct |
50 |
Correct |
796 ms |
3132 KB |
Output is correct |
51 |
Correct |
804 ms |
3120 KB |
Output is correct |
52 |
Correct |
742 ms |
3124 KB |
Output is correct |
53 |
Correct |
783 ms |
3376 KB |
Output is correct |
54 |
Correct |
796 ms |
3124 KB |
Output is correct |
55 |
Correct |
784 ms |
3168 KB |
Output is correct |
56 |
Correct |
28 ms |
2652 KB |
Output is correct |
57 |
Correct |
2 ms |
2908 KB |
Output is correct |
58 |
Correct |
1624 ms |
3112 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2648 KB |
Output is correct |
2 |
Correct |
3 ms |
2808 KB |
Output is correct |
3 |
Correct |
26 ms |
2884 KB |
Output is correct |
4 |
Execution timed out |
5073 ms |
13404 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2904 KB |
Output is correct |
2 |
Correct |
167 ms |
10588 KB |
Output is correct |
3 |
Correct |
248 ms |
11352 KB |
Output is correct |
4 |
Correct |
244 ms |
12624 KB |
Output is correct |
5 |
Correct |
120 ms |
16800 KB |
Output is correct |
6 |
Correct |
234 ms |
16976 KB |
Output is correct |
7 |
Correct |
238 ms |
16816 KB |
Output is correct |
8 |
Correct |
288 ms |
16976 KB |
Output is correct |
9 |
Correct |
305 ms |
17012 KB |
Output is correct |
10 |
Correct |
338 ms |
16976 KB |
Output is correct |
11 |
Correct |
369 ms |
17020 KB |
Output is correct |
12 |
Correct |
328 ms |
16980 KB |
Output is correct |
13 |
Correct |
305 ms |
17236 KB |
Output is correct |
14 |
Correct |
305 ms |
17628 KB |
Output is correct |
15 |
Correct |
233 ms |
19244 KB |
Output is correct |
16 |
Correct |
290 ms |
17232 KB |
Output is correct |
17 |
Correct |
298 ms |
17148 KB |
Output is correct |
18 |
Correct |
356 ms |
17232 KB |
Output is correct |
19 |
Correct |
193 ms |
17232 KB |
Output is correct |
20 |
Correct |
246 ms |
16988 KB |
Output is correct |
21 |
Correct |
218 ms |
16724 KB |
Output is correct |
22 |
Correct |
238 ms |
16980 KB |
Output is correct |
23 |
Correct |
288 ms |
16988 KB |
Output is correct |
24 |
Correct |
328 ms |
16948 KB |
Output is correct |
25 |
Correct |
342 ms |
16972 KB |
Output is correct |
26 |
Correct |
447 ms |
16988 KB |
Output is correct |
27 |
Correct |
498 ms |
17004 KB |
Output is correct |
28 |
Correct |
560 ms |
16984 KB |
Output is correct |
29 |
Correct |
613 ms |
16976 KB |
Output is correct |
30 |
Correct |
602 ms |
17064 KB |
Output is correct |
31 |
Correct |
599 ms |
17108 KB |
Output is correct |
32 |
Correct |
591 ms |
17232 KB |
Output is correct |
33 |
Correct |
519 ms |
17940 KB |
Output is correct |
34 |
Correct |
417 ms |
18936 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2652 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
25 ms |
2652 KB |
Output is correct |
4 |
Execution timed out |
5094 ms |
11100 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2652 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
13 ms |
2880 KB |
Output is correct |
5 |
Correct |
9 ms |
2652 KB |
Output is correct |
6 |
Correct |
11 ms |
2652 KB |
Output is correct |
7 |
Correct |
16 ms |
2652 KB |
Output is correct |
8 |
Correct |
9 ms |
2652 KB |
Output is correct |
9 |
Correct |
22 ms |
2652 KB |
Output is correct |
10 |
Correct |
29 ms |
2648 KB |
Output is correct |
11 |
Correct |
22 ms |
2652 KB |
Output is correct |
12 |
Correct |
2 ms |
2652 KB |
Output is correct |
13 |
Correct |
2 ms |
2652 KB |
Output is correct |
14 |
Correct |
3 ms |
2652 KB |
Output is correct |
15 |
Correct |
17 ms |
2904 KB |
Output is correct |
16 |
Correct |
13 ms |
2904 KB |
Output is correct |
17 |
Correct |
21 ms |
2652 KB |
Output is correct |
18 |
Correct |
20 ms |
2908 KB |
Output is correct |
19 |
Correct |
22 ms |
2652 KB |
Output is correct |
20 |
Correct |
17 ms |
2904 KB |
Output is correct |
21 |
Correct |
16 ms |
2652 KB |
Output is correct |
22 |
Correct |
16 ms |
2904 KB |
Output is correct |
23 |
Correct |
15 ms |
2900 KB |
Output is correct |
24 |
Correct |
16 ms |
2904 KB |
Output is correct |
25 |
Correct |
14 ms |
2908 KB |
Output is correct |
26 |
Correct |
14 ms |
2652 KB |
Output is correct |
27 |
Correct |
1 ms |
2652 KB |
Output is correct |
28 |
Correct |
2 ms |
2652 KB |
Output is correct |
29 |
Correct |
31 ms |
2848 KB |
Output is correct |
30 |
Correct |
571 ms |
3160 KB |
Output is correct |
31 |
Correct |
750 ms |
2904 KB |
Output is correct |
32 |
Correct |
1172 ms |
3128 KB |
Output is correct |
33 |
Correct |
1332 ms |
3128 KB |
Output is correct |
34 |
Correct |
1074 ms |
3152 KB |
Output is correct |
35 |
Correct |
30 ms |
2908 KB |
Output is correct |
36 |
Correct |
30 ms |
3160 KB |
Output is correct |
37 |
Correct |
28 ms |
2908 KB |
Output is correct |
38 |
Correct |
1159 ms |
3196 KB |
Output is correct |
39 |
Correct |
1002 ms |
3164 KB |
Output is correct |
40 |
Correct |
1278 ms |
3460 KB |
Output is correct |
41 |
Correct |
38 ms |
3164 KB |
Output is correct |
42 |
Correct |
45 ms |
3208 KB |
Output is correct |
43 |
Correct |
26 ms |
3160 KB |
Output is correct |
44 |
Correct |
1300 ms |
3416 KB |
Output is correct |
45 |
Correct |
1089 ms |
3128 KB |
Output is correct |
46 |
Correct |
1329 ms |
3164 KB |
Output is correct |
47 |
Correct |
33 ms |
3164 KB |
Output is correct |
48 |
Correct |
32 ms |
3160 KB |
Output is correct |
49 |
Correct |
29 ms |
3164 KB |
Output is correct |
50 |
Correct |
796 ms |
3132 KB |
Output is correct |
51 |
Correct |
804 ms |
3120 KB |
Output is correct |
52 |
Correct |
742 ms |
3124 KB |
Output is correct |
53 |
Correct |
783 ms |
3376 KB |
Output is correct |
54 |
Correct |
796 ms |
3124 KB |
Output is correct |
55 |
Correct |
784 ms |
3168 KB |
Output is correct |
56 |
Correct |
28 ms |
2652 KB |
Output is correct |
57 |
Correct |
2 ms |
2908 KB |
Output is correct |
58 |
Correct |
1624 ms |
3112 KB |
Output is correct |
59 |
Correct |
1 ms |
2648 KB |
Output is correct |
60 |
Correct |
3 ms |
2808 KB |
Output is correct |
61 |
Correct |
26 ms |
2884 KB |
Output is correct |
62 |
Execution timed out |
5073 ms |
13404 KB |
Time limit exceeded |
63 |
Halted |
0 ms |
0 KB |
- |