# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
789112 |
2023-07-21T05:09:39 Z |
박상훈(#10042) |
Tourism (JOI23_tourism) |
C++17 |
|
5000 ms |
16656 KB |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
constexpr int B = 400, INF = 1e9 + 100;
vector<int> adj[100100];
int a[100100], ans[100100], cnt[100100], val;
multiset<pair<int, int>> st;
int in[100100], out[100100], dep[100100], cnti;
struct Query{
int s, e, i;
Query(){}
Query(int _s, int _e, int _i): s(_s), e(_e), i(_i) {}
bool operator < (const Query &Q) const{
if (s / B == Q.s / B) return e < Q.e;
return s / B < Q.s / B;
}
}Q[100100];
struct Seg{
pair<int, int> tree[400400];
int sz;
void init(int n){
sz = n;
// for (int i=sz;i<sz*2;i++) printf("(%d, %d) ", tree[i].first, tree[i].second);
// printf("\n");
for (int i=sz-1;i;i--) tree[i] = min(tree[i<<1], tree[i<<1|1]);
}
int query(int l, int r){
// printf(" query %d to %d\n", l, r);
++r;
pair<int, int> ret = {INF, 0};
for (l+=sz, r+=sz;l<r;l>>=1, r>>=1){
if (l&1) ret = min(ret, tree[l++]);
if (r&1) ret = min(ret, tree[--r]);
}
return ret.second;
}
}rmq;
void dfs(int s, int pa = 0){
in[s] = ++cnti;
dep[s] = dep[pa] + 1;
rmq.tree[cnti + rmq.sz] = {dep[s], s};
for (auto &v:adj[s]) if (v!=pa){
dfs(v, s);
++cnti;
rmq.tree[cnti + rmq.sz] = {dep[s], s};
}
// out[s] = ++cnti;
// rmq.tree[cnti + rmq.sz] = {dep[s], s};
}
int dist(int x, int y){
int lca = rmq.query(min(in[x], in[y]), max(in[x], in[y]));
// printf(" calc %d %d -> %d (lca = %d)\n", x, y, dep[x] + dep[y] - dep[lca] * 2, lca);
return dep[x] + dep[y] - dep[lca] * 2;
}
void push(int x){
x = a[x];
// printf("push %d\n", x);
cnt[x]++;
if (cnt[x]==1){
auto iter = st.emplace(in[x], x);
auto prv = (iter==st.begin())?(prev(st.end())):(prev(iter));
auto nxt = (next(iter)==st.end())?(st.begin()):(next(iter));
val -= dist(prv->second, nxt->second);
val += dist(prv->second, iter->second);
val += dist(iter->second, nxt->second);
}
// printf(" ok val = %d\n", val);
}
void pop(int x){
x = a[x];
// printf("pop %d\n", x);
cnt[x]--;
if (cnt[x]==0){
auto iter = st.find(pair<int, int>(in[x], x));
assert(iter!=st.end());
auto prv = (iter==st.begin())?(prev(st.end())):(prev(iter));
auto nxt = (next(iter)==st.end())?(st.begin()):(next(iter));
val += dist(prv->second, nxt->second);
val -= dist(prv->second, iter->second);
val -= dist(iter->second, nxt->second);
st.erase(iter);
}
// printf(" ok val = %d\n", val);
}
int main(){
int n, m, q;
scanf("%d %d %d", &n, &m, &q);
for (int i=1;i<=n-1;i++){
int x, y;
scanf("%d %d", &x, &y);
adj[x].push_back(y);
adj[y].push_back(x);
}
for (int i=1;i<=m;i++) scanf("%d", a+i);
for (int i=1;i<=q;i++){
scanf("%d %d", &Q[i].s, &Q[i].e);
Q[i].i = i;
}
rmq.sz = n*2+10;
dfs(1);
rmq.init(n*2+10);
sort(Q+1, Q+q+1);
int l = 1, r = 0;
for (int i=1;i<=q;i++){
while(r<Q[i].e){
++r;
push(r);
}
while(l>Q[i].s){
--l;
push(l);
}
while(r>Q[i].e){
pop(r);
r--;
}
while(l<Q[i].s){
pop(l);
l++;
}
ans[Q[i].i] = val/2 + 1;
}
for (int i=1;i<=q;i++) printf("%d\n", ans[i]);
}
Compilation message
tourism.cpp: In function 'int main()':
tourism.cpp:111:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
111 | scanf("%d %d %d", &n, &m, &q);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
tourism.cpp:115:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
115 | scanf("%d %d", &x, &y);
| ~~~~~^~~~~~~~~~~~~~~~~
tourism.cpp:120:30: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
120 | for (int i=1;i<=m;i++) scanf("%d", a+i);
| ~~~~~^~~~~~~~~~~
tourism.cpp:123:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
123 | scanf("%d %d", &Q[i].s, &Q[i].e);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
5716 KB |
Output is correct |
2 |
Correct |
2 ms |
5716 KB |
Output is correct |
3 |
Correct |
3 ms |
5716 KB |
Output is correct |
4 |
Correct |
4 ms |
5716 KB |
Output is correct |
5 |
Correct |
5 ms |
5716 KB |
Output is correct |
6 |
Correct |
3 ms |
5800 KB |
Output is correct |
7 |
Correct |
4 ms |
5716 KB |
Output is correct |
8 |
Correct |
4 ms |
5832 KB |
Output is correct |
9 |
Correct |
5 ms |
5840 KB |
Output is correct |
10 |
Correct |
5 ms |
5844 KB |
Output is correct |
11 |
Correct |
5 ms |
5856 KB |
Output is correct |
12 |
Correct |
3 ms |
5844 KB |
Output is correct |
13 |
Correct |
3 ms |
5844 KB |
Output is correct |
14 |
Correct |
3 ms |
5844 KB |
Output is correct |
15 |
Correct |
5 ms |
5844 KB |
Output is correct |
16 |
Correct |
5 ms |
5844 KB |
Output is correct |
17 |
Correct |
5 ms |
5844 KB |
Output is correct |
18 |
Correct |
5 ms |
5844 KB |
Output is correct |
19 |
Correct |
5 ms |
5844 KB |
Output is correct |
20 |
Correct |
5 ms |
5852 KB |
Output is correct |
21 |
Correct |
5 ms |
5844 KB |
Output is correct |
22 |
Correct |
5 ms |
5844 KB |
Output is correct |
23 |
Correct |
4 ms |
5844 KB |
Output is correct |
24 |
Correct |
5 ms |
5844 KB |
Output is correct |
25 |
Correct |
5 ms |
5844 KB |
Output is correct |
26 |
Correct |
5 ms |
5804 KB |
Output is correct |
27 |
Correct |
2 ms |
5716 KB |
Output is correct |
28 |
Correct |
2 ms |
5716 KB |
Output is correct |
29 |
Correct |
3 ms |
5716 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
5716 KB |
Output is correct |
2 |
Correct |
2 ms |
5716 KB |
Output is correct |
3 |
Correct |
3 ms |
5716 KB |
Output is correct |
4 |
Correct |
4 ms |
5716 KB |
Output is correct |
5 |
Correct |
5 ms |
5716 KB |
Output is correct |
6 |
Correct |
3 ms |
5800 KB |
Output is correct |
7 |
Correct |
4 ms |
5716 KB |
Output is correct |
8 |
Correct |
4 ms |
5832 KB |
Output is correct |
9 |
Correct |
5 ms |
5840 KB |
Output is correct |
10 |
Correct |
5 ms |
5844 KB |
Output is correct |
11 |
Correct |
5 ms |
5856 KB |
Output is correct |
12 |
Correct |
3 ms |
5844 KB |
Output is correct |
13 |
Correct |
3 ms |
5844 KB |
Output is correct |
14 |
Correct |
3 ms |
5844 KB |
Output is correct |
15 |
Correct |
5 ms |
5844 KB |
Output is correct |
16 |
Correct |
5 ms |
5844 KB |
Output is correct |
17 |
Correct |
5 ms |
5844 KB |
Output is correct |
18 |
Correct |
5 ms |
5844 KB |
Output is correct |
19 |
Correct |
5 ms |
5844 KB |
Output is correct |
20 |
Correct |
5 ms |
5852 KB |
Output is correct |
21 |
Correct |
5 ms |
5844 KB |
Output is correct |
22 |
Correct |
5 ms |
5844 KB |
Output is correct |
23 |
Correct |
4 ms |
5844 KB |
Output is correct |
24 |
Correct |
5 ms |
5844 KB |
Output is correct |
25 |
Correct |
5 ms |
5844 KB |
Output is correct |
26 |
Correct |
5 ms |
5804 KB |
Output is correct |
27 |
Correct |
2 ms |
5716 KB |
Output is correct |
28 |
Correct |
2 ms |
5716 KB |
Output is correct |
29 |
Correct |
3 ms |
5716 KB |
Output is correct |
30 |
Correct |
27 ms |
5920 KB |
Output is correct |
31 |
Correct |
34 ms |
5944 KB |
Output is correct |
32 |
Correct |
44 ms |
5984 KB |
Output is correct |
33 |
Correct |
43 ms |
5992 KB |
Output is correct |
34 |
Correct |
40 ms |
5972 KB |
Output is correct |
35 |
Correct |
4 ms |
6004 KB |
Output is correct |
36 |
Correct |
5 ms |
5972 KB |
Output is correct |
37 |
Correct |
5 ms |
5972 KB |
Output is correct |
38 |
Correct |
34 ms |
6092 KB |
Output is correct |
39 |
Correct |
33 ms |
6092 KB |
Output is correct |
40 |
Correct |
34 ms |
6100 KB |
Output is correct |
41 |
Correct |
5 ms |
6100 KB |
Output is correct |
42 |
Correct |
5 ms |
6100 KB |
Output is correct |
43 |
Correct |
5 ms |
6100 KB |
Output is correct |
44 |
Correct |
35 ms |
5972 KB |
Output is correct |
45 |
Correct |
34 ms |
6032 KB |
Output is correct |
46 |
Correct |
38 ms |
6052 KB |
Output is correct |
47 |
Correct |
5 ms |
5972 KB |
Output is correct |
48 |
Correct |
5 ms |
5972 KB |
Output is correct |
49 |
Correct |
5 ms |
5972 KB |
Output is correct |
50 |
Correct |
35 ms |
6008 KB |
Output is correct |
51 |
Correct |
33 ms |
5972 KB |
Output is correct |
52 |
Correct |
33 ms |
6004 KB |
Output is correct |
53 |
Correct |
35 ms |
5964 KB |
Output is correct |
54 |
Correct |
34 ms |
5948 KB |
Output is correct |
55 |
Correct |
34 ms |
6012 KB |
Output is correct |
56 |
Correct |
4 ms |
5844 KB |
Output is correct |
57 |
Correct |
3 ms |
5844 KB |
Output is correct |
58 |
Correct |
6 ms |
5844 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
5716 KB |
Output is correct |
2 |
Correct |
3 ms |
5716 KB |
Output is correct |
3 |
Correct |
4 ms |
5844 KB |
Output is correct |
4 |
Execution timed out |
5031 ms |
16656 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
5716 KB |
Output is correct |
2 |
Correct |
82 ms |
8456 KB |
Output is correct |
3 |
Correct |
127 ms |
8804 KB |
Output is correct |
4 |
Correct |
102 ms |
9188 KB |
Output is correct |
5 |
Correct |
70 ms |
13564 KB |
Output is correct |
6 |
Correct |
83 ms |
12756 KB |
Output is correct |
7 |
Correct |
136 ms |
11344 KB |
Output is correct |
8 |
Correct |
122 ms |
10828 KB |
Output is correct |
9 |
Correct |
171 ms |
10688 KB |
Output is correct |
10 |
Correct |
139 ms |
10584 KB |
Output is correct |
11 |
Correct |
149 ms |
10528 KB |
Output is correct |
12 |
Correct |
155 ms |
10640 KB |
Output is correct |
13 |
Correct |
174 ms |
10728 KB |
Output is correct |
14 |
Correct |
196 ms |
11100 KB |
Output is correct |
15 |
Correct |
153 ms |
12336 KB |
Output is correct |
16 |
Correct |
146 ms |
12496 KB |
Output is correct |
17 |
Correct |
166 ms |
12416 KB |
Output is correct |
18 |
Correct |
161 ms |
12416 KB |
Output is correct |
19 |
Correct |
77 ms |
13616 KB |
Output is correct |
20 |
Correct |
95 ms |
13596 KB |
Output is correct |
21 |
Correct |
100 ms |
12164 KB |
Output is correct |
22 |
Correct |
110 ms |
11400 KB |
Output is correct |
23 |
Correct |
115 ms |
11088 KB |
Output is correct |
24 |
Correct |
112 ms |
10800 KB |
Output is correct |
25 |
Correct |
119 ms |
10584 KB |
Output is correct |
26 |
Correct |
138 ms |
10696 KB |
Output is correct |
27 |
Correct |
142 ms |
10664 KB |
Output is correct |
28 |
Correct |
144 ms |
10620 KB |
Output is correct |
29 |
Correct |
135 ms |
10660 KB |
Output is correct |
30 |
Correct |
154 ms |
10740 KB |
Output is correct |
31 |
Correct |
171 ms |
10780 KB |
Output is correct |
32 |
Correct |
154 ms |
10980 KB |
Output is correct |
33 |
Correct |
160 ms |
11468 KB |
Output is correct |
34 |
Correct |
141 ms |
12368 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5716 KB |
Output is correct |
2 |
Correct |
3 ms |
5716 KB |
Output is correct |
3 |
Correct |
4 ms |
5796 KB |
Output is correct |
4 |
Execution timed out |
5084 ms |
12868 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
5716 KB |
Output is correct |
2 |
Correct |
2 ms |
5716 KB |
Output is correct |
3 |
Correct |
3 ms |
5716 KB |
Output is correct |
4 |
Correct |
4 ms |
5716 KB |
Output is correct |
5 |
Correct |
5 ms |
5716 KB |
Output is correct |
6 |
Correct |
3 ms |
5800 KB |
Output is correct |
7 |
Correct |
4 ms |
5716 KB |
Output is correct |
8 |
Correct |
4 ms |
5832 KB |
Output is correct |
9 |
Correct |
5 ms |
5840 KB |
Output is correct |
10 |
Correct |
5 ms |
5844 KB |
Output is correct |
11 |
Correct |
5 ms |
5856 KB |
Output is correct |
12 |
Correct |
3 ms |
5844 KB |
Output is correct |
13 |
Correct |
3 ms |
5844 KB |
Output is correct |
14 |
Correct |
3 ms |
5844 KB |
Output is correct |
15 |
Correct |
5 ms |
5844 KB |
Output is correct |
16 |
Correct |
5 ms |
5844 KB |
Output is correct |
17 |
Correct |
5 ms |
5844 KB |
Output is correct |
18 |
Correct |
5 ms |
5844 KB |
Output is correct |
19 |
Correct |
5 ms |
5844 KB |
Output is correct |
20 |
Correct |
5 ms |
5852 KB |
Output is correct |
21 |
Correct |
5 ms |
5844 KB |
Output is correct |
22 |
Correct |
5 ms |
5844 KB |
Output is correct |
23 |
Correct |
4 ms |
5844 KB |
Output is correct |
24 |
Correct |
5 ms |
5844 KB |
Output is correct |
25 |
Correct |
5 ms |
5844 KB |
Output is correct |
26 |
Correct |
5 ms |
5804 KB |
Output is correct |
27 |
Correct |
2 ms |
5716 KB |
Output is correct |
28 |
Correct |
2 ms |
5716 KB |
Output is correct |
29 |
Correct |
3 ms |
5716 KB |
Output is correct |
30 |
Correct |
27 ms |
5920 KB |
Output is correct |
31 |
Correct |
34 ms |
5944 KB |
Output is correct |
32 |
Correct |
44 ms |
5984 KB |
Output is correct |
33 |
Correct |
43 ms |
5992 KB |
Output is correct |
34 |
Correct |
40 ms |
5972 KB |
Output is correct |
35 |
Correct |
4 ms |
6004 KB |
Output is correct |
36 |
Correct |
5 ms |
5972 KB |
Output is correct |
37 |
Correct |
5 ms |
5972 KB |
Output is correct |
38 |
Correct |
34 ms |
6092 KB |
Output is correct |
39 |
Correct |
33 ms |
6092 KB |
Output is correct |
40 |
Correct |
34 ms |
6100 KB |
Output is correct |
41 |
Correct |
5 ms |
6100 KB |
Output is correct |
42 |
Correct |
5 ms |
6100 KB |
Output is correct |
43 |
Correct |
5 ms |
6100 KB |
Output is correct |
44 |
Correct |
35 ms |
5972 KB |
Output is correct |
45 |
Correct |
34 ms |
6032 KB |
Output is correct |
46 |
Correct |
38 ms |
6052 KB |
Output is correct |
47 |
Correct |
5 ms |
5972 KB |
Output is correct |
48 |
Correct |
5 ms |
5972 KB |
Output is correct |
49 |
Correct |
5 ms |
5972 KB |
Output is correct |
50 |
Correct |
35 ms |
6008 KB |
Output is correct |
51 |
Correct |
33 ms |
5972 KB |
Output is correct |
52 |
Correct |
33 ms |
6004 KB |
Output is correct |
53 |
Correct |
35 ms |
5964 KB |
Output is correct |
54 |
Correct |
34 ms |
5948 KB |
Output is correct |
55 |
Correct |
34 ms |
6012 KB |
Output is correct |
56 |
Correct |
4 ms |
5844 KB |
Output is correct |
57 |
Correct |
3 ms |
5844 KB |
Output is correct |
58 |
Correct |
6 ms |
5844 KB |
Output is correct |
59 |
Correct |
2 ms |
5716 KB |
Output is correct |
60 |
Correct |
3 ms |
5716 KB |
Output is correct |
61 |
Correct |
4 ms |
5844 KB |
Output is correct |
62 |
Execution timed out |
5031 ms |
16656 KB |
Time limit exceeded |
63 |
Halted |
0 ms |
0 KB |
- |