답안 #953080

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
953080 2024-03-25T12:23:04 Z arbuzick Tourism (JOI23_tourism) C++17
100 / 100
2919 ms 174912 KB
#pragma GCC optimize("Ofast,no-stack-protector")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("fast-math")
#include <bits/stdc++.h>
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2")

using namespace std;

const int MAX_MEM = 7e8;
int mpos = 0;
char mem[MAX_MEM];
inline void *operator new(size_t n) {
    assert((mpos += n) <= MAX_MEM);
    return (void *)(mem + mpos - n);
}
void operator delete(void *) noexcept {}          // must have!
void operator delete(void *, size_t) noexcept {}  // must have!

constexpr int maxn = 1e5 + 5, lg = 20;

vector<int> g1[maxn];

vector<int> g[maxn];

int tin[maxn], tout[maxn];

int t = 0;

int d[maxn];

// int used_arr[maxn];

int tin1[maxn];

void dfs1(int v, int pr) {
    tin1[v] = t++;
    for (auto u : g1[v]) {
        if (u != pr) {
            dfs1(u, v);
        }
    }
}

int pos[maxn];

vector<int> ord;

void dfs(int v, int pr) {
    tin[v] = t++;
    pos[v] = ord.size();
    ord.push_back(d[v]);
    for (auto u : g[v]) {
        if (u != pr) {
            d[u] = d[v] + 1;
            dfs(u, v);
            ord.push_back(d[v]);
        }
    }
    tout[v] = t;
}

int mn[lg][maxn * 2];

int pw[lg];

int vl[maxn * 2];

void build(int n) {
    dfs1(0, 0);
    for (int i = 0; i < n; ++i) {
        for (auto j : g1[i]) {
            g[tin1[i]].push_back(tin1[j]);
        }
    }
    t = 0;
    dfs(0, 0);
    for (int i = 0; i < (int)ord.size(); ++i) {
        mn[0][i] = ord[i];
    }
    for (int i = 0; i < lg; ++i) {
        pw[i] = (1 << i);
    }
    for (int i = 2; i < maxn * 2; ++i) {
        vl[i] = vl[i / 2] + 1;
    }
    for (int i = 1; i < lg; ++i) {
        for (int l = 0; l + pw[i] <= (int)ord.size(); ++l) {
            int r = l + pw[i];
            mn[i][l] = min(mn[i - 1][l], mn[i - 1][r - pw[i - 1]]);
        }
    }
}

int lca(int a, int b) {
    // return min(a, b);
    a = pos[a], b = pos[b];
    if (a > b) {
        swap(a, b);
    }
    b++;
    int len = b - a;
    int l = vl[len];
    return min(mn[l][a], mn[l][b - pw[l]]);
}

constexpr int sq = 317;

vector<pair<int, int>> qs[sq];

constexpr int R = 1 << 17;

int sum[R * 2];

void change(int pos, int val) {
    pos += R;
    sum[pos] = val;
    for (pos /= 2; pos > 0; pos /= 2) {
        sum[pos] = sum[pos * 2] + sum[pos * 2 + 1];
    }
}

int get_l(int pos) {
    pos += R;
    int prv = pos;
    pos /= 2;
    while (sum[pos * 2] == 0 || prv == pos * 2) {
        prv = pos;
        pos /= 2;
    }
    pos = pos * 2;
    while (pos < R) {
        if (sum[pos * 2 + 1]) {
            pos = pos * 2 + 1;
        } else {
            pos = pos * 2;
        }
    }
    return pos - R;
}

int get_r(int pos) {
    pos += R;
    int prv = pos;
    pos /= 2;
    while (sum[pos * 2 + 1] == 0 || prv == pos * 2 + 1) {
        prv = pos;
        pos /= 2;
    }
    pos = pos * 2 + 1;
    while (pos < R) {
        if (sum[pos * 2]) {
            pos = pos * 2;
        } else {
            pos = pos * 2 + 1;
        }
    }
    return pos - R;
}

void solve() {
    int n, m, q;
    cin >> n >> m >> q;
    for (int i = 0; i < n - 1; ++i) {
        int a, b;
        cin >> a >> b;
        a--, b--;
        g1[a].push_back(b);
        g1[b].push_back(a);
    }
    build(n);
    vector<int> c(m);
    for (int i = 0; i < m; ++i) {
        cin >> c[i];
        c[i]--;
        c[i] = tin1[c[i]];
    }
    vector<int> l(q), r(q), ans(q);
    for (int i = 0; i < q; ++i) {
        cin >> l[i] >> r[i];
        l[i]--;
        qs[l[i] / sq].emplace_back(r[i], i);
    }
    for (int st = 0; st < sq; ++st) {
        sort(qs[st].begin(), qs[st].end());
        int ans_nw = 0, root_d = -1, mn = -1, mx = -1;
        int pos = (st + 1) * sq;
        for (auto [rr, ind] : qs[st]) {
            while (pos < rr) {
                if (root_d == -1) {
                    ans_nw = 1;
                    root_d = d[c[pos]];
                    mn = mx = c[pos];
                    change(c[pos], 1);
                    pos++;
                } else {
                    if (sum[c[pos] + R]) {
                        pos++;
                        continue;
                    }
                    if (mx < c[pos]) {
                        int dv = lca(c[pos], mx);
                        ans_nw += d[c[pos]] - dv;
                        if (dv < root_d) {
                            ans_nw += root_d - dv;
                            root_d = dv;
                        }
                        mx = c[pos];
                    } else if (c[pos] < mn) {
                        int dv = lca(c[pos], mn);
                        ans_nw += d[c[pos]] - dv;
                        if (dv < root_d) {
                            ans_nw += root_d - dv;
                            root_d = dv;
                        }
                        mn = c[pos];
                    } else {
                        int v1 = get_r(c[pos]), v2 = get_l(c[pos]);
                        v1 = lca(v1, c[pos]);
                        v2 = lca(v2, c[pos]);
                        ans_nw += d[c[pos]] - max(v1, v2);
                    }
                    change(c[pos], 1);
                    pos++;
                }
            }
            vector<int> add;
            int ans_nw_start = ans_nw, root_d_start = root_d, mn_start = mn, mx_start = mx;
            if (root_d == -1) {
                ans_nw = 1;
                root_d = d[c[l[ind]]];
                mn = mx = c[l[ind]];
                change(c[l[ind]], 1);
                add.push_back(c[l[ind]]);
            }
            for (int i = l[ind]; i < r[ind] && i < (st + 1) * sq; ++i) {
                if (sum[c[i] + R]) {
                    continue;
                }
                add.push_back(c[i]);
                if (mx < c[i]) {
                    int dv = lca(c[i], mx);
                    ans_nw += d[c[i]] - dv;
                    if (dv < root_d) {
                        ans_nw += root_d - dv;
                        root_d = dv;
                    }
                    mx = c[i];
                } else if (c[i] < mn) {
                    int dv = lca(c[i], mn);
                    ans_nw += d[c[i]] - dv;
                    if (dv < root_d) {
                        ans_nw += root_d - dv;
                        root_d = dv;
                    }
                    mn = c[i];
                } else {
                    int v1 = get_r(c[i]), v2 = get_l(c[i]);
                    v1 = lca(c[i], v1);
                    v2 = lca(c[i], v2);
                    ans_nw += d[c[i]] - max(v1, v2);
                }
                change(c[i], 1);
            }
            ans[ind] = ans_nw;
            for (auto vl : add) {
                change(vl, 0);
            }
            ans_nw = ans_nw_start;
            root_d = root_d_start;
            mn = mn_start;
            mx = mx_start;
        }
        for (int i = 0; i < n; ++i) {
            if (sum[i + R]) {
                change(i, 0);
            }
        }
    }
    for (int i = 0; i < q; ++i) {
        cout << ans[i] << '\n';
    }
}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int t = 1;
    // cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 12632 KB Output is correct
2 Correct 3 ms 12636 KB Output is correct
3 Correct 3 ms 14684 KB Output is correct
4 Correct 4 ms 16732 KB Output is correct
5 Correct 5 ms 16732 KB Output is correct
6 Correct 4 ms 16728 KB Output is correct
7 Correct 4 ms 16728 KB Output is correct
8 Correct 4 ms 18780 KB Output is correct
9 Correct 6 ms 18780 KB Output is correct
10 Correct 6 ms 18880 KB Output is correct
11 Correct 6 ms 18780 KB Output is correct
12 Correct 9 ms 20948 KB Output is correct
13 Correct 8 ms 20996 KB Output is correct
14 Correct 7 ms 20828 KB Output is correct
15 Correct 5 ms 18776 KB Output is correct
16 Correct 5 ms 18780 KB Output is correct
17 Correct 5 ms 18956 KB Output is correct
18 Correct 5 ms 18780 KB Output is correct
19 Correct 5 ms 18780 KB Output is correct
20 Correct 5 ms 18776 KB Output is correct
21 Correct 5 ms 18932 KB Output is correct
22 Correct 5 ms 18780 KB Output is correct
23 Correct 6 ms 18780 KB Output is correct
24 Correct 5 ms 18780 KB Output is correct
25 Correct 5 ms 18940 KB Output is correct
26 Correct 6 ms 18940 KB Output is correct
27 Correct 2 ms 10840 KB Output is correct
28 Correct 3 ms 18776 KB Output is correct
29 Correct 3 ms 18780 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 12632 KB Output is correct
2 Correct 3 ms 12636 KB Output is correct
3 Correct 3 ms 14684 KB Output is correct
4 Correct 4 ms 16732 KB Output is correct
5 Correct 5 ms 16732 KB Output is correct
6 Correct 4 ms 16728 KB Output is correct
7 Correct 4 ms 16728 KB Output is correct
8 Correct 4 ms 18780 KB Output is correct
9 Correct 6 ms 18780 KB Output is correct
10 Correct 6 ms 18880 KB Output is correct
11 Correct 6 ms 18780 KB Output is correct
12 Correct 9 ms 20948 KB Output is correct
13 Correct 8 ms 20996 KB Output is correct
14 Correct 7 ms 20828 KB Output is correct
15 Correct 5 ms 18776 KB Output is correct
16 Correct 5 ms 18780 KB Output is correct
17 Correct 5 ms 18956 KB Output is correct
18 Correct 5 ms 18780 KB Output is correct
19 Correct 5 ms 18780 KB Output is correct
20 Correct 5 ms 18776 KB Output is correct
21 Correct 5 ms 18932 KB Output is correct
22 Correct 5 ms 18780 KB Output is correct
23 Correct 6 ms 18780 KB Output is correct
24 Correct 5 ms 18780 KB Output is correct
25 Correct 5 ms 18940 KB Output is correct
26 Correct 6 ms 18940 KB Output is correct
27 Correct 2 ms 10840 KB Output is correct
28 Correct 3 ms 18776 KB Output is correct
29 Correct 3 ms 18780 KB Output is correct
30 Correct 17 ms 22876 KB Output is correct
31 Correct 22 ms 23124 KB Output is correct
32 Correct 25 ms 24924 KB Output is correct
33 Correct 26 ms 24868 KB Output is correct
34 Correct 25 ms 24864 KB Output is correct
35 Correct 23 ms 22876 KB Output is correct
36 Correct 23 ms 22872 KB Output is correct
37 Correct 22 ms 22876 KB Output is correct
38 Correct 24 ms 25184 KB Output is correct
39 Correct 24 ms 25180 KB Output is correct
40 Correct 24 ms 23132 KB Output is correct
41 Correct 24 ms 25168 KB Output is correct
42 Correct 23 ms 23140 KB Output is correct
43 Correct 22 ms 23152 KB Output is correct
44 Correct 25 ms 25180 KB Output is correct
45 Correct 23 ms 23116 KB Output is correct
46 Correct 24 ms 25184 KB Output is correct
47 Correct 24 ms 25048 KB Output is correct
48 Correct 21 ms 22872 KB Output is correct
49 Correct 23 ms 23132 KB Output is correct
50 Correct 25 ms 24924 KB Output is correct
51 Correct 28 ms 23040 KB Output is correct
52 Correct 24 ms 22876 KB Output is correct
53 Correct 24 ms 24872 KB Output is correct
54 Correct 26 ms 25120 KB Output is correct
55 Correct 25 ms 25132 KB Output is correct
56 Correct 3 ms 10596 KB Output is correct
57 Correct 4 ms 20828 KB Output is correct
58 Correct 4 ms 20828 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 12636 KB Output is correct
2 Correct 2 ms 10588 KB Output is correct
3 Correct 3 ms 10600 KB Output is correct
4 Correct 1651 ms 119944 KB Output is correct
5 Correct 1419 ms 132256 KB Output is correct
6 Correct 1720 ms 114756 KB Output is correct
7 Correct 2701 ms 174208 KB Output is correct
8 Correct 2680 ms 174336 KB Output is correct
9 Correct 2630 ms 174452 KB Output is correct
10 Correct 2729 ms 174884 KB Output is correct
11 Correct 2706 ms 174912 KB Output is correct
12 Correct 875 ms 139348 KB Output is correct
13 Correct 915 ms 139108 KB Output is correct
14 Correct 938 ms 159564 KB Output is correct
15 Correct 60 ms 42836 KB Output is correct
16 Correct 71 ms 46160 KB Output is correct
17 Correct 52 ms 16160 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 12756 KB Output is correct
2 Correct 38 ms 27964 KB Output is correct
3 Correct 45 ms 30296 KB Output is correct
4 Correct 43 ms 32340 KB Output is correct
5 Correct 52 ms 32856 KB Output is correct
6 Correct 56 ms 32820 KB Output is correct
7 Correct 58 ms 32956 KB Output is correct
8 Correct 62 ms 32856 KB Output is correct
9 Correct 62 ms 32848 KB Output is correct
10 Correct 74 ms 33076 KB Output is correct
11 Correct 69 ms 33060 KB Output is correct
12 Correct 64 ms 33076 KB Output is correct
13 Correct 63 ms 33200 KB Output is correct
14 Correct 66 ms 35372 KB Output is correct
15 Correct 67 ms 38480 KB Output is correct
16 Correct 61 ms 33108 KB Output is correct
17 Correct 61 ms 33108 KB Output is correct
18 Correct 63 ms 33188 KB Output is correct
19 Correct 52 ms 33108 KB Output is correct
20 Correct 55 ms 33104 KB Output is correct
21 Correct 58 ms 33068 KB Output is correct
22 Correct 57 ms 33116 KB Output is correct
23 Correct 62 ms 33108 KB Output is correct
24 Correct 62 ms 33104 KB Output is correct
25 Correct 66 ms 33116 KB Output is correct
26 Correct 67 ms 33108 KB Output is correct
27 Correct 74 ms 33432 KB Output is correct
28 Correct 65 ms 33048 KB Output is correct
29 Correct 63 ms 33236 KB Output is correct
30 Correct 63 ms 33108 KB Output is correct
31 Correct 60 ms 33364 KB Output is correct
32 Correct 69 ms 33364 KB Output is correct
33 Correct 68 ms 35808 KB Output is correct
34 Correct 70 ms 38584 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 12888 KB Output is correct
2 Correct 2 ms 10704 KB Output is correct
3 Correct 3 ms 10588 KB Output is correct
4 Correct 1938 ms 138068 KB Output is correct
5 Correct 1932 ms 133032 KB Output is correct
6 Correct 2570 ms 155944 KB Output is correct
7 Correct 2840 ms 167388 KB Output is correct
8 Correct 2911 ms 167872 KB Output is correct
9 Correct 2798 ms 167364 KB Output is correct
10 Correct 2865 ms 167516 KB Output is correct
11 Correct 2788 ms 167608 KB Output is correct
12 Correct 2760 ms 167224 KB Output is correct
13 Correct 2919 ms 167732 KB Output is correct
14 Correct 57 ms 16212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 12632 KB Output is correct
2 Correct 3 ms 12636 KB Output is correct
3 Correct 3 ms 14684 KB Output is correct
4 Correct 4 ms 16732 KB Output is correct
5 Correct 5 ms 16732 KB Output is correct
6 Correct 4 ms 16728 KB Output is correct
7 Correct 4 ms 16728 KB Output is correct
8 Correct 4 ms 18780 KB Output is correct
9 Correct 6 ms 18780 KB Output is correct
10 Correct 6 ms 18880 KB Output is correct
11 Correct 6 ms 18780 KB Output is correct
12 Correct 9 ms 20948 KB Output is correct
13 Correct 8 ms 20996 KB Output is correct
14 Correct 7 ms 20828 KB Output is correct
15 Correct 5 ms 18776 KB Output is correct
16 Correct 5 ms 18780 KB Output is correct
17 Correct 5 ms 18956 KB Output is correct
18 Correct 5 ms 18780 KB Output is correct
19 Correct 5 ms 18780 KB Output is correct
20 Correct 5 ms 18776 KB Output is correct
21 Correct 5 ms 18932 KB Output is correct
22 Correct 5 ms 18780 KB Output is correct
23 Correct 6 ms 18780 KB Output is correct
24 Correct 5 ms 18780 KB Output is correct
25 Correct 5 ms 18940 KB Output is correct
26 Correct 6 ms 18940 KB Output is correct
27 Correct 2 ms 10840 KB Output is correct
28 Correct 3 ms 18776 KB Output is correct
29 Correct 3 ms 18780 KB Output is correct
30 Correct 17 ms 22876 KB Output is correct
31 Correct 22 ms 23124 KB Output is correct
32 Correct 25 ms 24924 KB Output is correct
33 Correct 26 ms 24868 KB Output is correct
34 Correct 25 ms 24864 KB Output is correct
35 Correct 23 ms 22876 KB Output is correct
36 Correct 23 ms 22872 KB Output is correct
37 Correct 22 ms 22876 KB Output is correct
38 Correct 24 ms 25184 KB Output is correct
39 Correct 24 ms 25180 KB Output is correct
40 Correct 24 ms 23132 KB Output is correct
41 Correct 24 ms 25168 KB Output is correct
42 Correct 23 ms 23140 KB Output is correct
43 Correct 22 ms 23152 KB Output is correct
44 Correct 25 ms 25180 KB Output is correct
45 Correct 23 ms 23116 KB Output is correct
46 Correct 24 ms 25184 KB Output is correct
47 Correct 24 ms 25048 KB Output is correct
48 Correct 21 ms 22872 KB Output is correct
49 Correct 23 ms 23132 KB Output is correct
50 Correct 25 ms 24924 KB Output is correct
51 Correct 28 ms 23040 KB Output is correct
52 Correct 24 ms 22876 KB Output is correct
53 Correct 24 ms 24872 KB Output is correct
54 Correct 26 ms 25120 KB Output is correct
55 Correct 25 ms 25132 KB Output is correct
56 Correct 3 ms 10596 KB Output is correct
57 Correct 4 ms 20828 KB Output is correct
58 Correct 4 ms 20828 KB Output is correct
59 Correct 2 ms 12636 KB Output is correct
60 Correct 2 ms 10588 KB Output is correct
61 Correct 3 ms 10600 KB Output is correct
62 Correct 1651 ms 119944 KB Output is correct
63 Correct 1419 ms 132256 KB Output is correct
64 Correct 1720 ms 114756 KB Output is correct
65 Correct 2701 ms 174208 KB Output is correct
66 Correct 2680 ms 174336 KB Output is correct
67 Correct 2630 ms 174452 KB Output is correct
68 Correct 2729 ms 174884 KB Output is correct
69 Correct 2706 ms 174912 KB Output is correct
70 Correct 875 ms 139348 KB Output is correct
71 Correct 915 ms 139108 KB Output is correct
72 Correct 938 ms 159564 KB Output is correct
73 Correct 60 ms 42836 KB Output is correct
74 Correct 71 ms 46160 KB Output is correct
75 Correct 52 ms 16160 KB Output is correct
76 Correct 2 ms 12756 KB Output is correct
77 Correct 38 ms 27964 KB Output is correct
78 Correct 45 ms 30296 KB Output is correct
79 Correct 43 ms 32340 KB Output is correct
80 Correct 52 ms 32856 KB Output is correct
81 Correct 56 ms 32820 KB Output is correct
82 Correct 58 ms 32956 KB Output is correct
83 Correct 62 ms 32856 KB Output is correct
84 Correct 62 ms 32848 KB Output is correct
85 Correct 74 ms 33076 KB Output is correct
86 Correct 69 ms 33060 KB Output is correct
87 Correct 64 ms 33076 KB Output is correct
88 Correct 63 ms 33200 KB Output is correct
89 Correct 66 ms 35372 KB Output is correct
90 Correct 67 ms 38480 KB Output is correct
91 Correct 61 ms 33108 KB Output is correct
92 Correct 61 ms 33108 KB Output is correct
93 Correct 63 ms 33188 KB Output is correct
94 Correct 52 ms 33108 KB Output is correct
95 Correct 55 ms 33104 KB Output is correct
96 Correct 58 ms 33068 KB Output is correct
97 Correct 57 ms 33116 KB Output is correct
98 Correct 62 ms 33108 KB Output is correct
99 Correct 62 ms 33104 KB Output is correct
100 Correct 66 ms 33116 KB Output is correct
101 Correct 67 ms 33108 KB Output is correct
102 Correct 74 ms 33432 KB Output is correct
103 Correct 65 ms 33048 KB Output is correct
104 Correct 63 ms 33236 KB Output is correct
105 Correct 63 ms 33108 KB Output is correct
106 Correct 60 ms 33364 KB Output is correct
107 Correct 69 ms 33364 KB Output is correct
108 Correct 68 ms 35808 KB Output is correct
109 Correct 70 ms 38584 KB Output is correct
110 Correct 2 ms 12888 KB Output is correct
111 Correct 2 ms 10704 KB Output is correct
112 Correct 3 ms 10588 KB Output is correct
113 Correct 1938 ms 138068 KB Output is correct
114 Correct 1932 ms 133032 KB Output is correct
115 Correct 2570 ms 155944 KB Output is correct
116 Correct 2840 ms 167388 KB Output is correct
117 Correct 2911 ms 167872 KB Output is correct
118 Correct 2798 ms 167364 KB Output is correct
119 Correct 2865 ms 167516 KB Output is correct
120 Correct 2788 ms 167608 KB Output is correct
121 Correct 2760 ms 167224 KB Output is correct
122 Correct 2919 ms 167732 KB Output is correct
123 Correct 57 ms 16212 KB Output is correct
124 Correct 2426 ms 170240 KB Output is correct
125 Correct 1524 ms 115936 KB Output is correct
126 Correct 2786 ms 167656 KB Output is correct
127 Correct 2824 ms 167292 KB Output is correct
128 Correct 2772 ms 166524 KB Output is correct
129 Correct 2790 ms 167148 KB Output is correct
130 Correct 2770 ms 166740 KB Output is correct
131 Correct 2694 ms 172652 KB Output is correct
132 Correct 2658 ms 174332 KB Output is correct
133 Correct 2730 ms 170632 KB Output is correct
134 Correct 2707 ms 166480 KB Output is correct
135 Correct 2690 ms 166996 KB Output is correct
136 Correct 2735 ms 166256 KB Output is correct
137 Correct 2820 ms 166900 KB Output is correct
138 Correct 2716 ms 167000 KB Output is correct
139 Correct 2790 ms 167200 KB Output is correct
140 Correct 2779 ms 167032 KB Output is correct
141 Correct 2782 ms 166920 KB Output is correct
142 Correct 2768 ms 167300 KB Output is correct
143 Correct 57 ms 34900 KB Output is correct
144 Correct 76 ms 38484 KB Output is correct