답안 #1108728

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1108728 2024-11-04T20:56:02 Z vladilius Escape Route 2 (JOI24_escape2) C++17
100 / 100
1544 ms 190804 KB
#include <bits/stdc++.h>
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define pb push_back
#define ff first
#define ss second
const ll inf = numeric_limits<ll> :: max();
const int M = 1e5;
 
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    
    int n, D; cin>>n>>D;
    vector<int> x(n);
    vector<pii> t[n];
    for (int i = 1; i < n; i++){
        cin>>x[i];
        for (int j = 0; j < x[i]; j++){
            int a, b; cin>>a>>b;
            t[i].pb({a, b});
        }
    }
    
    for (int i = 1; i < n; i++){
        sort(t[i].begin(), t[i].end());
        vector<pii> nw;
        for (int j = 0; j < x[i]; j++){
            while (!nw.empty() && nw.back().ss >= t[i][j].ss){
                nw.pop_back();
            }
            if (nw.empty() || nw.back().ff != t[i][j].ff){
                nw.pb(t[i][j]);
            }
        }
        
        t[i] = nw;
        x[i] = (int) t[i].size();
    }
    
    vector<int> p(n);
    for (int i = 1; i < n; i++){
        p[i] = p[i - 1] + x[i];
    }
    int S = p.back();
    vector<int> g(S);
    vector<bool> e(S);
    for (int i = 1; i + 1 < n; i++){
        int k = x[i + 1] - 1, mn = 0;
        for (int j = x[i] - 1; j >= 0; j--){
            while (k >= 0 && t[i + 1][k].ff >= t[i][j].ss){
                mn = k--;
            }
            g[p[i - 1] + j] = p[i] + mn;
            e[p[i - 1] + j] = (t[i][j].ss > t[i + 1][mn].ff);
        }
    }
    
    int lg = (int) log2(S);
    pii sp[S][20];
    for (int i = 0; i < S; i++) sp[i][0] = {g[i], e[i]};
    for (int j = 1; j <= lg; j++){
        for (int i = 0; i + (1 << j) <= S; i++){
            sp[i][j] = {sp[sp[i][j - 1].ff][j - 1].ff, sp[i][j - 1].ss + sp[sp[i][j - 1].ff][j - 1].ss};
        }
    }
    
    auto get = [&](int l, int r){
        ll out = inf;
        for (int i = 0; i < x[l]; i++){
            int tt = r - l - 1, f = p[l - 1] + i, cc = 0;
            for (int j = lg; j >= 0; j--){
                int k = (1 << j);
                if (tt >= k){
                    cc += sp[f][j].ss;
                    f = sp[f][j].ff;
                    tt -= k;
                }
            }
            out = min(out, 1LL * D * cc + (t[r - 1][f - p[r - 2]].ss - t[l][i].ff));
        }
        return out;
    };
    
    const int A = 500;
    vector<int> all;
    pair<int, ll> st[M], nw[M];
    ll out[M / A + 5][n + 1];
    int s1 = 0, s2 = 0, cc = 0;
    ll mn;
    for (int i = 1; i < n; i++){
        if (x[i] <= A) continue;
        all.pb(i);
        s1 = s2 = 0;
        for (int j = 0; j < x[i]; j++){
            st[s1++] = {p[i - 1] + j, t[i][j].ss - t[i][j].ff};
        }
        for (int j = i + 1; j <= n; j++){
            mn = inf;
            for (int k = 0; k < s1; k++){
                mn = min(mn, st[k].ss);
            }
            out[cc][j] = mn;
            if (j == n) break;
            for (int k = 0; k < s1; k++){
                auto &[x, y] = st[k];
                y += D * e[x] + (t[j][g[x] - p[j - 1]].ss - t[j - 1][x - p[j - 2]].ss);
                x = g[x];
            }
            s2 = 0;
            int k = 0;
            while (k < s1){
                int h = k; mn = inf;
                while (h < s1 && st[k].ff == st[h].ff){
                    mn = min(mn, st[h].ss);
                    h++;
                }
                nw[s2++] = {st[k].ff, mn};
                k = h;
            }
            s1 = s2;
            for (int i = 0; i < s1; i++){
                st[i] = nw[i];
            }
        }
        cc++;
    }
    
    vector<int> :: iterator it;
    int q; cin>>q;
    for (int i = 1; i <= q; i++){
        int l, r; cin>>l>>r;
        if (x[l] <= A){
            cout<<get(l, r)<<"\n";
        }
        else {
            it = lower_bound(all.begin(), all.end(), l);
            int j = (int) (it - all.begin());
            cout<<out[j][r]<<"\n";
        }
    }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 3408 KB Output is correct
2 Correct 43 ms 6544 KB Output is correct
3 Correct 60 ms 8996 KB Output is correct
4 Correct 84 ms 11080 KB Output is correct
5 Correct 62 ms 10056 KB Output is correct
6 Correct 75 ms 11132 KB Output is correct
7 Correct 75 ms 11112 KB Output is correct
8 Correct 57 ms 8980 KB Output is correct
9 Correct 74 ms 11060 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 3408 KB Output is correct
2 Correct 43 ms 6544 KB Output is correct
3 Correct 60 ms 8996 KB Output is correct
4 Correct 84 ms 11080 KB Output is correct
5 Correct 62 ms 10056 KB Output is correct
6 Correct 75 ms 11132 KB Output is correct
7 Correct 75 ms 11112 KB Output is correct
8 Correct 57 ms 8980 KB Output is correct
9 Correct 74 ms 11060 KB Output is correct
10 Correct 4 ms 3408 KB Output is correct
11 Correct 75 ms 8028 KB Output is correct
12 Correct 84 ms 7936 KB Output is correct
13 Correct 75 ms 8024 KB Output is correct
14 Correct 72 ms 8008 KB Output is correct
15 Correct 70 ms 8640 KB Output is correct
16 Correct 43 ms 6472 KB Output is correct
17 Correct 76 ms 9800 KB Output is correct
18 Correct 71 ms 8788 KB Output is correct
19 Correct 73 ms 9800 KB Output is correct
20 Correct 70 ms 8520 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 3408 KB Output is correct
2 Correct 43 ms 6544 KB Output is correct
3 Correct 60 ms 8996 KB Output is correct
4 Correct 84 ms 11080 KB Output is correct
5 Correct 62 ms 10056 KB Output is correct
6 Correct 75 ms 11132 KB Output is correct
7 Correct 75 ms 11112 KB Output is correct
8 Correct 57 ms 8980 KB Output is correct
9 Correct 74 ms 11060 KB Output is correct
10 Correct 293 ms 150948 KB Output is correct
11 Correct 409 ms 190804 KB Output is correct
12 Correct 416 ms 190792 KB Output is correct
13 Correct 335 ms 189768 KB Output is correct
14 Correct 385 ms 190792 KB Output is correct
15 Correct 390 ms 190804 KB Output is correct
16 Correct 277 ms 150672 KB Output is correct
17 Correct 441 ms 190792 KB Output is correct
18 Correct 233 ms 169224 KB Output is correct
19 Correct 221 ms 169032 KB Output is correct
20 Correct 240 ms 169288 KB Output is correct
21 Correct 237 ms 169288 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 3408 KB Output is correct
2 Correct 43 ms 6544 KB Output is correct
3 Correct 60 ms 8996 KB Output is correct
4 Correct 84 ms 11080 KB Output is correct
5 Correct 62 ms 10056 KB Output is correct
6 Correct 75 ms 11132 KB Output is correct
7 Correct 75 ms 11112 KB Output is correct
8 Correct 57 ms 8980 KB Output is correct
9 Correct 74 ms 11060 KB Output is correct
10 Correct 4 ms 3408 KB Output is correct
11 Correct 75 ms 8028 KB Output is correct
12 Correct 84 ms 7936 KB Output is correct
13 Correct 75 ms 8024 KB Output is correct
14 Correct 72 ms 8008 KB Output is correct
15 Correct 70 ms 8640 KB Output is correct
16 Correct 43 ms 6472 KB Output is correct
17 Correct 76 ms 9800 KB Output is correct
18 Correct 71 ms 8788 KB Output is correct
19 Correct 73 ms 9800 KB Output is correct
20 Correct 70 ms 8520 KB Output is correct
21 Correct 293 ms 150948 KB Output is correct
22 Correct 409 ms 190804 KB Output is correct
23 Correct 416 ms 190792 KB Output is correct
24 Correct 335 ms 189768 KB Output is correct
25 Correct 385 ms 190792 KB Output is correct
26 Correct 390 ms 190804 KB Output is correct
27 Correct 277 ms 150672 KB Output is correct
28 Correct 441 ms 190792 KB Output is correct
29 Correct 233 ms 169224 KB Output is correct
30 Correct 221 ms 169032 KB Output is correct
31 Correct 240 ms 169288 KB Output is correct
32 Correct 237 ms 169288 KB Output is correct
33 Correct 331 ms 57920 KB Output is correct
34 Correct 256 ms 57672 KB Output is correct
35 Correct 234 ms 55788 KB Output is correct
36 Correct 219 ms 55788 KB Output is correct
37 Correct 215 ms 73032 KB Output is correct
38 Correct 226 ms 106568 KB Output is correct
39 Correct 295 ms 135028 KB Output is correct
40 Correct 219 ms 56136 KB Output is correct
41 Correct 207 ms 100180 KB Output is correct
42 Correct 287 ms 132936 KB Output is correct
43 Correct 188 ms 73288 KB Output is correct
44 Correct 219 ms 75168 KB Output is correct
45 Correct 186 ms 119368 KB Output is correct
46 Correct 141 ms 69704 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 3408 KB Output is correct
2 Correct 3 ms 3408 KB Output is correct
3 Correct 468 ms 20524 KB Output is correct
4 Correct 496 ms 20552 KB Output is correct
5 Correct 483 ms 20512 KB Output is correct
6 Correct 473 ms 20412 KB Output is correct
7 Correct 477 ms 20552 KB Output is correct
8 Correct 196 ms 13384 KB Output is correct
9 Correct 52 ms 20820 KB Output is correct
10 Correct 40 ms 12004 KB Output is correct
11 Correct 223 ms 169288 KB Output is correct
12 Correct 185 ms 119332 KB Output is correct
13 Correct 136 ms 69704 KB Output is correct
14 Correct 95 ms 51528 KB Output is correct
15 Correct 101 ms 51200 KB Output is correct
16 Correct 99 ms 52040 KB Output is correct
17 Correct 100 ms 51272 KB Output is correct
18 Correct 103 ms 22208 KB Output is correct
19 Correct 348 ms 20808 KB Output is correct
20 Correct 432 ms 129104 KB Output is correct
21 Correct 359 ms 128840 KB Output is correct
22 Correct 489 ms 74312 KB Output is correct
23 Correct 393 ms 74312 KB Output is correct
24 Correct 104 ms 74188 KB Output is correct
25 Correct 106 ms 74308 KB Output is correct
26 Correct 113 ms 74312 KB Output is correct
27 Correct 222 ms 169032 KB Output is correct
28 Correct 238 ms 169276 KB Output is correct
29 Correct 212 ms 169288 KB Output is correct
30 Correct 60 ms 16316 KB Output is correct
31 Correct 87 ms 5636 KB Output is correct
32 Correct 62 ms 12616 KB Output is correct
33 Correct 62 ms 6044 KB Output is correct
34 Correct 62 ms 7760 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 3408 KB Output is correct
2 Correct 43 ms 6544 KB Output is correct
3 Correct 60 ms 8996 KB Output is correct
4 Correct 84 ms 11080 KB Output is correct
5 Correct 62 ms 10056 KB Output is correct
6 Correct 75 ms 11132 KB Output is correct
7 Correct 75 ms 11112 KB Output is correct
8 Correct 57 ms 8980 KB Output is correct
9 Correct 74 ms 11060 KB Output is correct
10 Correct 4 ms 3408 KB Output is correct
11 Correct 75 ms 8028 KB Output is correct
12 Correct 84 ms 7936 KB Output is correct
13 Correct 75 ms 8024 KB Output is correct
14 Correct 72 ms 8008 KB Output is correct
15 Correct 70 ms 8640 KB Output is correct
16 Correct 43 ms 6472 KB Output is correct
17 Correct 76 ms 9800 KB Output is correct
18 Correct 71 ms 8788 KB Output is correct
19 Correct 73 ms 9800 KB Output is correct
20 Correct 70 ms 8520 KB Output is correct
21 Correct 293 ms 150948 KB Output is correct
22 Correct 409 ms 190804 KB Output is correct
23 Correct 416 ms 190792 KB Output is correct
24 Correct 335 ms 189768 KB Output is correct
25 Correct 385 ms 190792 KB Output is correct
26 Correct 390 ms 190804 KB Output is correct
27 Correct 277 ms 150672 KB Output is correct
28 Correct 441 ms 190792 KB Output is correct
29 Correct 233 ms 169224 KB Output is correct
30 Correct 221 ms 169032 KB Output is correct
31 Correct 240 ms 169288 KB Output is correct
32 Correct 237 ms 169288 KB Output is correct
33 Correct 331 ms 57920 KB Output is correct
34 Correct 256 ms 57672 KB Output is correct
35 Correct 234 ms 55788 KB Output is correct
36 Correct 219 ms 55788 KB Output is correct
37 Correct 215 ms 73032 KB Output is correct
38 Correct 226 ms 106568 KB Output is correct
39 Correct 295 ms 135028 KB Output is correct
40 Correct 219 ms 56136 KB Output is correct
41 Correct 207 ms 100180 KB Output is correct
42 Correct 287 ms 132936 KB Output is correct
43 Correct 188 ms 73288 KB Output is correct
44 Correct 219 ms 75168 KB Output is correct
45 Correct 186 ms 119368 KB Output is correct
46 Correct 141 ms 69704 KB Output is correct
47 Correct 3 ms 3408 KB Output is correct
48 Correct 3 ms 3408 KB Output is correct
49 Correct 468 ms 20524 KB Output is correct
50 Correct 496 ms 20552 KB Output is correct
51 Correct 483 ms 20512 KB Output is correct
52 Correct 473 ms 20412 KB Output is correct
53 Correct 477 ms 20552 KB Output is correct
54 Correct 196 ms 13384 KB Output is correct
55 Correct 52 ms 20820 KB Output is correct
56 Correct 40 ms 12004 KB Output is correct
57 Correct 223 ms 169288 KB Output is correct
58 Correct 185 ms 119332 KB Output is correct
59 Correct 136 ms 69704 KB Output is correct
60 Correct 95 ms 51528 KB Output is correct
61 Correct 101 ms 51200 KB Output is correct
62 Correct 99 ms 52040 KB Output is correct
63 Correct 100 ms 51272 KB Output is correct
64 Correct 103 ms 22208 KB Output is correct
65 Correct 348 ms 20808 KB Output is correct
66 Correct 432 ms 129104 KB Output is correct
67 Correct 359 ms 128840 KB Output is correct
68 Correct 489 ms 74312 KB Output is correct
69 Correct 393 ms 74312 KB Output is correct
70 Correct 104 ms 74188 KB Output is correct
71 Correct 106 ms 74308 KB Output is correct
72 Correct 113 ms 74312 KB Output is correct
73 Correct 222 ms 169032 KB Output is correct
74 Correct 238 ms 169276 KB Output is correct
75 Correct 212 ms 169288 KB Output is correct
76 Correct 60 ms 16316 KB Output is correct
77 Correct 87 ms 5636 KB Output is correct
78 Correct 62 ms 12616 KB Output is correct
79 Correct 62 ms 6044 KB Output is correct
80 Correct 62 ms 7760 KB Output is correct
81 Correct 1054 ms 24656 KB Output is correct
82 Correct 1141 ms 24540 KB Output is correct
83 Correct 1051 ms 24512 KB Output is correct
84 Correct 1053 ms 24436 KB Output is correct
85 Correct 1070 ms 24392 KB Output is correct
86 Correct 413 ms 16968 KB Output is correct
87 Correct 772 ms 25168 KB Output is correct
88 Correct 828 ms 25412 KB Output is correct
89 Correct 858 ms 25608 KB Output is correct
90 Correct 342 ms 17736 KB Output is correct
91 Correct 67 ms 13500 KB Output is correct
92 Correct 186 ms 60932 KB Output is correct
93 Correct 183 ms 59652 KB Output is correct
94 Correct 179 ms 59976 KB Output is correct
95 Correct 205 ms 59484 KB Output is correct
96 Correct 237 ms 26952 KB Output is correct
97 Correct 1010 ms 25140 KB Output is correct
98 Correct 1232 ms 133568 KB Output is correct
99 Correct 979 ms 133700 KB Output is correct
100 Correct 1544 ms 79176 KB Output is correct
101 Correct 1178 ms 79188 KB Output is correct
102 Correct 169 ms 79000 KB Output is correct
103 Correct 152 ms 79116 KB Output is correct
104 Correct 168 ms 79176 KB Output is correct
105 Correct 117 ms 11648 KB Output is correct
106 Correct 197 ms 7616 KB Output is correct
107 Correct 153 ms 16464 KB Output is correct
108 Correct 118 ms 8968 KB Output is correct
109 Correct 164 ms 10824 KB Output is correct