답안 #949347

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
949347 2024-03-19T06:42:23 Z vladilius Tourism (JOI23_tourism) C++17
100 / 100
941 ms 27832 KB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;

struct FT{
    vector<int> bit;
    int n;
    FT(int ns){
        n = ns;
        bit.resize(n + 1);
    }
    void upd(int v, int k){
        while (v <= n){
            bit[v] += k;
            v |= (v + 1);
        }
    }
    int get(int v){
        int out = 0;
        while (v > 0){
            out += bit[v];
            v = (v & (v + 1)) - 1;
        }
        return out;
    }
};

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    
    int n, m, q; cin>>n>>m>>q;
    vector<int> g[n + 1];
    for (int i = 1; i < n; i++){
        int a, b; cin>>a>>b;
        g[a].push_back(b);
        g[b].push_back(a);
    }
    vector<int> c(m + 1);
    for (int i = 1; i <= m; i++){
        cin>>c[i];
    }
    
    vector<int> pos(n + 1), sz(n + 1), p(n + 1), heavy(n + 1), head(n + 1), d(n + 1);
    function<void(int, int)> dfs = [&](int v, int pr){
        p[v] = pr; sz[v] = 1;
        for (int i: g[v]){
            if (i == pr) continue;
            d[i] = d[v] + 1;
            dfs(i, v);
            if (sz[i] > sz[heavy[v]]){
                heavy[v] = i;
            }
            sz[v] += sz[i];
        }
    };
    dfs(1, 0);
    int timer = 0;
    function<void(int, int, int)> dfs_hld = [&](int v, int pr, int k){
        pos[v] = ++timer;
        head[v] = k;
        if (!heavy[v]) return;
        dfs_hld(heavy[v], v, k);
        for (int i: g[v]){
            if (i == pr || i == heavy[v]){
                continue;
            }
            dfs_hld(i, v, i);
        }
    };
    dfs_hld(1, 0, 1);
    
    vector<pii> end[m + 1];
    vector<int> out(q + 1);
    for (int i = 1; i <= q; i++){
        int l, r; cin>>l>>r;
        if (l == r){
            out[i] = 1;
            continue;
        }
        end[l].push_back({r, i});
    }
    
    FT T(m);
    set<array<int, 3>> st;
    auto set = [&](int l, int r, int x){
        while (!st.empty()){
            auto it = st.lower_bound({l, 0, 0});
            if (it == st.end() || (*it)[0] > r) break;
            auto [a, b, k] = *it;
            if (b <= r){
                T.upd(k, -(b - a + 1));
                st.erase(it);
            }
            else {
                T.upd(k, -(r - a + 1));
                st.erase(it);
                st.insert({r + 1, b, k});
            }
        }
        while (!st.empty()){
            auto it = st.upper_bound({l, 0, 0}); 
            if (it == st.begin()) break;
            it--;
            if ((*it)[1] < l) break;
            auto [a, b, k] = *it;
            if (b < r){
                T.upd(k, -(b - l + 1));
                st.erase(it);
                if (a < l) st.insert({a, l - 1, k});
            }
            else {
                T.upd(k, -(r - l + 1));
                st.erase(it);
                if (a < l) st.insert({a, l - 1, k});
                if (r < b) st.insert({r + 1, b});
            }
        }
        T.upd(x, (r - l + 1));
        st.insert({0, 0, 0});
        st.insert({l, r, x});
    };
    auto add = [&](int u, int v, int x){
        while (head[u] != head[v]){
            if (d[head[u]] > d[head[v]]){
                swap(u, v);
            }
            set(pos[head[v]], pos[v], x);
            v = p[head[v]];
        }
        if (d[u] > d[v]) swap(u, v);
        set(pos[u] + 1, pos[v], x);
    };
    
    for (int l = m; l > 0; l--){
        if (l != m){
            add(c[l], c[l + 1], l);
        }
        for (auto [r, j]: end[l]){
            out[j] = T.get(r - 1) + 1;
        }
    }
    for (int i = 1; i <= q; i++){
        cout<<out[i]<<"\n";
    }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 600 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 344 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 1 ms 508 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 1 ms 456 KB Output is correct
26 Correct 1 ms 344 KB Output is correct
27 Correct 1 ms 344 KB Output is correct
28 Correct 1 ms 348 KB Output is correct
29 Correct 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 600 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 344 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 1 ms 508 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 1 ms 456 KB Output is correct
26 Correct 1 ms 344 KB Output is correct
27 Correct 1 ms 344 KB Output is correct
28 Correct 1 ms 348 KB Output is correct
29 Correct 1 ms 348 KB Output is correct
30 Correct 4 ms 604 KB Output is correct
31 Correct 5 ms 856 KB Output is correct
32 Correct 7 ms 604 KB Output is correct
33 Correct 6 ms 604 KB Output is correct
34 Correct 6 ms 616 KB Output is correct
35 Correct 6 ms 604 KB Output is correct
36 Correct 7 ms 604 KB Output is correct
37 Correct 5 ms 600 KB Output is correct
38 Correct 2 ms 856 KB Output is correct
39 Correct 2 ms 860 KB Output is correct
40 Correct 2 ms 860 KB Output is correct
41 Correct 2 ms 724 KB Output is correct
42 Correct 2 ms 860 KB Output is correct
43 Correct 3 ms 604 KB Output is correct
44 Correct 3 ms 604 KB Output is correct
45 Correct 4 ms 604 KB Output is correct
46 Correct 3 ms 604 KB Output is correct
47 Correct 3 ms 604 KB Output is correct
48 Correct 4 ms 600 KB Output is correct
49 Correct 3 ms 604 KB Output is correct
50 Correct 4 ms 860 KB Output is correct
51 Correct 3 ms 860 KB Output is correct
52 Correct 4 ms 860 KB Output is correct
53 Correct 4 ms 856 KB Output is correct
54 Correct 5 ms 856 KB Output is correct
55 Correct 4 ms 860 KB Output is correct
56 Correct 2 ms 604 KB Output is correct
57 Correct 1 ms 604 KB Output is correct
58 Correct 6 ms 604 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
3 Correct 2 ms 604 KB Output is correct
4 Correct 74 ms 13716 KB Output is correct
5 Correct 66 ms 19792 KB Output is correct
6 Correct 80 ms 22228 KB Output is correct
7 Correct 105 ms 25684 KB Output is correct
8 Correct 107 ms 25792 KB Output is correct
9 Correct 99 ms 25788 KB Output is correct
10 Correct 100 ms 25796 KB Output is correct
11 Correct 110 ms 25788 KB Output is correct
12 Correct 103 ms 25176 KB Output is correct
13 Correct 105 ms 25172 KB Output is correct
14 Correct 113 ms 25172 KB Output is correct
15 Correct 51 ms 21532 KB Output is correct
16 Correct 89 ms 23376 KB Output is correct
17 Correct 84 ms 13592 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 266 ms 8396 KB Output is correct
3 Correct 410 ms 11344 KB Output is correct
4 Correct 321 ms 11528 KB Output is correct
5 Correct 505 ms 16428 KB Output is correct
6 Correct 521 ms 16608 KB Output is correct
7 Correct 512 ms 16756 KB Output is correct
8 Correct 497 ms 16524 KB Output is correct
9 Correct 534 ms 16388 KB Output is correct
10 Correct 544 ms 16400 KB Output is correct
11 Correct 503 ms 16460 KB Output is correct
12 Correct 565 ms 16656 KB Output is correct
13 Correct 508 ms 17068 KB Output is correct
14 Correct 578 ms 17804 KB Output is correct
15 Correct 559 ms 18084 KB Output is correct
16 Correct 506 ms 16988 KB Output is correct
17 Correct 510 ms 16980 KB Output is correct
18 Correct 486 ms 16976 KB Output is correct
19 Correct 317 ms 13376 KB Output is correct
20 Correct 302 ms 13384 KB Output is correct
21 Correct 319 ms 13388 KB Output is correct
22 Correct 318 ms 13288 KB Output is correct
23 Correct 316 ms 13380 KB Output is correct
24 Correct 305 ms 13356 KB Output is correct
25 Correct 313 ms 13392 KB Output is correct
26 Correct 305 ms 13376 KB Output is correct
27 Correct 330 ms 13392 KB Output is correct
28 Correct 339 ms 13416 KB Output is correct
29 Correct 312 ms 13476 KB Output is correct
30 Correct 333 ms 13928 KB Output is correct
31 Correct 317 ms 13860 KB Output is correct
32 Correct 339 ms 14672 KB Output is correct
33 Correct 338 ms 15280 KB Output is correct
34 Correct 322 ms 14980 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 2 ms 604 KB Output is correct
4 Correct 761 ms 12948 KB Output is correct
5 Correct 768 ms 15444 KB Output is correct
6 Correct 842 ms 19240 KB Output is correct
7 Correct 938 ms 20852 KB Output is correct
8 Correct 895 ms 20960 KB Output is correct
9 Correct 871 ms 20988 KB Output is correct
10 Correct 882 ms 21080 KB Output is correct
11 Correct 872 ms 21032 KB Output is correct
12 Correct 941 ms 21164 KB Output is correct
13 Correct 874 ms 20868 KB Output is correct
14 Correct 106 ms 13668 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 600 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 344 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 1 ms 508 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 1 ms 456 KB Output is correct
26 Correct 1 ms 344 KB Output is correct
27 Correct 1 ms 344 KB Output is correct
28 Correct 1 ms 348 KB Output is correct
29 Correct 1 ms 348 KB Output is correct
30 Correct 4 ms 604 KB Output is correct
31 Correct 5 ms 856 KB Output is correct
32 Correct 7 ms 604 KB Output is correct
33 Correct 6 ms 604 KB Output is correct
34 Correct 6 ms 616 KB Output is correct
35 Correct 6 ms 604 KB Output is correct
36 Correct 7 ms 604 KB Output is correct
37 Correct 5 ms 600 KB Output is correct
38 Correct 2 ms 856 KB Output is correct
39 Correct 2 ms 860 KB Output is correct
40 Correct 2 ms 860 KB Output is correct
41 Correct 2 ms 724 KB Output is correct
42 Correct 2 ms 860 KB Output is correct
43 Correct 3 ms 604 KB Output is correct
44 Correct 3 ms 604 KB Output is correct
45 Correct 4 ms 604 KB Output is correct
46 Correct 3 ms 604 KB Output is correct
47 Correct 3 ms 604 KB Output is correct
48 Correct 4 ms 600 KB Output is correct
49 Correct 3 ms 604 KB Output is correct
50 Correct 4 ms 860 KB Output is correct
51 Correct 3 ms 860 KB Output is correct
52 Correct 4 ms 860 KB Output is correct
53 Correct 4 ms 856 KB Output is correct
54 Correct 5 ms 856 KB Output is correct
55 Correct 4 ms 860 KB Output is correct
56 Correct 2 ms 604 KB Output is correct
57 Correct 1 ms 604 KB Output is correct
58 Correct 6 ms 604 KB Output is correct
59 Correct 0 ms 348 KB Output is correct
60 Correct 1 ms 344 KB Output is correct
61 Correct 2 ms 604 KB Output is correct
62 Correct 74 ms 13716 KB Output is correct
63 Correct 66 ms 19792 KB Output is correct
64 Correct 80 ms 22228 KB Output is correct
65 Correct 105 ms 25684 KB Output is correct
66 Correct 107 ms 25792 KB Output is correct
67 Correct 99 ms 25788 KB Output is correct
68 Correct 100 ms 25796 KB Output is correct
69 Correct 110 ms 25788 KB Output is correct
70 Correct 103 ms 25176 KB Output is correct
71 Correct 105 ms 25172 KB Output is correct
72 Correct 113 ms 25172 KB Output is correct
73 Correct 51 ms 21532 KB Output is correct
74 Correct 89 ms 23376 KB Output is correct
75 Correct 84 ms 13592 KB Output is correct
76 Correct 0 ms 344 KB Output is correct
77 Correct 266 ms 8396 KB Output is correct
78 Correct 410 ms 11344 KB Output is correct
79 Correct 321 ms 11528 KB Output is correct
80 Correct 505 ms 16428 KB Output is correct
81 Correct 521 ms 16608 KB Output is correct
82 Correct 512 ms 16756 KB Output is correct
83 Correct 497 ms 16524 KB Output is correct
84 Correct 534 ms 16388 KB Output is correct
85 Correct 544 ms 16400 KB Output is correct
86 Correct 503 ms 16460 KB Output is correct
87 Correct 565 ms 16656 KB Output is correct
88 Correct 508 ms 17068 KB Output is correct
89 Correct 578 ms 17804 KB Output is correct
90 Correct 559 ms 18084 KB Output is correct
91 Correct 506 ms 16988 KB Output is correct
92 Correct 510 ms 16980 KB Output is correct
93 Correct 486 ms 16976 KB Output is correct
94 Correct 317 ms 13376 KB Output is correct
95 Correct 302 ms 13384 KB Output is correct
96 Correct 319 ms 13388 KB Output is correct
97 Correct 318 ms 13288 KB Output is correct
98 Correct 316 ms 13380 KB Output is correct
99 Correct 305 ms 13356 KB Output is correct
100 Correct 313 ms 13392 KB Output is correct
101 Correct 305 ms 13376 KB Output is correct
102 Correct 330 ms 13392 KB Output is correct
103 Correct 339 ms 13416 KB Output is correct
104 Correct 312 ms 13476 KB Output is correct
105 Correct 333 ms 13928 KB Output is correct
106 Correct 317 ms 13860 KB Output is correct
107 Correct 339 ms 14672 KB Output is correct
108 Correct 338 ms 15280 KB Output is correct
109 Correct 322 ms 14980 KB Output is correct
110 Correct 0 ms 348 KB Output is correct
111 Correct 0 ms 348 KB Output is correct
112 Correct 2 ms 604 KB Output is correct
113 Correct 761 ms 12948 KB Output is correct
114 Correct 768 ms 15444 KB Output is correct
115 Correct 842 ms 19240 KB Output is correct
116 Correct 938 ms 20852 KB Output is correct
117 Correct 895 ms 20960 KB Output is correct
118 Correct 871 ms 20988 KB Output is correct
119 Correct 882 ms 21080 KB Output is correct
120 Correct 872 ms 21032 KB Output is correct
121 Correct 941 ms 21164 KB Output is correct
122 Correct 874 ms 20868 KB Output is correct
123 Correct 106 ms 13668 KB Output is correct
124 Correct 455 ms 19660 KB Output is correct
125 Correct 343 ms 16756 KB Output is correct
126 Correct 566 ms 20668 KB Output is correct
127 Correct 550 ms 20564 KB Output is correct
128 Correct 574 ms 20664 KB Output is correct
129 Correct 562 ms 20704 KB Output is correct
130 Correct 540 ms 20496 KB Output is correct
131 Correct 110 ms 24144 KB Output is correct
132 Correct 107 ms 25696 KB Output is correct
133 Correct 113 ms 20304 KB Output is correct
134 Correct 325 ms 17360 KB Output is correct
135 Correct 357 ms 17432 KB Output is correct
136 Correct 335 ms 17488 KB Output is correct
137 Correct 242 ms 24268 KB Output is correct
138 Correct 263 ms 27832 KB Output is correct
139 Correct 242 ms 27680 KB Output is correct
140 Correct 229 ms 22984 KB Output is correct
141 Correct 261 ms 26568 KB Output is correct
142 Correct 226 ms 23780 KB Output is correct
143 Correct 41 ms 10304 KB Output is correct
144 Correct 526 ms 18264 KB Output is correct