답안 #898026

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
898026 2024-01-04T08:16:27 Z vjudge1 Tourism (JOI23_tourism) C++17
28 / 100
5000 ms 41304 KB
// MDSPro
// #pragma GCC optimize("Ofast")
// #pragma GCC optimize ("unroll-loops")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx")

#include "bits/stdc++.h"

using namespace std;

using ll = long long;
using ld = long double;

const ld PI = 3.141592653589793;
const int MOD = 1e9+7;
const int INF = 1e9;
const ll INFLL = 4e18;
const double EPS = 1e-9;
const int MAXN = 1000*1007;

template<typename T>
struct SparseTable {
    vector<vector<T>> sparse;
    function<T(const T&, const T&)> merge;
 
    SparseTable(const vector<T>& arr, const function<T(const T&, const T&)>& func) : merge(func) {
        int n = arr.size();
        int logn = 32 - __builtin_clz(n);
        sparse.resize(logn, vector<T>(n));
        sparse[0] = arr;
        for (int lg = 1; lg < logn; lg++) {
            for (int i = 0; i + (1 << lg) <= n; i++) {
                sparse[lg][i] = merge(sparse[lg - 1][i], sparse[lg - 1][i + (1 << (lg - 1))]);
            }
        }   
    }
 
    T get(int l, int r) { // [l, r)
        int cur_log = 31 - __builtin_clz(r - l);
        return merge(sparse[cur_log][l], sparse[cur_log][r - (1 << cur_log)]);
    }
};

void solve(int NT){
    int n, m, q; cin >> n >> m >> q;
    vector<vector<int>> g(n + 1); 
    for(int i = 1; i < n; ++i) {
        int x, y; cin >> x >> y;
        g[x].emplace_back(y);
        g[y].emplace_back(x);
    }

    int timer = 1;
    vector<pair<int, int>> euler_tour;
    vector<int> t_in(n + 1), dep(n + 1), t_out(n + 1), occ(n + 1);
    auto dfs = [&](auto dfs, int x, int d = 1) -> void {
        t_in[x] = ++timer;
        dep[x] = d;
        occ[x] = euler_tour.size();
        euler_tour.emplace_back(x, dep[x]);
        for(int z: g[x]) {
            if(dep[z] == 0) {
                dfs(dfs, z, d + 1);
                euler_tour.emplace_back(x, dep[x]);
            }
        }
        t_out[x] = timer;
    };

    auto upper = [&](int x, int y) -> bool {
        return t_in[x] <= t_in[y] && t_out[y] <= t_out[x];
    };

    

    dfs(dfs, 1);
    SparseTable<pair<int, int>> sp(euler_tour, [&](auto x, auto y) {
        if(x.second < y.second) return x;
        return y;
    });

    auto lca = [&](int x, int y) -> int {
        return sp.get(min(occ[x], occ[y]), max(occ[x], occ[y]) + 1).first;
    };

    vector<int> c(m);
    for(int i = 0; i < m; ++i) cin >> c[i];

    auto virt_tree = [&](int l, int r) -> int {
        vector<int> v;
        for(int i = l; i < r; ++i) v.emplace_back(c[i]);
        sort(v.begin(), v.end(), [&](int x, int y){return t_in[x] < t_in[y];});

        for(int i = 1; i < r - l; ++i) {
            int nvert = lca(v[i - 1], v[i]);
            v.emplace_back(nvert);
        }
        sort(v.begin(), v.end(), [&](int x, int y){return t_in[x] < t_in[y];});
        v.erase(unique(v.begin(), v.end()), v.end());

        int sz = v.size(), ans = 0;

        vector<int> st;
        st.emplace_back(v[0]);
        for(int i = 1; i < sz; ++i) {
            int x = v[i];
            while(st.size() >= 2 && !upper(st.back(), x)) {
                ans += dep[st.back()] - dep[st[st.size() - 2]];
                st.pop_back();
            }
            st.emplace_back(x);
        }

        while (st.size() >= 2) {
            ans += dep[st.back()] - dep[st[st.size() - 2]];
            st.pop_back();
        }

        return ans + 1;
    };

    while(q--) {
        int l, r; cin >> l >> r; l--;
        cout << virt_tree(l, r) << '\n';
    }
}

// #define TESTCASES
int main() {
    cin.tie(0)->sync_with_stdio(0);

    int t = 1;
    #ifdef TESTCASES
        cin >> t;
    #endif
    
    for(int i = 1; i <= t; ++i){
        solve(i);
        cout << "\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 3 ms 348 KB Output is correct
5 Correct 2 ms 348 KB Output is correct
6 Correct 2 ms 348 KB Output is correct
7 Correct 2 ms 348 KB Output is correct
8 Correct 2 ms 348 KB Output is correct
9 Correct 4 ms 552 KB Output is correct
10 Correct 4 ms 348 KB Output is correct
11 Correct 4 ms 348 KB Output is correct
12 Correct 11 ms 344 KB Output is correct
13 Correct 10 ms 348 KB Output is correct
14 Correct 12 ms 604 KB Output is correct
15 Correct 4 ms 348 KB Output is correct
16 Correct 4 ms 348 KB Output is correct
17 Correct 4 ms 348 KB Output is correct
18 Correct 4 ms 348 KB Output is correct
19 Correct 4 ms 348 KB Output is correct
20 Correct 4 ms 568 KB Output is correct
21 Correct 3 ms 348 KB Output is correct
22 Correct 3 ms 348 KB Output is correct
23 Correct 4 ms 348 KB Output is correct
24 Correct 3 ms 564 KB Output is correct
25 Correct 4 ms 344 KB Output is correct
26 Correct 3 ms 344 KB Output is correct
27 Correct 1 ms 344 KB Output is correct
28 Correct 1 ms 344 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 3 ms 348 KB Output is correct
5 Correct 2 ms 348 KB Output is correct
6 Correct 2 ms 348 KB Output is correct
7 Correct 2 ms 348 KB Output is correct
8 Correct 2 ms 348 KB Output is correct
9 Correct 4 ms 552 KB Output is correct
10 Correct 4 ms 348 KB Output is correct
11 Correct 4 ms 348 KB Output is correct
12 Correct 11 ms 344 KB Output is correct
13 Correct 10 ms 348 KB Output is correct
14 Correct 12 ms 604 KB Output is correct
15 Correct 4 ms 348 KB Output is correct
16 Correct 4 ms 348 KB Output is correct
17 Correct 4 ms 348 KB Output is correct
18 Correct 4 ms 348 KB Output is correct
19 Correct 4 ms 348 KB Output is correct
20 Correct 4 ms 568 KB Output is correct
21 Correct 3 ms 348 KB Output is correct
22 Correct 3 ms 348 KB Output is correct
23 Correct 4 ms 348 KB Output is correct
24 Correct 3 ms 564 KB Output is correct
25 Correct 4 ms 344 KB Output is correct
26 Correct 3 ms 344 KB Output is correct
27 Correct 1 ms 344 KB Output is correct
28 Correct 1 ms 344 KB Output is correct
29 Correct 1 ms 348 KB Output is correct
30 Correct 90 ms 856 KB Output is correct
31 Correct 136 ms 916 KB Output is correct
32 Correct 192 ms 1104 KB Output is correct
33 Correct 191 ms 1088 KB Output is correct
34 Correct 190 ms 1092 KB Output is correct
35 Correct 611 ms 1092 KB Output is correct
36 Correct 595 ms 1100 KB Output is correct
37 Correct 594 ms 1096 KB Output is correct
38 Correct 194 ms 1620 KB Output is correct
39 Correct 190 ms 1268 KB Output is correct
40 Correct 181 ms 1112 KB Output is correct
41 Correct 669 ms 1308 KB Output is correct
42 Correct 634 ms 1112 KB Output is correct
43 Correct 640 ms 1364 KB Output is correct
44 Correct 180 ms 1116 KB Output is correct
45 Correct 187 ms 1112 KB Output is correct
46 Correct 187 ms 1200 KB Output is correct
47 Correct 636 ms 1360 KB Output is correct
48 Correct 600 ms 1152 KB Output is correct
49 Correct 623 ms 1168 KB Output is correct
50 Correct 138 ms 1116 KB Output is correct
51 Correct 147 ms 1112 KB Output is correct
52 Correct 147 ms 1116 KB Output is correct
53 Correct 136 ms 1112 KB Output is correct
54 Correct 143 ms 1112 KB Output is correct
55 Correct 151 ms 1116 KB Output is correct
56 Correct 38 ms 508 KB Output is correct
57 Correct 2 ms 860 KB Output is correct
58 Correct 2 ms 860 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 37 ms 348 KB Output is correct
4 Execution timed out 5019 ms 30504 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 39 ms 21340 KB Output is correct
3 Correct 48 ms 23248 KB Output is correct
4 Correct 49 ms 27584 KB Output is correct
5 Correct 97 ms 41132 KB Output is correct
6 Correct 91 ms 40616 KB Output is correct
7 Correct 84 ms 40276 KB Output is correct
8 Correct 81 ms 40432 KB Output is correct
9 Correct 79 ms 40412 KB Output is correct
10 Correct 80 ms 40448 KB Output is correct
11 Correct 84 ms 40644 KB Output is correct
12 Correct 81 ms 40388 KB Output is correct
13 Correct 76 ms 40388 KB Output is correct
14 Correct 80 ms 40384 KB Output is correct
15 Correct 82 ms 40616 KB Output is correct
16 Correct 84 ms 40332 KB Output is correct
17 Correct 80 ms 40384 KB Output is correct
18 Correct 82 ms 40500 KB Output is correct
19 Correct 94 ms 41304 KB Output is correct
20 Correct 88 ms 40704 KB Output is correct
21 Correct 79 ms 40468 KB Output is correct
22 Correct 79 ms 40396 KB Output is correct
23 Correct 82 ms 40496 KB Output is correct
24 Correct 75 ms 40388 KB Output is correct
25 Correct 80 ms 40388 KB Output is correct
26 Correct 73 ms 40496 KB Output is correct
27 Correct 72 ms 40428 KB Output is correct
28 Correct 75 ms 40388 KB Output is correct
29 Correct 91 ms 40384 KB Output is correct
30 Correct 78 ms 40652 KB Output is correct
31 Correct 70 ms 40384 KB Output is correct
32 Correct 74 ms 40372 KB Output is correct
33 Correct 76 ms 40504 KB Output is correct
34 Correct 78 ms 40944 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 2 ms 348 KB Output is correct
3 Correct 37 ms 516 KB Output is correct
4 Execution timed out 5058 ms 23684 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 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 3 ms 348 KB Output is correct
5 Correct 2 ms 348 KB Output is correct
6 Correct 2 ms 348 KB Output is correct
7 Correct 2 ms 348 KB Output is correct
8 Correct 2 ms 348 KB Output is correct
9 Correct 4 ms 552 KB Output is correct
10 Correct 4 ms 348 KB Output is correct
11 Correct 4 ms 348 KB Output is correct
12 Correct 11 ms 344 KB Output is correct
13 Correct 10 ms 348 KB Output is correct
14 Correct 12 ms 604 KB Output is correct
15 Correct 4 ms 348 KB Output is correct
16 Correct 4 ms 348 KB Output is correct
17 Correct 4 ms 348 KB Output is correct
18 Correct 4 ms 348 KB Output is correct
19 Correct 4 ms 348 KB Output is correct
20 Correct 4 ms 568 KB Output is correct
21 Correct 3 ms 348 KB Output is correct
22 Correct 3 ms 348 KB Output is correct
23 Correct 4 ms 348 KB Output is correct
24 Correct 3 ms 564 KB Output is correct
25 Correct 4 ms 344 KB Output is correct
26 Correct 3 ms 344 KB Output is correct
27 Correct 1 ms 344 KB Output is correct
28 Correct 1 ms 344 KB Output is correct
29 Correct 1 ms 348 KB Output is correct
30 Correct 90 ms 856 KB Output is correct
31 Correct 136 ms 916 KB Output is correct
32 Correct 192 ms 1104 KB Output is correct
33 Correct 191 ms 1088 KB Output is correct
34 Correct 190 ms 1092 KB Output is correct
35 Correct 611 ms 1092 KB Output is correct
36 Correct 595 ms 1100 KB Output is correct
37 Correct 594 ms 1096 KB Output is correct
38 Correct 194 ms 1620 KB Output is correct
39 Correct 190 ms 1268 KB Output is correct
40 Correct 181 ms 1112 KB Output is correct
41 Correct 669 ms 1308 KB Output is correct
42 Correct 634 ms 1112 KB Output is correct
43 Correct 640 ms 1364 KB Output is correct
44 Correct 180 ms 1116 KB Output is correct
45 Correct 187 ms 1112 KB Output is correct
46 Correct 187 ms 1200 KB Output is correct
47 Correct 636 ms 1360 KB Output is correct
48 Correct 600 ms 1152 KB Output is correct
49 Correct 623 ms 1168 KB Output is correct
50 Correct 138 ms 1116 KB Output is correct
51 Correct 147 ms 1112 KB Output is correct
52 Correct 147 ms 1116 KB Output is correct
53 Correct 136 ms 1112 KB Output is correct
54 Correct 143 ms 1112 KB Output is correct
55 Correct 151 ms 1116 KB Output is correct
56 Correct 38 ms 508 KB Output is correct
57 Correct 2 ms 860 KB Output is correct
58 Correct 2 ms 860 KB Output is correct
59 Correct 1 ms 348 KB Output is correct
60 Correct 1 ms 348 KB Output is correct
61 Correct 37 ms 348 KB Output is correct
62 Execution timed out 5019 ms 30504 KB Time limit exceeded
63 Halted 0 ms 0 KB -