# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
1013721 |
2024-07-04T03:33:59 Z |
12345678 |
Tourism (JOI23_tourism) |
C++17 |
|
652 ms |
19540 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, 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(int u, int cur)
{
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)
{
if (l==r) return ans=1, s.update(1, n, 1, in[u], 1), void();
int v=u, lca=getlca(c[l], r-l);
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(c[l], r-l+1);
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];
for (int i=1; i<=q; i++)
{
cin>>a[i]>>b[i];
while (r<b[i]) add(c[++r]); //cout<<l<<' '<<r<<' '<<ans<<'\n';
while (l<a[i]) rem(c[l++]); //cout<<l<<' '<<r<<' '<<ans<<'\n';
cout<<ans<<'\n';
}
}
/*
7 6 6
1 2
1 3
2 4
2 5
3 6
3 7
2 3 6 4 5 7
1 1
2 2
3 3
4 4
5 5
6 6
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
8536 KB |
Output is correct |
2 |
Incorrect |
1 ms |
8540 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
8536 KB |
Output is correct |
2 |
Incorrect |
1 ms |
8540 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
8536 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
8540 KB |
Output is correct |
2 |
Correct |
179 ms |
14688 KB |
Output is correct |
3 |
Correct |
272 ms |
16268 KB |
Output is correct |
4 |
Correct |
215 ms |
16464 KB |
Output is correct |
5 |
Correct |
115 ms |
18272 KB |
Output is correct |
6 |
Correct |
212 ms |
18260 KB |
Output is correct |
7 |
Correct |
251 ms |
18256 KB |
Output is correct |
8 |
Correct |
303 ms |
18324 KB |
Output is correct |
9 |
Correct |
320 ms |
18324 KB |
Output is correct |
10 |
Correct |
339 ms |
18088 KB |
Output is correct |
11 |
Correct |
367 ms |
18260 KB |
Output is correct |
12 |
Correct |
320 ms |
18360 KB |
Output is correct |
13 |
Correct |
316 ms |
18612 KB |
Output is correct |
14 |
Correct |
326 ms |
18524 KB |
Output is correct |
15 |
Correct |
228 ms |
19540 KB |
Output is correct |
16 |
Correct |
315 ms |
18268 KB |
Output is correct |
17 |
Correct |
288 ms |
18256 KB |
Output is correct |
18 |
Correct |
309 ms |
18260 KB |
Output is correct |
19 |
Correct |
142 ms |
18260 KB |
Output is correct |
20 |
Correct |
185 ms |
18264 KB |
Output is correct |
21 |
Correct |
245 ms |
18256 KB |
Output is correct |
22 |
Correct |
252 ms |
18260 KB |
Output is correct |
23 |
Correct |
265 ms |
18300 KB |
Output is correct |
24 |
Correct |
292 ms |
18256 KB |
Output is correct |
25 |
Correct |
321 ms |
18516 KB |
Output is correct |
26 |
Correct |
470 ms |
18260 KB |
Output is correct |
27 |
Correct |
467 ms |
18148 KB |
Output is correct |
28 |
Correct |
500 ms |
18256 KB |
Output is correct |
29 |
Correct |
576 ms |
18332 KB |
Output is correct |
30 |
Correct |
652 ms |
18632 KB |
Output is correct |
31 |
Correct |
523 ms |
18288 KB |
Output is correct |
32 |
Correct |
537 ms |
18516 KB |
Output is correct |
33 |
Correct |
530 ms |
18812 KB |
Output is correct |
34 |
Correct |
379 ms |
19540 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
8536 KB |
Output is correct |
2 |
Correct |
2 ms |
8540 KB |
Output is correct |
3 |
Correct |
1 ms |
8540 KB |
Output is correct |
4 |
Incorrect |
160 ms |
15156 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
8536 KB |
Output is correct |
2 |
Incorrect |
1 ms |
8540 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |