답안 #1101629

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1101629 2024-10-16T12:01:24 Z Pacybwoah Tourism (JOI23_tourism) C++17
100 / 100
675 ms 39368 KB
#include<iostream>
#include<vector>
#include<set>
#include<utility>
#include<cassert>
#include<algorithm>
using namespace std;
int n, m, q;
vector<vector<int>> graph, anc;
vector<int> vec, in, out, ord, head, sz, bit, dep;
int cnt = 0;
void dfs(int node, int parent){
    sz[node] = 1;
    anc[0][node] = parent;
    head[node] = node;
    dep[node] = dep[parent] + 1;
    int maxi = 0, pos = -1;
    for(auto &x: graph[node]){
        if(x == parent) continue;
        dfs(x, node);
        sz[node] += sz[x];
        if(maxi < sz[x]){
            maxi = sz[x];
            pos = x;
        }
    }
    if(pos > 0){
        for(auto &x: graph[node]){
            if(x == pos) swap(graph[node][0], x);
        }
        head[pos] = node;
    }
}
void dfs_head(int node, int parent){
    ord[++cnt] = node;
    in[node] = cnt;
    if(head[node] != node) head[node] = head[parent];
    for(auto &x: graph[node]){
        if(x != parent) dfs_head(x, node);
    }
    out[node] = cnt;
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin >> n >> m >> q;
    graph.resize(n + 1);
    vec.resize(m + 1);
    in.resize(n + 1);
    out.resize(n + 1);
    ord.resize(n + 1);
    head.resize(n + 1);
    sz.resize(n + 1);
    dep.resize(n + 1);
    for(int i = 1; i < n; i++){
        int a, b;
        cin >> a >> b;
        graph[a].push_back(b);
        graph[b].push_back(a);
    }
    anc.resize(18, vector<int>(n + 1));
    dfs(1, 1);
    dfs_head(1, 1);
    for(int i = 1; i < 18; i++) for(int j = 1; j <= n; j++) anc[i][j] = anc[i - 1][anc[i - 1][j]];
    auto lca = [&](int a, int b){
        if(dep[a] < dep[b]) swap(a, b);
        for(int i = 0; i < 18; i++){
            if((dep[a] - dep[b]) & (1 << i)) a = anc[i][a];
        }
        for(int i = 17; i >= 0; i--){
            if(anc[i][a] != anc[i][b]){
                a = anc[i][a];
                b = anc[i][b];
            }
        }
        if(a != b) return anc[0][a];
        else return a;
    };
    bit.resize(m + 1);
    for(int i = 1; i <= m; i++) cin >> vec[i];
    auto modify = [&](int pos, int val){
        while(pos <= m){
            bit[pos] += val;
            pos += (pos & -pos);
        }
    };
    auto query = [&](int pos){
        int ans = 0;
        while(pos > 0){
            ans += bit[pos];
            pos -= (pos & -pos);
        }
        return ans;
    };
    vector<vector<pair<int, int>>> sweep(m + 1);
    vector<int> ans(q);
    for(int i = 0; i < q; i++){
        int l, r;
        cin >> l >> r;
        sweep[r].emplace_back(l, i);
    }
    vector<vector<int>> st(18, vector<int>(m));
    for(int i = 1; i < m; i++) st[0][i] = lca(vec[i], vec[i + 1]);
    for(int i = 1; i < 18; i++){
        for(int j = 1; j < m; j++) st[i][j] = lca(st[i - 1][j], st[i - 1][min(m - 1, j + (1 << (i - 1)))]);
    }
    vector<int> bases(m);
    for(int i = 2; i < m; i++){
        bases[i] = bases[i - 1];
        if((1 << (bases[i] + 1)) == i) bases[i]++;
    }
    auto qlca = [&](int l, int r){
        r--;
        if(l > r) return vec[l];
        int len = r - l + 1;
        return lca(st[bases[len]][l], st[bases[len]][r - (1 << bases[len]) + 1]);
    };
    set<pair<pair<int, int>, int>> s;
    auto assign = [&](int l, int r, int col){
        //cout << l << " " << r << " " << col << "\n";
        while(!s.empty()){
            auto iter = s.lower_bound(make_pair(make_pair(l, 0), 0));
            if(iter == s.end()) break;
            auto [rng, scol] = (*iter);
            if(rng.first > r) break;
            s.erase(iter);
            modify(scol, -(rng.second - rng.first + 1));
            if(rng.second > r){
                modify(scol, rng.second - r);
                s.insert(make_pair(make_pair(r + 1, rng.second), scol));
            }
        }
        auto iter2 = s.lower_bound(make_pair(make_pair(l, 0), 0));
        if(iter2 != s.begin()){
            iter2 = prev(iter2);
            if((*iter2).first.second >= l){
                auto [rng2, col2] = (*iter2);
                s.erase(iter2);
                modify(col2, -(rng2.second - l + 1));
                s.insert(make_pair(make_pair(rng2.first, l - 1), col2));
                if(r < rng2.second){
                    modify(col2, rng2.second - r);
                    s.insert(make_pair(make_pair(r + 1, rng2.second), col2));
                }
            }
        }
        s.insert(make_pair(make_pair(l, r), col));
        modify(col, r - l + 1);
    };
    for(int i = 1; i <= m; i++){
        int cur = vec[i];
        while(1){
            assign(in[head[cur]], in[cur], i);
            if(head[cur] == 1) break;
            cur = anc[0][head[cur]];
        }
        for(auto &[l, id]: sweep[i]){
            ans[id] += query(i) - query(l - 1);
            ans[id] -= dep[qlca(l, i)];
        }
    }
    for(int i = 0; i < q; i++){
        cout << ans[i] + 1 << "\n";
    }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 2 ms 336 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Correct 1 ms 336 KB Output is correct
10 Correct 1 ms 508 KB Output is correct
11 Correct 1 ms 336 KB Output is correct
12 Correct 1 ms 336 KB Output is correct
13 Correct 1 ms 336 KB Output is correct
14 Correct 1 ms 336 KB Output is correct
15 Correct 1 ms 336 KB Output is correct
16 Correct 1 ms 336 KB Output is correct
17 Correct 1 ms 336 KB Output is correct
18 Correct 1 ms 336 KB Output is correct
19 Correct 2 ms 336 KB Output is correct
20 Correct 1 ms 336 KB Output is correct
21 Correct 1 ms 336 KB Output is correct
22 Correct 1 ms 336 KB Output is correct
23 Correct 1 ms 336 KB Output is correct
24 Correct 1 ms 336 KB Output is correct
25 Correct 1 ms 336 KB Output is correct
26 Correct 1 ms 336 KB Output is correct
27 Correct 1 ms 336 KB Output is correct
28 Correct 1 ms 336 KB Output is correct
29 Correct 1 ms 336 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 2 ms 336 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Correct 1 ms 336 KB Output is correct
10 Correct 1 ms 508 KB Output is correct
11 Correct 1 ms 336 KB Output is correct
12 Correct 1 ms 336 KB Output is correct
13 Correct 1 ms 336 KB Output is correct
14 Correct 1 ms 336 KB Output is correct
15 Correct 1 ms 336 KB Output is correct
16 Correct 1 ms 336 KB Output is correct
17 Correct 1 ms 336 KB Output is correct
18 Correct 1 ms 336 KB Output is correct
19 Correct 2 ms 336 KB Output is correct
20 Correct 1 ms 336 KB Output is correct
21 Correct 1 ms 336 KB Output is correct
22 Correct 1 ms 336 KB Output is correct
23 Correct 1 ms 336 KB Output is correct
24 Correct 1 ms 336 KB Output is correct
25 Correct 1 ms 336 KB Output is correct
26 Correct 1 ms 336 KB Output is correct
27 Correct 1 ms 336 KB Output is correct
28 Correct 1 ms 336 KB Output is correct
29 Correct 1 ms 336 KB Output is correct
30 Correct 6 ms 1020 KB Output is correct
31 Correct 5 ms 832 KB Output is correct
32 Correct 6 ms 848 KB Output is correct
33 Correct 6 ms 1076 KB Output is correct
34 Correct 6 ms 1024 KB Output is correct
35 Correct 6 ms 848 KB Output is correct
36 Correct 6 ms 1068 KB Output is correct
37 Correct 6 ms 848 KB Output is correct
38 Correct 5 ms 1272 KB Output is correct
39 Correct 5 ms 1104 KB Output is correct
40 Correct 5 ms 1272 KB Output is correct
41 Correct 4 ms 1104 KB Output is correct
42 Correct 4 ms 1104 KB Output is correct
43 Correct 4 ms 1104 KB Output is correct
44 Correct 5 ms 848 KB Output is correct
45 Correct 5 ms 848 KB Output is correct
46 Correct 5 ms 848 KB Output is correct
47 Correct 5 ms 848 KB Output is correct
48 Correct 8 ms 848 KB Output is correct
49 Correct 5 ms 848 KB Output is correct
50 Correct 5 ms 1104 KB Output is correct
51 Correct 4 ms 1104 KB Output is correct
52 Correct 5 ms 1272 KB Output is correct
53 Correct 5 ms 1120 KB Output is correct
54 Correct 5 ms 1104 KB Output is correct
55 Correct 5 ms 1104 KB Output is correct
56 Correct 3 ms 592 KB Output is correct
57 Correct 2 ms 592 KB Output is correct
58 Correct 6 ms 848 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 3 ms 592 KB Output is correct
4 Correct 279 ms 24816 KB Output is correct
5 Correct 226 ms 26636 KB Output is correct
6 Correct 281 ms 31720 KB Output is correct
7 Correct 382 ms 36544 KB Output is correct
8 Correct 373 ms 36800 KB Output is correct
9 Correct 382 ms 36572 KB Output is correct
10 Correct 363 ms 36556 KB Output is correct
11 Correct 366 ms 36636 KB Output is correct
12 Correct 272 ms 35876 KB Output is correct
13 Correct 285 ms 35996 KB Output is correct
14 Correct 277 ms 36080 KB Output is correct
15 Correct 51 ms 25024 KB Output is correct
16 Correct 348 ms 36436 KB Output is correct
17 Correct 136 ms 13544 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 238 ms 16496 KB Output is correct
3 Correct 389 ms 20992 KB Output is correct
4 Correct 283 ms 20028 KB Output is correct
5 Correct 472 ms 29204 KB Output is correct
6 Correct 460 ms 29180 KB Output is correct
7 Correct 475 ms 29372 KB Output is correct
8 Correct 446 ms 29180 KB Output is correct
9 Correct 471 ms 29120 KB Output is correct
10 Correct 472 ms 29304 KB Output is correct
11 Correct 456 ms 29264 KB Output is correct
12 Correct 484 ms 29472 KB Output is correct
13 Correct 465 ms 29532 KB Output is correct
14 Correct 467 ms 30400 KB Output is correct
15 Correct 483 ms 32956 KB Output is correct
16 Correct 459 ms 29648 KB Output is correct
17 Correct 468 ms 29632 KB Output is correct
18 Correct 476 ms 29632 KB Output is correct
19 Correct 367 ms 26304 KB Output is correct
20 Correct 364 ms 27848 KB Output is correct
21 Correct 378 ms 27844 KB Output is correct
22 Correct 353 ms 27976 KB Output is correct
23 Correct 356 ms 27844 KB Output is correct
24 Correct 383 ms 27844 KB Output is correct
25 Correct 365 ms 27844 KB Output is correct
26 Correct 388 ms 27844 KB Output is correct
27 Correct 355 ms 27944 KB Output is correct
28 Correct 367 ms 27844 KB Output is correct
29 Correct 377 ms 28100 KB Output is correct
30 Correct 380 ms 28356 KB Output is correct
31 Correct 388 ms 28356 KB Output is correct
32 Correct 404 ms 28876 KB Output is correct
33 Correct 405 ms 30320 KB Output is correct
34 Correct 417 ms 32964 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 4 ms 592 KB Output is correct
4 Correct 519 ms 23564 KB Output is correct
5 Correct 561 ms 23980 KB Output is correct
6 Correct 650 ms 29620 KB Output is correct
7 Correct 663 ms 32556 KB Output is correct
8 Correct 668 ms 32544 KB Output is correct
9 Correct 675 ms 32540 KB Output is correct
10 Correct 643 ms 32588 KB Output is correct
11 Correct 627 ms 32704 KB Output is correct
12 Correct 645 ms 32568 KB Output is correct
13 Correct 660 ms 32704 KB Output is correct
14 Correct 133 ms 13504 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 2 ms 336 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Correct 1 ms 336 KB Output is correct
10 Correct 1 ms 508 KB Output is correct
11 Correct 1 ms 336 KB Output is correct
12 Correct 1 ms 336 KB Output is correct
13 Correct 1 ms 336 KB Output is correct
14 Correct 1 ms 336 KB Output is correct
15 Correct 1 ms 336 KB Output is correct
16 Correct 1 ms 336 KB Output is correct
17 Correct 1 ms 336 KB Output is correct
18 Correct 1 ms 336 KB Output is correct
19 Correct 2 ms 336 KB Output is correct
20 Correct 1 ms 336 KB Output is correct
21 Correct 1 ms 336 KB Output is correct
22 Correct 1 ms 336 KB Output is correct
23 Correct 1 ms 336 KB Output is correct
24 Correct 1 ms 336 KB Output is correct
25 Correct 1 ms 336 KB Output is correct
26 Correct 1 ms 336 KB Output is correct
27 Correct 1 ms 336 KB Output is correct
28 Correct 1 ms 336 KB Output is correct
29 Correct 1 ms 336 KB Output is correct
30 Correct 6 ms 1020 KB Output is correct
31 Correct 5 ms 832 KB Output is correct
32 Correct 6 ms 848 KB Output is correct
33 Correct 6 ms 1076 KB Output is correct
34 Correct 6 ms 1024 KB Output is correct
35 Correct 6 ms 848 KB Output is correct
36 Correct 6 ms 1068 KB Output is correct
37 Correct 6 ms 848 KB Output is correct
38 Correct 5 ms 1272 KB Output is correct
39 Correct 5 ms 1104 KB Output is correct
40 Correct 5 ms 1272 KB Output is correct
41 Correct 4 ms 1104 KB Output is correct
42 Correct 4 ms 1104 KB Output is correct
43 Correct 4 ms 1104 KB Output is correct
44 Correct 5 ms 848 KB Output is correct
45 Correct 5 ms 848 KB Output is correct
46 Correct 5 ms 848 KB Output is correct
47 Correct 5 ms 848 KB Output is correct
48 Correct 8 ms 848 KB Output is correct
49 Correct 5 ms 848 KB Output is correct
50 Correct 5 ms 1104 KB Output is correct
51 Correct 4 ms 1104 KB Output is correct
52 Correct 5 ms 1272 KB Output is correct
53 Correct 5 ms 1120 KB Output is correct
54 Correct 5 ms 1104 KB Output is correct
55 Correct 5 ms 1104 KB Output is correct
56 Correct 3 ms 592 KB Output is correct
57 Correct 2 ms 592 KB Output is correct
58 Correct 6 ms 848 KB Output is correct
59 Correct 1 ms 336 KB Output is correct
60 Correct 1 ms 336 KB Output is correct
61 Correct 3 ms 592 KB Output is correct
62 Correct 279 ms 24816 KB Output is correct
63 Correct 226 ms 26636 KB Output is correct
64 Correct 281 ms 31720 KB Output is correct
65 Correct 382 ms 36544 KB Output is correct
66 Correct 373 ms 36800 KB Output is correct
67 Correct 382 ms 36572 KB Output is correct
68 Correct 363 ms 36556 KB Output is correct
69 Correct 366 ms 36636 KB Output is correct
70 Correct 272 ms 35876 KB Output is correct
71 Correct 285 ms 35996 KB Output is correct
72 Correct 277 ms 36080 KB Output is correct
73 Correct 51 ms 25024 KB Output is correct
74 Correct 348 ms 36436 KB Output is correct
75 Correct 136 ms 13544 KB Output is correct
76 Correct 1 ms 336 KB Output is correct
77 Correct 238 ms 16496 KB Output is correct
78 Correct 389 ms 20992 KB Output is correct
79 Correct 283 ms 20028 KB Output is correct
80 Correct 472 ms 29204 KB Output is correct
81 Correct 460 ms 29180 KB Output is correct
82 Correct 475 ms 29372 KB Output is correct
83 Correct 446 ms 29180 KB Output is correct
84 Correct 471 ms 29120 KB Output is correct
85 Correct 472 ms 29304 KB Output is correct
86 Correct 456 ms 29264 KB Output is correct
87 Correct 484 ms 29472 KB Output is correct
88 Correct 465 ms 29532 KB Output is correct
89 Correct 467 ms 30400 KB Output is correct
90 Correct 483 ms 32956 KB Output is correct
91 Correct 459 ms 29648 KB Output is correct
92 Correct 468 ms 29632 KB Output is correct
93 Correct 476 ms 29632 KB Output is correct
94 Correct 367 ms 26304 KB Output is correct
95 Correct 364 ms 27848 KB Output is correct
96 Correct 378 ms 27844 KB Output is correct
97 Correct 353 ms 27976 KB Output is correct
98 Correct 356 ms 27844 KB Output is correct
99 Correct 383 ms 27844 KB Output is correct
100 Correct 365 ms 27844 KB Output is correct
101 Correct 388 ms 27844 KB Output is correct
102 Correct 355 ms 27944 KB Output is correct
103 Correct 367 ms 27844 KB Output is correct
104 Correct 377 ms 28100 KB Output is correct
105 Correct 380 ms 28356 KB Output is correct
106 Correct 388 ms 28356 KB Output is correct
107 Correct 404 ms 28876 KB Output is correct
108 Correct 405 ms 30320 KB Output is correct
109 Correct 417 ms 32964 KB Output is correct
110 Correct 1 ms 336 KB Output is correct
111 Correct 1 ms 336 KB Output is correct
112 Correct 4 ms 592 KB Output is correct
113 Correct 519 ms 23564 KB Output is correct
114 Correct 561 ms 23980 KB Output is correct
115 Correct 650 ms 29620 KB Output is correct
116 Correct 663 ms 32556 KB Output is correct
117 Correct 668 ms 32544 KB Output is correct
118 Correct 675 ms 32540 KB Output is correct
119 Correct 643 ms 32588 KB Output is correct
120 Correct 627 ms 32704 KB Output is correct
121 Correct 645 ms 32568 KB Output is correct
122 Correct 660 ms 32704 KB Output is correct
123 Correct 133 ms 13504 KB Output is correct
124 Correct 460 ms 32964 KB Output is correct
125 Correct 322 ms 28116 KB Output is correct
126 Correct 509 ms 35012 KB Output is correct
127 Correct 496 ms 35140 KB Output is correct
128 Correct 526 ms 35180 KB Output is correct
129 Correct 503 ms 35012 KB Output is correct
130 Correct 499 ms 35016 KB Output is correct
131 Correct 353 ms 38348 KB Output is correct
132 Correct 399 ms 39368 KB Output is correct
133 Correct 332 ms 35524 KB Output is correct
134 Correct 428 ms 32160 KB Output is correct
135 Correct 425 ms 32072 KB Output is correct
136 Correct 445 ms 32196 KB Output is correct
137 Correct 349 ms 36064 KB Output is correct
138 Correct 345 ms 35904 KB Output is correct
139 Correct 337 ms 35940 KB Output is correct
140 Correct 360 ms 35924 KB Output is correct
141 Correct 321 ms 35900 KB Output is correct
142 Correct 327 ms 35940 KB Output is correct
143 Correct 50 ms 18884 KB Output is correct
144 Correct 495 ms 34920 KB Output is correct