답안 #987673

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
987673 2024-05-23T10:42:23 Z snpmrnhlol Tourism (JOI23_tourism) C++17
100 / 100
779 ms 24864 KB
#include<bits/stdc++.h>
using namespace std;
const int N = 1e5;
vector <int> e[N];
vector <pair<int,int>> q2[N];
map <int,pair<int,int>> f;
int sub[N];
int top[N];
int st[N],cnt = 0;
int c[N];
int d[N];
int pr[N];
int ans[N];
int st2[N];
int n,m,q;
void dfs(int node, int p){
    sub[node] = 1;
    pr[node] = p;
    if(p == -1){
        d[node] = 0;
    }else{
        d[node] = d[p] + 1;
    }
    for(auto i:e[node]){
        if(i == p)continue;
        dfs(i, node);
        sub[node]+=sub[i];
    }
}
void dfs2(int node, int p){
    int mxid = -1;
    for(auto i:e[node]){
        if(i == p)continue;
        if(mxid == -1 || sub[mxid] < sub[i]){
            mxid = i;
        }
    }
    st[node] = cnt++;
    st2[cnt - 1] = node;
    if(mxid != -1){
        top[mxid] = top[node];
        dfs2(mxid, node);
    }
    for(auto i:e[node]){
        if(i == p || i == mxid)continue;
        top[i] = i;
        dfs2(i, node);
    }
}
int fen[N];
void add2(int pos,int nr){
    for(int i = pos;i >= 0;i&=(i + 1),i--){
        fen[i]+=nr;
    }
}
void add(int u,int w,int id){
    u = st[u];
    w = st[w];
    if(u > w)swap(u,w);
    ///segment override
    while(1){
        auto x = f.lower_bound(u);
        if(x == f.end() || x->first > w){
            break;
        }
        int l = min(x->first,u),r = max(x->second.first,w),id2 = x->second.second;
        add2(id2,-(min(x->second.first,w) - max(x->first,u) + 1));
        f.erase(x);
        if(l < u){
            f[l] = {u - 1,id2};
        }
        if(w < r){
            f[w + 1] = {r,id2};
        }
    }
    auto x = f.lower_bound(u);
    if(x != f.begin() && prev(x)->second.first >= u){
        x = prev(x);
        int l = min(x->first,u),r = max(x->second.first,w),id2 = x->second.second;
        add2(id2,-(min(x->second.first,w) - max(x->first,u) + 1));
        f.erase(x);
        if(l < u){
            f[l] = {u - 1,id2};
        }
        if(w < r){
            f[w + 1] = {r,id2};
        }
    }
    add2(id,w - u + 1);
    f[u] = {w,id};
}
int get(int pos){
    int r = 0;
    for(int i = pos;i < m;i|=(i + 1)){
        r+=fen[i];
    }
    return r;
}
int main(){
    //ios_base::sync_with_stdio(0);
    //cin.tie(0);
    cin>>n>>m>>q;
    for(int i = 0;i < n - 1;i++){
        int a,b;
        cin>>a>>b;
        a--;b--;
        e[a].push_back(b);
        e[b].push_back(a);
    }
    dfs(0, -1);
    top[0] = 0;
    dfs2(0, -1);
    for(int i = 0;i < m;i++){
        cin>>c[i];
        c[i]--;
    }
    for(int i = 0;i < q;i++){
        int l,r;
        cin>>l>>r;
        if(l == r){
            ans[i] = 1;
            continue;
        }
        q2[r - 1].push_back({l - 1,i});
    }
    for(int i = 1;i < m;i++){
        ///add
        int u = c[i],w = c[i - 1];
        while(top[u] != top[w]){
            if(d[top[u]] > d[top[w]])swap(u,w);
            //cout<<w<<' '<<top[w]<<' '<<i - 1<<'\n';
            add(w,top[w],i - 1);
            w = pr[top[w]];
        }
        //cout<<u<<' '<<w<<' '<<i - 1<<'\n';
        add(u,w,i - 1);
        get(0);
        ///queries
        for(auto j:q2[i]){
            ans[j.second] = get(j.first);
        }
    }
    for(int i = 0;i < q;i++){
        cout<<ans[i]<<'\n';
    }
    return 0;
}
/**
8 3 1
1 2
2 3
3 4
4 5
5 6
6 7
7 8
2 4 7
1 3
**/
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 8280 KB Output is correct
2 Correct 3 ms 8284 KB Output is correct
3 Correct 3 ms 8284 KB Output is correct
4 Correct 4 ms 8284 KB Output is correct
5 Correct 4 ms 8480 KB Output is correct
6 Correct 3 ms 8284 KB Output is correct
7 Correct 3 ms 8284 KB Output is correct
8 Correct 3 ms 8284 KB Output is correct
9 Correct 4 ms 8284 KB Output is correct
10 Correct 3 ms 8284 KB Output is correct
11 Correct 3 ms 8284 KB Output is correct
12 Correct 3 ms 8284 KB Output is correct
13 Correct 3 ms 8284 KB Output is correct
14 Correct 3 ms 8284 KB Output is correct
15 Correct 3 ms 8536 KB Output is correct
16 Correct 3 ms 8284 KB Output is correct
17 Correct 3 ms 8472 KB Output is correct
18 Correct 3 ms 8512 KB Output is correct
19 Correct 3 ms 8292 KB Output is correct
20 Correct 3 ms 8284 KB Output is correct
21 Correct 3 ms 8284 KB Output is correct
22 Correct 3 ms 8436 KB Output is correct
23 Correct 3 ms 8280 KB Output is correct
24 Correct 3 ms 8284 KB Output is correct
25 Correct 3 ms 8284 KB Output is correct
26 Correct 3 ms 8284 KB Output is correct
27 Correct 3 ms 8280 KB Output is correct
28 Correct 3 ms 8284 KB Output is correct
29 Correct 4 ms 8280 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 8280 KB Output is correct
2 Correct 3 ms 8284 KB Output is correct
3 Correct 3 ms 8284 KB Output is correct
4 Correct 4 ms 8284 KB Output is correct
5 Correct 4 ms 8480 KB Output is correct
6 Correct 3 ms 8284 KB Output is correct
7 Correct 3 ms 8284 KB Output is correct
8 Correct 3 ms 8284 KB Output is correct
9 Correct 4 ms 8284 KB Output is correct
10 Correct 3 ms 8284 KB Output is correct
11 Correct 3 ms 8284 KB Output is correct
12 Correct 3 ms 8284 KB Output is correct
13 Correct 3 ms 8284 KB Output is correct
14 Correct 3 ms 8284 KB Output is correct
15 Correct 3 ms 8536 KB Output is correct
16 Correct 3 ms 8284 KB Output is correct
17 Correct 3 ms 8472 KB Output is correct
18 Correct 3 ms 8512 KB Output is correct
19 Correct 3 ms 8292 KB Output is correct
20 Correct 3 ms 8284 KB Output is correct
21 Correct 3 ms 8284 KB Output is correct
22 Correct 3 ms 8436 KB Output is correct
23 Correct 3 ms 8280 KB Output is correct
24 Correct 3 ms 8284 KB Output is correct
25 Correct 3 ms 8284 KB Output is correct
26 Correct 3 ms 8284 KB Output is correct
27 Correct 3 ms 8280 KB Output is correct
28 Correct 3 ms 8284 KB Output is correct
29 Correct 4 ms 8280 KB Output is correct
30 Correct 7 ms 8540 KB Output is correct
31 Correct 7 ms 8540 KB Output is correct
32 Correct 10 ms 8540 KB Output is correct
33 Correct 8 ms 8540 KB Output is correct
34 Correct 8 ms 8540 KB Output is correct
35 Correct 8 ms 8540 KB Output is correct
36 Correct 9 ms 8540 KB Output is correct
37 Correct 9 ms 8480 KB Output is correct
38 Correct 6 ms 8540 KB Output is correct
39 Correct 6 ms 8536 KB Output is correct
40 Correct 6 ms 8540 KB Output is correct
41 Correct 7 ms 8536 KB Output is correct
42 Correct 5 ms 8540 KB Output is correct
43 Correct 5 ms 8540 KB Output is correct
44 Correct 6 ms 8536 KB Output is correct
45 Correct 7 ms 8540 KB Output is correct
46 Correct 6 ms 8536 KB Output is correct
47 Correct 6 ms 8540 KB Output is correct
48 Correct 6 ms 8540 KB Output is correct
49 Correct 7 ms 8540 KB Output is correct
50 Correct 6 ms 8540 KB Output is correct
51 Correct 7 ms 8540 KB Output is correct
52 Correct 7 ms 8540 KB Output is correct
53 Correct 7 ms 8480 KB Output is correct
54 Correct 6 ms 8540 KB Output is correct
55 Correct 8 ms 8540 KB Output is correct
56 Correct 4 ms 8284 KB Output is correct
57 Correct 5 ms 8540 KB Output is correct
58 Correct 11 ms 8536 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 8280 KB Output is correct
2 Correct 3 ms 8284 KB Output is correct
3 Correct 4 ms 8540 KB Output is correct
4 Correct 120 ms 16688 KB Output is correct
5 Correct 114 ms 18920 KB Output is correct
6 Correct 122 ms 20188 KB Output is correct
7 Correct 160 ms 22084 KB Output is correct
8 Correct 178 ms 22324 KB Output is correct
9 Correct 159 ms 22096 KB Output is correct
10 Correct 160 ms 22096 KB Output is correct
11 Correct 177 ms 22308 KB Output is correct
12 Correct 154 ms 21792 KB Output is correct
13 Correct 150 ms 21628 KB Output is correct
14 Correct 156 ms 21712 KB Output is correct
15 Correct 82 ms 21024 KB Output is correct
16 Correct 150 ms 22612 KB Output is correct
17 Correct 72 ms 11960 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 8440 KB Output is correct
2 Correct 224 ms 12280 KB Output is correct
3 Correct 358 ms 12688 KB Output is correct
4 Correct 305 ms 13196 KB Output is correct
5 Correct 463 ms 14988 KB Output is correct
6 Correct 451 ms 15204 KB Output is correct
7 Correct 449 ms 15452 KB Output is correct
8 Correct 441 ms 15372 KB Output is correct
9 Correct 452 ms 15172 KB Output is correct
10 Correct 451 ms 15176 KB Output is correct
11 Correct 442 ms 14988 KB Output is correct
12 Correct 463 ms 15264 KB Output is correct
13 Correct 443 ms 15452 KB Output is correct
14 Correct 493 ms 18016 KB Output is correct
15 Correct 511 ms 18264 KB Output is correct
16 Correct 446 ms 17192 KB Output is correct
17 Correct 479 ms 17252 KB Output is correct
18 Correct 449 ms 17232 KB Output is correct
19 Correct 281 ms 13792 KB Output is correct
20 Correct 279 ms 13652 KB Output is correct
21 Correct 287 ms 13648 KB Output is correct
22 Correct 276 ms 13908 KB Output is correct
23 Correct 307 ms 13916 KB Output is correct
24 Correct 279 ms 13880 KB Output is correct
25 Correct 312 ms 13956 KB Output is correct
26 Correct 281 ms 13888 KB Output is correct
27 Correct 284 ms 13916 KB Output is correct
28 Correct 281 ms 13736 KB Output is correct
29 Correct 315 ms 13956 KB Output is correct
30 Correct 287 ms 13908 KB Output is correct
31 Correct 290 ms 14160 KB Output is correct
32 Correct 297 ms 14960 KB Output is correct
33 Correct 356 ms 15544 KB Output is correct
34 Correct 330 ms 15184 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 8280 KB Output is correct
2 Correct 3 ms 8280 KB Output is correct
3 Correct 4 ms 8284 KB Output is correct
4 Correct 622 ms 15148 KB Output is correct
5 Correct 680 ms 17304 KB Output is correct
6 Correct 757 ms 19796 KB Output is correct
7 Correct 757 ms 20976 KB Output is correct
8 Correct 759 ms 21188 KB Output is correct
9 Correct 779 ms 20948 KB Output is correct
10 Correct 771 ms 20816 KB Output is correct
11 Correct 771 ms 20968 KB Output is correct
12 Correct 759 ms 20820 KB Output is correct
13 Correct 766 ms 20820 KB Output is correct
14 Correct 71 ms 12112 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 8280 KB Output is correct
2 Correct 3 ms 8284 KB Output is correct
3 Correct 3 ms 8284 KB Output is correct
4 Correct 4 ms 8284 KB Output is correct
5 Correct 4 ms 8480 KB Output is correct
6 Correct 3 ms 8284 KB Output is correct
7 Correct 3 ms 8284 KB Output is correct
8 Correct 3 ms 8284 KB Output is correct
9 Correct 4 ms 8284 KB Output is correct
10 Correct 3 ms 8284 KB Output is correct
11 Correct 3 ms 8284 KB Output is correct
12 Correct 3 ms 8284 KB Output is correct
13 Correct 3 ms 8284 KB Output is correct
14 Correct 3 ms 8284 KB Output is correct
15 Correct 3 ms 8536 KB Output is correct
16 Correct 3 ms 8284 KB Output is correct
17 Correct 3 ms 8472 KB Output is correct
18 Correct 3 ms 8512 KB Output is correct
19 Correct 3 ms 8292 KB Output is correct
20 Correct 3 ms 8284 KB Output is correct
21 Correct 3 ms 8284 KB Output is correct
22 Correct 3 ms 8436 KB Output is correct
23 Correct 3 ms 8280 KB Output is correct
24 Correct 3 ms 8284 KB Output is correct
25 Correct 3 ms 8284 KB Output is correct
26 Correct 3 ms 8284 KB Output is correct
27 Correct 3 ms 8280 KB Output is correct
28 Correct 3 ms 8284 KB Output is correct
29 Correct 4 ms 8280 KB Output is correct
30 Correct 7 ms 8540 KB Output is correct
31 Correct 7 ms 8540 KB Output is correct
32 Correct 10 ms 8540 KB Output is correct
33 Correct 8 ms 8540 KB Output is correct
34 Correct 8 ms 8540 KB Output is correct
35 Correct 8 ms 8540 KB Output is correct
36 Correct 9 ms 8540 KB Output is correct
37 Correct 9 ms 8480 KB Output is correct
38 Correct 6 ms 8540 KB Output is correct
39 Correct 6 ms 8536 KB Output is correct
40 Correct 6 ms 8540 KB Output is correct
41 Correct 7 ms 8536 KB Output is correct
42 Correct 5 ms 8540 KB Output is correct
43 Correct 5 ms 8540 KB Output is correct
44 Correct 6 ms 8536 KB Output is correct
45 Correct 7 ms 8540 KB Output is correct
46 Correct 6 ms 8536 KB Output is correct
47 Correct 6 ms 8540 KB Output is correct
48 Correct 6 ms 8540 KB Output is correct
49 Correct 7 ms 8540 KB Output is correct
50 Correct 6 ms 8540 KB Output is correct
51 Correct 7 ms 8540 KB Output is correct
52 Correct 7 ms 8540 KB Output is correct
53 Correct 7 ms 8480 KB Output is correct
54 Correct 6 ms 8540 KB Output is correct
55 Correct 8 ms 8540 KB Output is correct
56 Correct 4 ms 8284 KB Output is correct
57 Correct 5 ms 8540 KB Output is correct
58 Correct 11 ms 8536 KB Output is correct
59 Correct 3 ms 8280 KB Output is correct
60 Correct 3 ms 8284 KB Output is correct
61 Correct 4 ms 8540 KB Output is correct
62 Correct 120 ms 16688 KB Output is correct
63 Correct 114 ms 18920 KB Output is correct
64 Correct 122 ms 20188 KB Output is correct
65 Correct 160 ms 22084 KB Output is correct
66 Correct 178 ms 22324 KB Output is correct
67 Correct 159 ms 22096 KB Output is correct
68 Correct 160 ms 22096 KB Output is correct
69 Correct 177 ms 22308 KB Output is correct
70 Correct 154 ms 21792 KB Output is correct
71 Correct 150 ms 21628 KB Output is correct
72 Correct 156 ms 21712 KB Output is correct
73 Correct 82 ms 21024 KB Output is correct
74 Correct 150 ms 22612 KB Output is correct
75 Correct 72 ms 11960 KB Output is correct
76 Correct 3 ms 8440 KB Output is correct
77 Correct 224 ms 12280 KB Output is correct
78 Correct 358 ms 12688 KB Output is correct
79 Correct 305 ms 13196 KB Output is correct
80 Correct 463 ms 14988 KB Output is correct
81 Correct 451 ms 15204 KB Output is correct
82 Correct 449 ms 15452 KB Output is correct
83 Correct 441 ms 15372 KB Output is correct
84 Correct 452 ms 15172 KB Output is correct
85 Correct 451 ms 15176 KB Output is correct
86 Correct 442 ms 14988 KB Output is correct
87 Correct 463 ms 15264 KB Output is correct
88 Correct 443 ms 15452 KB Output is correct
89 Correct 493 ms 18016 KB Output is correct
90 Correct 511 ms 18264 KB Output is correct
91 Correct 446 ms 17192 KB Output is correct
92 Correct 479 ms 17252 KB Output is correct
93 Correct 449 ms 17232 KB Output is correct
94 Correct 281 ms 13792 KB Output is correct
95 Correct 279 ms 13652 KB Output is correct
96 Correct 287 ms 13648 KB Output is correct
97 Correct 276 ms 13908 KB Output is correct
98 Correct 307 ms 13916 KB Output is correct
99 Correct 279 ms 13880 KB Output is correct
100 Correct 312 ms 13956 KB Output is correct
101 Correct 281 ms 13888 KB Output is correct
102 Correct 284 ms 13916 KB Output is correct
103 Correct 281 ms 13736 KB Output is correct
104 Correct 315 ms 13956 KB Output is correct
105 Correct 287 ms 13908 KB Output is correct
106 Correct 290 ms 14160 KB Output is correct
107 Correct 297 ms 14960 KB Output is correct
108 Correct 356 ms 15544 KB Output is correct
109 Correct 330 ms 15184 KB Output is correct
110 Correct 3 ms 8280 KB Output is correct
111 Correct 3 ms 8280 KB Output is correct
112 Correct 4 ms 8284 KB Output is correct
113 Correct 622 ms 15148 KB Output is correct
114 Correct 680 ms 17304 KB Output is correct
115 Correct 757 ms 19796 KB Output is correct
116 Correct 757 ms 20976 KB Output is correct
117 Correct 759 ms 21188 KB Output is correct
118 Correct 779 ms 20948 KB Output is correct
119 Correct 771 ms 20816 KB Output is correct
120 Correct 771 ms 20968 KB Output is correct
121 Correct 759 ms 20820 KB Output is correct
122 Correct 766 ms 20820 KB Output is correct
123 Correct 71 ms 12112 KB Output is correct
124 Correct 435 ms 20048 KB Output is correct
125 Correct 360 ms 18012 KB Output is correct
126 Correct 542 ms 20632 KB Output is correct
127 Correct 530 ms 20564 KB Output is correct
128 Correct 520 ms 20532 KB Output is correct
129 Correct 554 ms 20656 KB Output is correct
130 Correct 503 ms 20488 KB Output is correct
131 Correct 178 ms 23824 KB Output is correct
132 Correct 180 ms 24864 KB Output is correct
133 Correct 180 ms 21108 KB Output is correct
134 Correct 339 ms 17560 KB Output is correct
135 Correct 348 ms 17856 KB Output is correct
136 Correct 345 ms 17492 KB Output is correct
137 Correct 231 ms 21352 KB Output is correct
138 Correct 230 ms 21568 KB Output is correct
139 Correct 246 ms 21452 KB Output is correct
140 Correct 248 ms 21704 KB Output is correct
141 Correct 248 ms 21488 KB Output is correct
142 Correct 238 ms 21448 KB Output is correct
143 Correct 84 ms 13392 KB Output is correct
144 Correct 518 ms 18092 KB Output is correct