# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
789176 |
2023-07-21T07:02:48 Z |
반딧불(#10041) |
Tourism (JOI23_tourism) |
C++17 |
|
158 ms |
54320 KB |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int B = 300;
int n, k, q;
vector<int> link[100002];
int inCnt, in[100002], out[100002], idx[100002];
int eventCnt, eventIn[100002], eventOut[100002], event[200002];
int par[100002], LCADB[100002][20], depth[100002];
int arr[100002];
int spsR[200002][18];
void dfs(int x, int p=-1){
in[x] = ++inCnt;
idx[in[x]] = x;
eventIn[x] = eventOut[x] = ++eventCnt;
event[eventIn[x]] = depth[x];
for(auto y: link[x]){
if(y==p) continue;
par[y] = x, depth[y] = depth[x] + 1;
dfs(y, x);
eventOut[x] = ++eventCnt;
event[eventOut[x]] = depth[x];
}
out[x] = inCnt;
}
int p[200002];
inline int getDist(int x, int y){
if(x==y) return 0;
if(in[x]>in[y]) swap(x,y);
int td=depth[x]+depth[y];
x=eventOut[x], y=eventIn[y];
int d=p[y-x+1];
return td - 2 * min(spsR[x][d], spsR[y-(1<<d)+1][d]);
}
struct Query{
int l, r, idx;
Query(){}
Query(int l, int r, int idx): l(l), r(r), idx(idx){}
bool operator<(const Query &nxt)const{
if(l/B != nxt.l/B) return l/B < nxt.l/B;
return r < nxt.r;
}
} query[100002];
struct segTree{
int tree[1<<18];
inline void add(int i, int l, int r, int x, int v){
tree[i] += v;
if(l==r) return;
int m = (l+r)>>1;
if(x<=m) add(i*2, l, m, x, v);
else add(i*2+1, m+1, r, x, v);
}
inline int nxt(int i, int l, int r, int x){
if(r<x||!tree[i]) return r+1;
if(l==r) return l;
int m = (l+r)>>1;
int v = nxt(i*2,l,m,x);
return v==m+1 ? nxt(i*2+1, m+1, r, x) : v;
}
inline int prv(int i, int l, int r, int x){
if(x<l||!tree[i]) return l-1;
if(l==r) return l;
int m = (l+r)>>1;
int v = prv(i*2+1, m+1, r, x);
return v==m ? prv(i*2, l, m, x) : v;
}
} tree;
int ans[100002];
int distSum, cnt;
inline void add(int x){
if(cnt==0) cnt++, tree.add(1, 1, n, arr[x], 1);
else if(cnt==1){
cnt++;
int tmp = arr[x]==1 ? 0 : tree.prv(1, 1, n, arr[x]);
if(tmp==0) tmp = tree.nxt(1, 1, n, arr[x]);
distSum += getDist(idx[arr[x]], idx[tmp]) * 2;
tree.add(1, 1, n, arr[x], 1);
}
else{
cnt++;
int prv = arr[x]==1 ? 0 : tree.prv(1, 1, n, arr[x]);
if(prv==0) prv = tree.prv(1, 1, n, n);
int nxt = arr[x]==n ? n+1 : tree.nxt(1, 1, n, arr[x]);
if(nxt==n+1) nxt = tree.nxt(1, 1, n, 1);
distSum += getDist(idx[arr[x]], idx[prv]) + getDist(idx[arr[x]], idx[nxt]) - getDist(idx[prv], idx[nxt]);
tree.add(1, 1, n, arr[x], 1);
}
}
inline void del(int x){
if(cnt<=1) cnt--, tree.add(1, 1, n, arr[x], -1), distSum = 0;
else{
cnt--;
tree.add(1, 1, n, arr[x], -1);
int prv = arr[x]==1 ? 0 : tree.prv(1, 1, n, arr[x]);
if(prv==0) prv = tree.prv(1, 1, n, n);
int nxt = arr[x]==n ? n+1 : tree.nxt(1, 1, n, arr[x]);
if(nxt==n+1) nxt = tree.nxt(1, 1, n, 1);
distSum -= getDist(idx[arr[x]], idx[prv]) + getDist(idx[arr[x]], idx[nxt]) - getDist(idx[prv], idx[nxt]);
}
}
int main(){
scanf("%d %d %d", &n, &k, &q);
for(int i=1; i<n; i++){
int x, y;
scanf("%d %d", &x, &y);
link[x].push_back(y);
link[y].push_back(x);
}
dfs(1);
for(int i=1; i<=n+n; i++) spsR[i][0] = event[i];
for(int d=1; d<18; d++) for(int i=1; i<=n+n; i++)
spsR[i][d] = min(spsR[i][d-1], (i+(1<<(d-1))) > n+n ? INT_MAX : spsR[i+(1<<(d-1))][d-1]);
for(int i=1; i<=n+n+1; i++){
int j=0;
while((1<<(j+1))<=i) j++;
p[i]=j;
}
for(int i=1; i<=k; i++){
scanf("%d", &arr[i]);
arr[i] = in[arr[i]];
}
for(int i=1; i<=q; i++){
scanf("%d %d", &query[i].l, &query[i].r);
query[i].idx = i;
}
sort(query+1, query+q+1);
int l = 1, r = 0;
for(int i=1; i<=q; i++){
while(r < query[i].r) add(++r);
while(l > query[i].l) add(--l);
while(r > query[i].r) del(r--);
while(l < query[i].l) del(l++);
ans[query[i].idx] = distSum / 2 + 1;
}
for(int i=1; i<=q; i++){
printf("%d\n", ans[i]);
}
}
Compilation message
tourism.cpp: In function 'int main()':
tourism.cpp:118:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
118 | scanf("%d %d %d", &n, &k, &q);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
tourism.cpp:121:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
121 | scanf("%d %d", &x, &y);
| ~~~~~^~~~~~~~~~~~~~~~~
tourism.cpp:136:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
136 | scanf("%d", &arr[i]);
| ~~~~~^~~~~~~~~~~~~~~
tourism.cpp:140:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
140 | scanf("%d %d", &query[i].l, &query[i].r);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
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 |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
3 ms |
2644 KB |
Output is correct |
5 |
Correct |
3 ms |
2772 KB |
Output is correct |
6 |
Correct |
2 ms |
2644 KB |
Output is correct |
7 |
Correct |
3 ms |
2772 KB |
Output is correct |
8 |
Correct |
2 ms |
2772 KB |
Output is correct |
9 |
Correct |
3 ms |
2772 KB |
Output is correct |
10 |
Correct |
4 ms |
2788 KB |
Output is correct |
11 |
Correct |
3 ms |
2772 KB |
Output is correct |
12 |
Correct |
2 ms |
2772 KB |
Output is correct |
13 |
Correct |
2 ms |
2772 KB |
Output is correct |
14 |
Correct |
2 ms |
2772 KB |
Output is correct |
15 |
Correct |
4 ms |
2772 KB |
Output is correct |
16 |
Correct |
3 ms |
2772 KB |
Output is correct |
17 |
Correct |
4 ms |
2772 KB |
Output is correct |
18 |
Correct |
5 ms |
2792 KB |
Output is correct |
19 |
Correct |
4 ms |
2772 KB |
Output is correct |
20 |
Correct |
3 ms |
2772 KB |
Output is correct |
21 |
Correct |
4 ms |
2788 KB |
Output is correct |
22 |
Correct |
3 ms |
2772 KB |
Output is correct |
23 |
Correct |
3 ms |
2772 KB |
Output is correct |
24 |
Correct |
3 ms |
2772 KB |
Output is correct |
25 |
Correct |
4 ms |
2772 KB |
Output is correct |
26 |
Correct |
4 ms |
2772 KB |
Output is correct |
27 |
Correct |
2 ms |
2772 KB |
Output is correct |
28 |
Correct |
2 ms |
2772 KB |
Output is correct |
29 |
Correct |
2 ms |
2772 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 |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
3 ms |
2644 KB |
Output is correct |
5 |
Correct |
3 ms |
2772 KB |
Output is correct |
6 |
Correct |
2 ms |
2644 KB |
Output is correct |
7 |
Correct |
3 ms |
2772 KB |
Output is correct |
8 |
Correct |
2 ms |
2772 KB |
Output is correct |
9 |
Correct |
3 ms |
2772 KB |
Output is correct |
10 |
Correct |
4 ms |
2788 KB |
Output is correct |
11 |
Correct |
3 ms |
2772 KB |
Output is correct |
12 |
Correct |
2 ms |
2772 KB |
Output is correct |
13 |
Correct |
2 ms |
2772 KB |
Output is correct |
14 |
Correct |
2 ms |
2772 KB |
Output is correct |
15 |
Correct |
4 ms |
2772 KB |
Output is correct |
16 |
Correct |
3 ms |
2772 KB |
Output is correct |
17 |
Correct |
4 ms |
2772 KB |
Output is correct |
18 |
Correct |
5 ms |
2792 KB |
Output is correct |
19 |
Correct |
4 ms |
2772 KB |
Output is correct |
20 |
Correct |
3 ms |
2772 KB |
Output is correct |
21 |
Correct |
4 ms |
2788 KB |
Output is correct |
22 |
Correct |
3 ms |
2772 KB |
Output is correct |
23 |
Correct |
3 ms |
2772 KB |
Output is correct |
24 |
Correct |
3 ms |
2772 KB |
Output is correct |
25 |
Correct |
4 ms |
2772 KB |
Output is correct |
26 |
Correct |
4 ms |
2772 KB |
Output is correct |
27 |
Correct |
2 ms |
2772 KB |
Output is correct |
28 |
Correct |
2 ms |
2772 KB |
Output is correct |
29 |
Correct |
2 ms |
2772 KB |
Output is correct |
30 |
Correct |
21 ms |
3080 KB |
Output is correct |
31 |
Correct |
25 ms |
3028 KB |
Output is correct |
32 |
Correct |
38 ms |
3188 KB |
Output is correct |
33 |
Correct |
35 ms |
3184 KB |
Output is correct |
34 |
Correct |
32 ms |
3200 KB |
Output is correct |
35 |
Correct |
5 ms |
3212 KB |
Output is correct |
36 |
Correct |
6 ms |
3156 KB |
Output is correct |
37 |
Correct |
5 ms |
3156 KB |
Output is correct |
38 |
Correct |
37 ms |
3288 KB |
Output is correct |
39 |
Correct |
35 ms |
3296 KB |
Output is correct |
40 |
Correct |
32 ms |
3324 KB |
Output is correct |
41 |
Correct |
6 ms |
3344 KB |
Output is correct |
42 |
Correct |
5 ms |
3360 KB |
Output is correct |
43 |
Correct |
5 ms |
3284 KB |
Output is correct |
44 |
Correct |
33 ms |
3256 KB |
Output is correct |
45 |
Correct |
33 ms |
3232 KB |
Output is correct |
46 |
Correct |
33 ms |
3256 KB |
Output is correct |
47 |
Correct |
5 ms |
3156 KB |
Output is correct |
48 |
Correct |
6 ms |
3232 KB |
Output is correct |
49 |
Correct |
5 ms |
3156 KB |
Output is correct |
50 |
Correct |
32 ms |
3196 KB |
Output is correct |
51 |
Correct |
32 ms |
3212 KB |
Output is correct |
52 |
Correct |
39 ms |
3156 KB |
Output is correct |
53 |
Correct |
34 ms |
3192 KB |
Output is correct |
54 |
Correct |
32 ms |
3212 KB |
Output is correct |
55 |
Correct |
34 ms |
3156 KB |
Output is correct |
56 |
Correct |
4 ms |
2644 KB |
Output is correct |
57 |
Correct |
2 ms |
3156 KB |
Output is correct |
58 |
Correct |
4 ms |
3156 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
4 ms |
2752 KB |
Output is correct |
4 |
Runtime error |
67 ms |
40832 KB |
Execution killed with signal 11 |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
72 ms |
15256 KB |
Output is correct |
3 |
Correct |
103 ms |
16408 KB |
Output is correct |
4 |
Correct |
94 ms |
18648 KB |
Output is correct |
5 |
Correct |
101 ms |
25612 KB |
Output is correct |
6 |
Correct |
130 ms |
25628 KB |
Output is correct |
7 |
Correct |
152 ms |
25708 KB |
Output is correct |
8 |
Correct |
154 ms |
25704 KB |
Output is correct |
9 |
Correct |
150 ms |
25656 KB |
Output is correct |
10 |
Correct |
155 ms |
25676 KB |
Output is correct |
11 |
Correct |
158 ms |
25624 KB |
Output is correct |
12 |
Correct |
148 ms |
25744 KB |
Output is correct |
13 |
Correct |
146 ms |
25888 KB |
Output is correct |
14 |
Correct |
148 ms |
26156 KB |
Output is correct |
15 |
Runtime error |
110 ms |
54320 KB |
Execution killed with signal 11 |
16 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2728 KB |
Output is correct |
3 |
Correct |
4 ms |
2644 KB |
Output is correct |
4 |
Runtime error |
72 ms |
33868 KB |
Execution killed with signal 11 |
5 |
Halted |
0 ms |
0 KB |
- |
# |
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 |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
3 ms |
2644 KB |
Output is correct |
5 |
Correct |
3 ms |
2772 KB |
Output is correct |
6 |
Correct |
2 ms |
2644 KB |
Output is correct |
7 |
Correct |
3 ms |
2772 KB |
Output is correct |
8 |
Correct |
2 ms |
2772 KB |
Output is correct |
9 |
Correct |
3 ms |
2772 KB |
Output is correct |
10 |
Correct |
4 ms |
2788 KB |
Output is correct |
11 |
Correct |
3 ms |
2772 KB |
Output is correct |
12 |
Correct |
2 ms |
2772 KB |
Output is correct |
13 |
Correct |
2 ms |
2772 KB |
Output is correct |
14 |
Correct |
2 ms |
2772 KB |
Output is correct |
15 |
Correct |
4 ms |
2772 KB |
Output is correct |
16 |
Correct |
3 ms |
2772 KB |
Output is correct |
17 |
Correct |
4 ms |
2772 KB |
Output is correct |
18 |
Correct |
5 ms |
2792 KB |
Output is correct |
19 |
Correct |
4 ms |
2772 KB |
Output is correct |
20 |
Correct |
3 ms |
2772 KB |
Output is correct |
21 |
Correct |
4 ms |
2788 KB |
Output is correct |
22 |
Correct |
3 ms |
2772 KB |
Output is correct |
23 |
Correct |
3 ms |
2772 KB |
Output is correct |
24 |
Correct |
3 ms |
2772 KB |
Output is correct |
25 |
Correct |
4 ms |
2772 KB |
Output is correct |
26 |
Correct |
4 ms |
2772 KB |
Output is correct |
27 |
Correct |
2 ms |
2772 KB |
Output is correct |
28 |
Correct |
2 ms |
2772 KB |
Output is correct |
29 |
Correct |
2 ms |
2772 KB |
Output is correct |
30 |
Correct |
21 ms |
3080 KB |
Output is correct |
31 |
Correct |
25 ms |
3028 KB |
Output is correct |
32 |
Correct |
38 ms |
3188 KB |
Output is correct |
33 |
Correct |
35 ms |
3184 KB |
Output is correct |
34 |
Correct |
32 ms |
3200 KB |
Output is correct |
35 |
Correct |
5 ms |
3212 KB |
Output is correct |
36 |
Correct |
6 ms |
3156 KB |
Output is correct |
37 |
Correct |
5 ms |
3156 KB |
Output is correct |
38 |
Correct |
37 ms |
3288 KB |
Output is correct |
39 |
Correct |
35 ms |
3296 KB |
Output is correct |
40 |
Correct |
32 ms |
3324 KB |
Output is correct |
41 |
Correct |
6 ms |
3344 KB |
Output is correct |
42 |
Correct |
5 ms |
3360 KB |
Output is correct |
43 |
Correct |
5 ms |
3284 KB |
Output is correct |
44 |
Correct |
33 ms |
3256 KB |
Output is correct |
45 |
Correct |
33 ms |
3232 KB |
Output is correct |
46 |
Correct |
33 ms |
3256 KB |
Output is correct |
47 |
Correct |
5 ms |
3156 KB |
Output is correct |
48 |
Correct |
6 ms |
3232 KB |
Output is correct |
49 |
Correct |
5 ms |
3156 KB |
Output is correct |
50 |
Correct |
32 ms |
3196 KB |
Output is correct |
51 |
Correct |
32 ms |
3212 KB |
Output is correct |
52 |
Correct |
39 ms |
3156 KB |
Output is correct |
53 |
Correct |
34 ms |
3192 KB |
Output is correct |
54 |
Correct |
32 ms |
3212 KB |
Output is correct |
55 |
Correct |
34 ms |
3156 KB |
Output is correct |
56 |
Correct |
4 ms |
2644 KB |
Output is correct |
57 |
Correct |
2 ms |
3156 KB |
Output is correct |
58 |
Correct |
4 ms |
3156 KB |
Output is correct |
59 |
Correct |
1 ms |
2644 KB |
Output is correct |
60 |
Correct |
2 ms |
2644 KB |
Output is correct |
61 |
Correct |
4 ms |
2752 KB |
Output is correct |
62 |
Runtime error |
67 ms |
40832 KB |
Execution killed with signal 11 |
63 |
Halted |
0 ms |
0 KB |
- |