Submission #1103920

# Submission time Handle Problem Language Result Execution time Memory
1103920 2024-10-22T09:04:26 Z Pacybwoah Escape Route 2 (JOI24_escape2) C++17
100 / 100
702 ms 90392 KB
#include<iostream>
#include<vector>
#include<algorithm>
#include<utility>
const int K = 1000;
using namespace std;
typedef long long ll;
const ll inf = 8e18;
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n, m = 0;
    ll t;
    cin >> n >> t;
    vector<vector<pair<int, int>>> rngs(n);
    vector<int> sz(n), pre(n);
    for(int i = 1; i < n; i++){
        int tmp;
        cin >> tmp;
        for(int j = 0; j < tmp; j++){
            int a, b;
            cin >> a >> b;
            rngs[i].emplace_back(a, b);
        }
        sort(rngs[i].begin(), rngs[i].end());
        vector<pair<int, int>> vtmp;
        int mini = 2e9;
        for(int j = tmp - 1; j >= 0; j--){
            auto x = rngs[i][j];
            if(x.second < mini){
                mini = x.second;
                vtmp.push_back(x);
            }
        }
        reverse(vtmp.begin(), vtmp.end());
        rngs[i] = vtmp;
        sz[i] = (int)rngs[i].size();
        m += sz[i];
        pre[i] = pre[i - 1] + sz[i];
    }
    vector<vector<pair<int, ll>>> panc(17, vector<pair<int, ll>>(m + 1)), nanc(17, vector<pair<int, ll>>(m + 1));
    vector<pair<int, ll>> lst(m + 1), nxt(m + 1);
    for(int i = 2; i < n; i++){
        int ptr = 0;
        for(int j = 0; j < sz[i - 1]; j++){
            int id = pre[i - 2] + j + 1;
            while(ptr < sz[i] && rngs[i][ptr].first < rngs[i - 1][j].second) ptr++;
            if(ptr < sz[i]) nxt[id] = make_pair(pre[i - 1] + ptr + 1, rngs[i][ptr].first - rngs[i - 1][j].second);
            else nxt[id] = make_pair(pre[i - 1] + 1, t + rngs[i][0].first - rngs[i - 1][j].second);
        }
        ptr = -1;
        for(int j = 0; j < sz[i]; j++){
            int id = pre[i - 1] + j + 1;
            while(ptr < sz[i - 1] - 1 && rngs[i - 1][ptr + 1].second <= rngs[i][j].first) ptr++;
            if(ptr >= 0) lst[id] = make_pair(pre[i - 2] + ptr + 1, rngs[i][j].first - rngs[i - 1][ptr].second);
            else lst[id] = make_pair(pre[i - 1], t + rngs[i][j].first - rngs[i - 1][sz[i - 1] - 1].second);
        }
    }
    for(int i = 1; i < n; i++){
        for(int j = 0; j < sz[i]; j++){
            int id = pre[i - 1] + j + 1;
            nanc[0][id] = make_pair(id, rngs[i][j].second - rngs[i][j].first);
            panc[0][id] = nanc[0][id];
        }
    }
    for(int i = 1; i < 17; i++){
        for(int j = 1; j <= m; j++){
            nanc[i][j] = make_pair(nanc[i - 1][nxt[nanc[i - 1][j].first].first].first, nanc[i - 1][j].second + nanc[i - 1][nxt[nanc[i - 1][j].first].first].second + nxt[nanc[i - 1][j].first].second);
            panc[i][j] = make_pair(panc[i - 1][lst[panc[i - 1][j].first].first].first, panc[i - 1][j].second + panc[i - 1][lst[panc[i - 1][j].first].first].second + lst[panc[i - 1][j].first].second);
        }
    }
    auto nquery = [&](int pos, int kk, int k2){
        ll mini = inf;
        for(int times = 0; times < sz[pos]; times++){
            int id = pre[pos - 1] + times + 1;
            ll ans = -rngs[pos][times].second + rngs[pos][times].first;
            int k = kk;
            for(int i = 0; i < 17; i++){
                if(k & (1 << i)){
                    k -= (1 << i);
                    ans += nanc[i][id].second;
                    id = nanc[i][id].first;
                    if(k > 0) ans += nxt[id].second;
                    id = nxt[id].first;
                    //cout << id << " " << ans << "\n";
                }
            }
            //cout << ans << "\n";
            id = pre[pos - 1] + times + 1;
            k = k2;
            for(int i = 0; i < 17; i++){
                if(k & (1 << i)){
                    k -= (1 << i);
                    ans += panc[i][id].second;
                    id = panc[i][id].first;
                    if(k > 0) ans += lst[id].second;
                    id = lst[id].first;
                }
            }
            mini = min(mini, ans);
        }
        return mini;
    };
    vector<vector<pair<int, int>>> st(17, vector<pair<int, int>>(n));
    for(int j = 1; j < n; j++){
        st[0][j] = make_pair(sz[j], j);
    }
    for(int i = 1; i < 17; i++) for(int j = 1; j < n; j++) st[i][j] = min(st[i - 1][j], st[i - 1][min(n - 1, j + (1 << (i - 1)))]);
    vector<int> bases(n);
    for(int i = 1; i < n; i++){
        bases[i] = bases[i - 1];
        if(i == (1 << (bases[i] + 1))) bases[i]++;
    }
    auto query = [&](int l, int r){
        r--;
        int len = r - l + 1;
        return min(st[bases[len]][l], st[bases[len]][r - (1 << bases[len]) + 1]).second;
    };
    int q;
    cin >> q;
    vector<ll> anss(q);
    vector<vector<pair<int, int>>> queries(n + 1);
    for(int i = 0; i < q; i++){
        int l, r;
        cin >> l >> r;
        if(r - l > K){
            int best = query(l, r);
            anss[i] = nquery(best, r - best, best - l + 1);
            //cout << best << "\n";
        }
        else{
            queries[l].emplace_back(r, i);
        }
    }
    ll mini = inf;
    for(int i = 1; i < n; i++){
        if(queries[i].empty()) continue;
        sort(queries[i].begin(), queries[i].end());
        int szz = queries[i].size();
        int ptr = 0;
        vector<ll> ans(sz[i]);
        vector<int> id(sz[i]);
        for(int j = 0; j < sz[i]; j++) id[j] = pre[i - 1] + j + 1;
        for(int j = i; j < min(n, i + K); j++){
            mini = inf;
            for(int k = 0; k < sz[i]; k++){
                ans[k] += nanc[0][id[k]].second;
                mini = min(mini, ans[k]);
            }
            while(ptr < szz && queries[i][ptr].first - 1 == j){
                anss[queries[i][ptr].second] = mini;
                ptr++;
            }
            for(int k = 0; k < sz[i]; k++){
                ans[k] += nxt[id[k]].second;
                id[k] = nxt[id[k]].first;
            }
        }
    }
    for(int i = 0; i < q; i++) cout << anss[i] << "\n";
    //for(int i = 1; i <= m; i++) cout << nanc[1][i].first << " " << nanc[1][i].second << '\n';
}
// g++ -std=c++17 -Wall -Wextra -Wshadow -fsanitize=undefined -fsanitize=address -o run escape.cpp
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 53 ms 11192 KB Output is correct
3 Correct 61 ms 8864 KB Output is correct
4 Correct 80 ms 11088 KB Output is correct
5 Correct 68 ms 10568 KB Output is correct
6 Correct 102 ms 11804 KB Output is correct
7 Correct 85 ms 11868 KB Output is correct
8 Correct 72 ms 10312 KB Output is correct
9 Correct 87 ms 13964 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 53 ms 11192 KB Output is correct
3 Correct 61 ms 8864 KB Output is correct
4 Correct 80 ms 11088 KB Output is correct
5 Correct 68 ms 10568 KB Output is correct
6 Correct 102 ms 11804 KB Output is correct
7 Correct 85 ms 11868 KB Output is correct
8 Correct 72 ms 10312 KB Output is correct
9 Correct 87 ms 13964 KB Output is correct
10 Correct 1 ms 336 KB Output is correct
11 Correct 70 ms 12872 KB Output is correct
12 Correct 77 ms 12396 KB Output is correct
13 Correct 75 ms 12872 KB Output is correct
14 Correct 69 ms 12360 KB Output is correct
15 Correct 70 ms 13048 KB Output is correct
16 Correct 51 ms 9912 KB Output is correct
17 Correct 82 ms 11848 KB Output is correct
18 Correct 71 ms 11336 KB Output is correct
19 Correct 85 ms 11804 KB Output is correct
20 Correct 72 ms 12492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 53 ms 11192 KB Output is correct
3 Correct 61 ms 8864 KB Output is correct
4 Correct 80 ms 11088 KB Output is correct
5 Correct 68 ms 10568 KB Output is correct
6 Correct 102 ms 11804 KB Output is correct
7 Correct 85 ms 11868 KB Output is correct
8 Correct 72 ms 10312 KB Output is correct
9 Correct 87 ms 13964 KB Output is correct
10 Correct 315 ms 69748 KB Output is correct
11 Correct 461 ms 86248 KB Output is correct
12 Correct 418 ms 88036 KB Output is correct
13 Correct 359 ms 85804 KB Output is correct
14 Correct 449 ms 90392 KB Output is correct
15 Correct 414 ms 86244 KB Output is correct
16 Correct 221 ms 68460 KB Output is correct
17 Correct 410 ms 87100 KB Output is correct
18 Correct 225 ms 75924 KB Output is correct
19 Correct 184 ms 74068 KB Output is correct
20 Correct 191 ms 76016 KB Output is correct
21 Correct 223 ms 73884 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 53 ms 11192 KB Output is correct
3 Correct 61 ms 8864 KB Output is correct
4 Correct 80 ms 11088 KB Output is correct
5 Correct 68 ms 10568 KB Output is correct
6 Correct 102 ms 11804 KB Output is correct
7 Correct 85 ms 11868 KB Output is correct
8 Correct 72 ms 10312 KB Output is correct
9 Correct 87 ms 13964 KB Output is correct
10 Correct 1 ms 336 KB Output is correct
11 Correct 70 ms 12872 KB Output is correct
12 Correct 77 ms 12396 KB Output is correct
13 Correct 75 ms 12872 KB Output is correct
14 Correct 69 ms 12360 KB Output is correct
15 Correct 70 ms 13048 KB Output is correct
16 Correct 51 ms 9912 KB Output is correct
17 Correct 82 ms 11848 KB Output is correct
18 Correct 71 ms 11336 KB Output is correct
19 Correct 85 ms 11804 KB Output is correct
20 Correct 72 ms 12492 KB Output is correct
21 Correct 315 ms 69748 KB Output is correct
22 Correct 461 ms 86248 KB Output is correct
23 Correct 418 ms 88036 KB Output is correct
24 Correct 359 ms 85804 KB Output is correct
25 Correct 449 ms 90392 KB Output is correct
26 Correct 414 ms 86244 KB Output is correct
27 Correct 221 ms 68460 KB Output is correct
28 Correct 410 ms 87100 KB Output is correct
29 Correct 225 ms 75924 KB Output is correct
30 Correct 184 ms 74068 KB Output is correct
31 Correct 191 ms 76016 KB Output is correct
32 Correct 223 ms 73884 KB Output is correct
33 Correct 549 ms 69164 KB Output is correct
34 Correct 702 ms 71772 KB Output is correct
35 Correct 606 ms 63156 KB Output is correct
36 Correct 533 ms 67236 KB Output is correct
37 Correct 517 ms 65568 KB Output is correct
38 Correct 394 ms 63064 KB Output is correct
39 Correct 408 ms 78680 KB Output is correct
40 Correct 327 ms 52520 KB Output is correct
41 Correct 374 ms 54476 KB Output is correct
42 Correct 467 ms 72372 KB Output is correct
43 Correct 318 ms 57096 KB Output is correct
44 Correct 322 ms 55152 KB Output is correct
45 Correct 159 ms 66760 KB Output is correct
46 Correct 172 ms 61932 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 464 KB Output is correct
3 Correct 95 ms 55180 KB Output is correct
4 Correct 101 ms 57296 KB Output is correct
5 Correct 107 ms 55240 KB Output is correct
6 Correct 96 ms 56652 KB Output is correct
7 Correct 94 ms 54988 KB Output is correct
8 Correct 76 ms 30088 KB Output is correct
9 Correct 104 ms 55504 KB Output is correct
10 Correct 56 ms 29672 KB Output is correct
11 Correct 181 ms 76488 KB Output is correct
12 Correct 195 ms 69576 KB Output is correct
13 Correct 171 ms 63220 KB Output is correct
14 Correct 128 ms 60848 KB Output is correct
15 Correct 172 ms 60800 KB Output is correct
16 Correct 150 ms 60872 KB Output is correct
17 Correct 161 ms 60852 KB Output is correct
18 Correct 204 ms 57732 KB Output is correct
19 Correct 120 ms 57524 KB Output is correct
20 Correct 98 ms 70856 KB Output is correct
21 Correct 137 ms 70956 KB Output is correct
22 Correct 93 ms 63688 KB Output is correct
23 Correct 133 ms 63856 KB Output is correct
24 Correct 164 ms 63864 KB Output is correct
25 Correct 104 ms 63588 KB Output is correct
26 Correct 193 ms 63688 KB Output is correct
27 Correct 180 ms 76004 KB Output is correct
28 Correct 204 ms 76496 KB Output is correct
29 Correct 206 ms 76488 KB Output is correct
30 Correct 96 ms 14016 KB Output is correct
31 Correct 43 ms 6660 KB Output is correct
32 Correct 92 ms 19192 KB Output is correct
33 Correct 43 ms 7616 KB Output is correct
34 Correct 50 ms 12504 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 53 ms 11192 KB Output is correct
3 Correct 61 ms 8864 KB Output is correct
4 Correct 80 ms 11088 KB Output is correct
5 Correct 68 ms 10568 KB Output is correct
6 Correct 102 ms 11804 KB Output is correct
7 Correct 85 ms 11868 KB Output is correct
8 Correct 72 ms 10312 KB Output is correct
9 Correct 87 ms 13964 KB Output is correct
10 Correct 1 ms 336 KB Output is correct
11 Correct 70 ms 12872 KB Output is correct
12 Correct 77 ms 12396 KB Output is correct
13 Correct 75 ms 12872 KB Output is correct
14 Correct 69 ms 12360 KB Output is correct
15 Correct 70 ms 13048 KB Output is correct
16 Correct 51 ms 9912 KB Output is correct
17 Correct 82 ms 11848 KB Output is correct
18 Correct 71 ms 11336 KB Output is correct
19 Correct 85 ms 11804 KB Output is correct
20 Correct 72 ms 12492 KB Output is correct
21 Correct 315 ms 69748 KB Output is correct
22 Correct 461 ms 86248 KB Output is correct
23 Correct 418 ms 88036 KB Output is correct
24 Correct 359 ms 85804 KB Output is correct
25 Correct 449 ms 90392 KB Output is correct
26 Correct 414 ms 86244 KB Output is correct
27 Correct 221 ms 68460 KB Output is correct
28 Correct 410 ms 87100 KB Output is correct
29 Correct 225 ms 75924 KB Output is correct
30 Correct 184 ms 74068 KB Output is correct
31 Correct 191 ms 76016 KB Output is correct
32 Correct 223 ms 73884 KB Output is correct
33 Correct 549 ms 69164 KB Output is correct
34 Correct 702 ms 71772 KB Output is correct
35 Correct 606 ms 63156 KB Output is correct
36 Correct 533 ms 67236 KB Output is correct
37 Correct 517 ms 65568 KB Output is correct
38 Correct 394 ms 63064 KB Output is correct
39 Correct 408 ms 78680 KB Output is correct
40 Correct 327 ms 52520 KB Output is correct
41 Correct 374 ms 54476 KB Output is correct
42 Correct 467 ms 72372 KB Output is correct
43 Correct 318 ms 57096 KB Output is correct
44 Correct 322 ms 55152 KB Output is correct
45 Correct 159 ms 66760 KB Output is correct
46 Correct 172 ms 61932 KB Output is correct
47 Correct 1 ms 336 KB Output is correct
48 Correct 1 ms 464 KB Output is correct
49 Correct 95 ms 55180 KB Output is correct
50 Correct 101 ms 57296 KB Output is correct
51 Correct 107 ms 55240 KB Output is correct
52 Correct 96 ms 56652 KB Output is correct
53 Correct 94 ms 54988 KB Output is correct
54 Correct 76 ms 30088 KB Output is correct
55 Correct 104 ms 55504 KB Output is correct
56 Correct 56 ms 29672 KB Output is correct
57 Correct 181 ms 76488 KB Output is correct
58 Correct 195 ms 69576 KB Output is correct
59 Correct 171 ms 63220 KB Output is correct
60 Correct 128 ms 60848 KB Output is correct
61 Correct 172 ms 60800 KB Output is correct
62 Correct 150 ms 60872 KB Output is correct
63 Correct 161 ms 60852 KB Output is correct
64 Correct 204 ms 57732 KB Output is correct
65 Correct 120 ms 57524 KB Output is correct
66 Correct 98 ms 70856 KB Output is correct
67 Correct 137 ms 70956 KB Output is correct
68 Correct 93 ms 63688 KB Output is correct
69 Correct 133 ms 63856 KB Output is correct
70 Correct 164 ms 63864 KB Output is correct
71 Correct 104 ms 63588 KB Output is correct
72 Correct 193 ms 63688 KB Output is correct
73 Correct 180 ms 76004 KB Output is correct
74 Correct 204 ms 76496 KB Output is correct
75 Correct 206 ms 76488 KB Output is correct
76 Correct 96 ms 14016 KB Output is correct
77 Correct 43 ms 6660 KB Output is correct
78 Correct 92 ms 19192 KB Output is correct
79 Correct 43 ms 7616 KB Output is correct
80 Correct 50 ms 12504 KB Output is correct
81 Correct 202 ms 70960 KB Output is correct
82 Correct 190 ms 70960 KB Output is correct
83 Correct 194 ms 70976 KB Output is correct
84 Correct 185 ms 70712 KB Output is correct
85 Correct 173 ms 70708 KB Output is correct
86 Correct 125 ms 43284 KB Output is correct
87 Correct 202 ms 71760 KB Output is correct
88 Correct 193 ms 72200 KB Output is correct
89 Correct 197 ms 72132 KB Output is correct
90 Correct 138 ms 44344 KB Output is correct
91 Correct 86 ms 41912 KB Output is correct
92 Correct 291 ms 74588 KB Output is correct
93 Correct 296 ms 74216 KB Output is correct
94 Correct 284 ms 74284 KB Output is correct
95 Correct 342 ms 74540 KB Output is correct
96 Correct 312 ms 71468 KB Output is correct
97 Correct 181 ms 71468 KB Output is correct
98 Correct 171 ms 83500 KB Output is correct
99 Correct 292 ms 83764 KB Output is correct
100 Correct 170 ms 76336 KB Output is correct
101 Correct 269 ms 76396 KB Output is correct
102 Correct 320 ms 76604 KB Output is correct
103 Correct 187 ms 75956 KB Output is correct
104 Correct 292 ms 76332 KB Output is correct
105 Correct 113 ms 17732 KB Output is correct
106 Correct 92 ms 14280 KB Output is correct
107 Correct 165 ms 28764 KB Output is correct
108 Correct 112 ms 16644 KB Output is correct
109 Correct 92 ms 21788 KB Output is correct