답안 #949348

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
949348 2024-03-19T06:43:39 Z vladilius Tourism (JOI23_tourism) C++17
100 / 100
822 ms 24980 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({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 344 KB Output is correct
2 Correct 0 ms 344 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 348 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 348 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 344 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 348 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 348 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Correct 1 ms 344 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 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 348 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 348 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 344 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 348 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 348 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Correct 1 ms 344 KB Output is correct
28 Correct 0 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 4 ms 604 KB Output is correct
32 Correct 6 ms 600 KB Output is correct
33 Correct 5 ms 604 KB Output is correct
34 Correct 5 ms 604 KB Output is correct
35 Correct 5 ms 648 KB Output is correct
36 Correct 6 ms 764 KB Output is correct
37 Correct 7 ms 604 KB Output is correct
38 Correct 2 ms 604 KB Output is correct
39 Correct 2 ms 604 KB Output is correct
40 Correct 2 ms 856 KB Output is correct
41 Correct 2 ms 860 KB Output is correct
42 Correct 2 ms 860 KB Output is correct
43 Correct 2 ms 604 KB Output is correct
44 Correct 3 ms 604 KB Output is correct
45 Correct 3 ms 604 KB Output is correct
46 Correct 3 ms 604 KB Output is correct
47 Correct 4 ms 604 KB Output is correct
48 Correct 3 ms 604 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 3 ms 860 KB Output is correct
53 Correct 3 ms 856 KB Output is correct
54 Correct 3 ms 604 KB Output is correct
55 Correct 3 ms 860 KB Output is correct
56 Correct 1 ms 604 KB Output is correct
57 Correct 1 ms 604 KB Output is correct
58 Correct 5 ms 604 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 1 ms 604 KB Output is correct
4 Correct 70 ms 13680 KB Output is correct
5 Correct 62 ms 17852 KB Output is correct
6 Correct 84 ms 20052 KB Output is correct
7 Correct 95 ms 22864 KB Output is correct
8 Correct 102 ms 22920 KB Output is correct
9 Correct 97 ms 22824 KB Output is correct
10 Correct 115 ms 22860 KB Output is correct
11 Correct 97 ms 22868 KB Output is correct
12 Correct 94 ms 22652 KB Output is correct
13 Correct 92 ms 22496 KB Output is correct
14 Correct 95 ms 22456 KB Output is correct
15 Correct 41 ms 19792 KB Output is correct
16 Correct 86 ms 20376 KB Output is correct
17 Correct 68 ms 12368 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 251 ms 8328 KB Output is correct
3 Correct 384 ms 10600 KB Output is correct
4 Correct 289 ms 10036 KB Output is correct
5 Correct 474 ms 14928 KB Output is correct
6 Correct 514 ms 14676 KB Output is correct
7 Correct 469 ms 14932 KB Output is correct
8 Correct 495 ms 15184 KB Output is correct
9 Correct 501 ms 14864 KB Output is correct
10 Correct 557 ms 14780 KB Output is correct
11 Correct 474 ms 14768 KB Output is correct
12 Correct 527 ms 14860 KB Output is correct
13 Correct 489 ms 15136 KB Output is correct
14 Correct 510 ms 15804 KB Output is correct
15 Correct 495 ms 15364 KB Output is correct
16 Correct 459 ms 15048 KB Output is correct
17 Correct 522 ms 15188 KB Output is correct
18 Correct 450 ms 15056 KB Output is correct
19 Correct 284 ms 11436 KB Output is correct
20 Correct 284 ms 11664 KB Output is correct
21 Correct 297 ms 11660 KB Output is correct
22 Correct 294 ms 11672 KB Output is correct
23 Correct 302 ms 11544 KB Output is correct
24 Correct 293 ms 11888 KB Output is correct
25 Correct 322 ms 11604 KB Output is correct
26 Correct 286 ms 11664 KB Output is correct
27 Correct 286 ms 11600 KB Output is correct
28 Correct 279 ms 11692 KB Output is correct
29 Correct 316 ms 11856 KB Output is correct
30 Correct 292 ms 11840 KB Output is correct
31 Correct 301 ms 12112 KB Output is correct
32 Correct 294 ms 12448 KB Output is correct
33 Correct 313 ms 13312 KB Output is correct
34 Correct 296 ms 12128 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
3 Correct 1 ms 600 KB Output is correct
4 Correct 657 ms 12752 KB Output is correct
5 Correct 721 ms 13036 KB Output is correct
6 Correct 794 ms 16408 KB Output is correct
7 Correct 816 ms 18004 KB Output is correct
8 Correct 794 ms 17988 KB Output is correct
9 Correct 822 ms 18208 KB Output is correct
10 Correct 793 ms 18024 KB Output is correct
11 Correct 816 ms 17976 KB Output is correct
12 Correct 820 ms 18320 KB Output is correct
13 Correct 801 ms 18052 KB Output is correct
14 Correct 72 ms 12368 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 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 348 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 348 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 344 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 348 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 348 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Correct 1 ms 344 KB Output is correct
28 Correct 0 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 4 ms 604 KB Output is correct
32 Correct 6 ms 600 KB Output is correct
33 Correct 5 ms 604 KB Output is correct
34 Correct 5 ms 604 KB Output is correct
35 Correct 5 ms 648 KB Output is correct
36 Correct 6 ms 764 KB Output is correct
37 Correct 7 ms 604 KB Output is correct
38 Correct 2 ms 604 KB Output is correct
39 Correct 2 ms 604 KB Output is correct
40 Correct 2 ms 856 KB Output is correct
41 Correct 2 ms 860 KB Output is correct
42 Correct 2 ms 860 KB Output is correct
43 Correct 2 ms 604 KB Output is correct
44 Correct 3 ms 604 KB Output is correct
45 Correct 3 ms 604 KB Output is correct
46 Correct 3 ms 604 KB Output is correct
47 Correct 4 ms 604 KB Output is correct
48 Correct 3 ms 604 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 3 ms 860 KB Output is correct
53 Correct 3 ms 856 KB Output is correct
54 Correct 3 ms 604 KB Output is correct
55 Correct 3 ms 860 KB Output is correct
56 Correct 1 ms 604 KB Output is correct
57 Correct 1 ms 604 KB Output is correct
58 Correct 5 ms 604 KB Output is correct
59 Correct 0 ms 348 KB Output is correct
60 Correct 0 ms 348 KB Output is correct
61 Correct 1 ms 604 KB Output is correct
62 Correct 70 ms 13680 KB Output is correct
63 Correct 62 ms 17852 KB Output is correct
64 Correct 84 ms 20052 KB Output is correct
65 Correct 95 ms 22864 KB Output is correct
66 Correct 102 ms 22920 KB Output is correct
67 Correct 97 ms 22824 KB Output is correct
68 Correct 115 ms 22860 KB Output is correct
69 Correct 97 ms 22868 KB Output is correct
70 Correct 94 ms 22652 KB Output is correct
71 Correct 92 ms 22496 KB Output is correct
72 Correct 95 ms 22456 KB Output is correct
73 Correct 41 ms 19792 KB Output is correct
74 Correct 86 ms 20376 KB Output is correct
75 Correct 68 ms 12368 KB Output is correct
76 Correct 0 ms 348 KB Output is correct
77 Correct 251 ms 8328 KB Output is correct
78 Correct 384 ms 10600 KB Output is correct
79 Correct 289 ms 10036 KB Output is correct
80 Correct 474 ms 14928 KB Output is correct
81 Correct 514 ms 14676 KB Output is correct
82 Correct 469 ms 14932 KB Output is correct
83 Correct 495 ms 15184 KB Output is correct
84 Correct 501 ms 14864 KB Output is correct
85 Correct 557 ms 14780 KB Output is correct
86 Correct 474 ms 14768 KB Output is correct
87 Correct 527 ms 14860 KB Output is correct
88 Correct 489 ms 15136 KB Output is correct
89 Correct 510 ms 15804 KB Output is correct
90 Correct 495 ms 15364 KB Output is correct
91 Correct 459 ms 15048 KB Output is correct
92 Correct 522 ms 15188 KB Output is correct
93 Correct 450 ms 15056 KB Output is correct
94 Correct 284 ms 11436 KB Output is correct
95 Correct 284 ms 11664 KB Output is correct
96 Correct 297 ms 11660 KB Output is correct
97 Correct 294 ms 11672 KB Output is correct
98 Correct 302 ms 11544 KB Output is correct
99 Correct 293 ms 11888 KB Output is correct
100 Correct 322 ms 11604 KB Output is correct
101 Correct 286 ms 11664 KB Output is correct
102 Correct 286 ms 11600 KB Output is correct
103 Correct 279 ms 11692 KB Output is correct
104 Correct 316 ms 11856 KB Output is correct
105 Correct 292 ms 11840 KB Output is correct
106 Correct 301 ms 12112 KB Output is correct
107 Correct 294 ms 12448 KB Output is correct
108 Correct 313 ms 13312 KB Output is correct
109 Correct 296 ms 12128 KB Output is correct
110 Correct 0 ms 344 KB Output is correct
111 Correct 1 ms 344 KB Output is correct
112 Correct 1 ms 600 KB Output is correct
113 Correct 657 ms 12752 KB Output is correct
114 Correct 721 ms 13036 KB Output is correct
115 Correct 794 ms 16408 KB Output is correct
116 Correct 816 ms 18004 KB Output is correct
117 Correct 794 ms 17988 KB Output is correct
118 Correct 822 ms 18208 KB Output is correct
119 Correct 793 ms 18024 KB Output is correct
120 Correct 816 ms 17976 KB Output is correct
121 Correct 820 ms 18320 KB Output is correct
122 Correct 801 ms 18052 KB Output is correct
123 Correct 72 ms 12368 KB Output is correct
124 Correct 418 ms 16844 KB Output is correct
125 Correct 310 ms 14368 KB Output is correct
126 Correct 509 ms 17596 KB Output is correct
127 Correct 526 ms 17972 KB Output is correct
128 Correct 505 ms 17744 KB Output is correct
129 Correct 522 ms 17840 KB Output is correct
130 Correct 513 ms 17844 KB Output is correct
131 Correct 110 ms 21180 KB Output is correct
132 Correct 108 ms 22684 KB Output is correct
133 Correct 119 ms 17404 KB Output is correct
134 Correct 333 ms 14476 KB Output is correct
135 Correct 316 ms 14420 KB Output is correct
136 Correct 313 ms 14660 KB Output is correct
137 Correct 210 ms 21192 KB Output is correct
138 Correct 201 ms 24776 KB Output is correct
139 Correct 230 ms 24980 KB Output is correct
140 Correct 191 ms 19960 KB Output is correct
141 Correct 216 ms 23664 KB Output is correct
142 Correct 204 ms 20652 KB Output is correct
143 Correct 43 ms 8800 KB Output is correct
144 Correct 485 ms 15232 KB Output is correct