Submission #878941

# Submission time Handle Problem Language Result Execution time Memory
878941 2023-11-25T16:35:14 Z Tymond Railway Trip 2 (JOI22_ho_t4) C++17
100 / 100
791 ms 383144 KB
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define sec second
 
const int BASE = 1024 * 1024;
const int MAXK = 21;
 
pair<int, int> tree[2 * BASE + 7][MAXK];
pair<int, int> wDol[2 * BASE + 7];
pair<int, int> skacz[BASE + 7][MAXK];
int n, k;
 
void init(){
    for(int i = 0; i < BASE; i++){
        for(int j = 0; j < MAXK; j++){
            tree[i + BASE][j] = {i, i};
        }
    }
    
    for(int i = BASE - 1; i >= 1; i--){
        for(int j = 0; j < MAXK; j++){
            tree[i][j].fi = min(tree[2 * i][j].fi, tree[2 * i + 1][j].fi);
            tree[i][j].sec = max(tree[2 * i][j].sec, tree[2 * i + 1][j].sec);
        }
    }
    
    for(int i = 0; i < 2 * BASE; i++){
        wDol[i] = {1e9, -1e9};
    }
}
 
void pchnij(int v, int dep){
    tree[2 * v][dep].fi = min(tree[2 * v][dep].fi, wDol[v].fi);
    tree[2 * v][dep].sec = max(tree[2 * v][dep].sec, wDol[v].sec);
    wDol[2 * v].fi = min(wDol[2 * v].fi, wDol[v].fi);
    wDol[2 * v].sec = max(wDol[2 * v].sec, wDol[v].sec);
    
    tree[2 * v + 1][dep].fi = min(tree[2 * v + 1][dep].fi, wDol[v].fi);
    tree[2 * v + 1][dep].sec = max(tree[2 * v + 1][dep].sec, wDol[v].sec);
    wDol[2 * v + 1].fi = min(wDol[2 * v + 1].fi, wDol[v].fi);
    wDol[2 * v + 1].sec = max(wDol[2 * v + 1].sec, wDol[v].sec);
    
    wDol[v] = {1e9, -1e9};
}
 
int depth, a, b, val;
void update(int v, int l, int p){
    if(p < a || b < l){
        return;
    }
    
    if(a <= l && p <= b){
        tree[v][depth].fi = min(tree[v][depth].fi, val);
        tree[v][depth].sec = max(tree[v][depth].sec, val);
        
        wDol[v].fi = min(wDol[v].fi, val);
        wDol[v].sec = max(wDol[v].sec, val);
        
        return;
    }
    
    pchnij(v, depth);
    
    int mid = (l + p) / 2;
    update(2 * v, l, mid);
    update(2 * v + 1, mid + 1, p);
    
    tree[v][depth].fi = min(tree[2 * v][depth].fi, tree[2 * v + 1][depth].fi);
    tree[v][depth].sec = max(tree[2 * v][depth].sec, tree[2 * v + 1][depth].sec);
}

pair<int, int> query(int v, int l, int p){
    if(p < a || b < l){
        return {1e9, -1e9};
    }
    
    if(a <= l && p <= b){
        return tree[v][depth];
    }
    
    pchnij(v, depth);
    
    int mid = (l + p) / 2;
    pair<int, int> lewo = query(2 * v, l, mid);
    pair<int, int> prawo = query(2 * v + 1, mid + 1, p);
    
    return {min(lewo.fi, prawo.fi), max(lewo.sec, prawo.sec)};
}

pair<int, int> q2(){
    a += BASE;
    a--;
    b += BASE;
    b++;
    
    pair<int, int> res = {1e9, -1e9};
    while(a / 2 != b / 2){
        if(a % 2 == 0){
            res.fi = min(res.fi, tree[a + 1][depth].fi);
            res.sec = max(res.sec, tree[a + 1][depth].sec);
        }
        
        if(b & 1){
            res.fi = min(res.fi, tree[b- 1][depth].fi);
            res.sec = max(res.sec, tree[b - 1][depth].sec);
        }
        
        a = a / 2;
        b = b / 2;
    }
   // cerr << res.first << ' ' << res.second << '\n';
    return res;
}
 
void upd(int v){
    v += BASE;
    while(v >= 1){
        tree[v][depth].fi = min(tree[v][depth].fi, a);
        tree[v][depth].sec = max(tree[v][depth].sec, b);
        v /= 2;
    }
}
 
void getAns(int start, int cel){
    if(start == cel){
        cout << 0 << '\n';
        return;
    }
    
    int ans = 0;
    pair<int, int> akt = {start, start};
    for(int i = MAXK - 1; i >= 0; i--){
        depth = i;
        a = akt.fi;
        b = akt.sec;
        pair<int, int> zapyt = q2();
        if(cel < zapyt.fi || cel > zapyt.sec){
            if(i == MAXK - 1){
                cout << -1 << '\n';
                return;
            }
            ans = ans + (1 << i);
            akt = zapyt;
        }
    }
    
    cout << ans + 1 << '\n';
}
 
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    
    cin >> n >> k;
    
    init();
    int m;
 
    cin >> m;
    int x, l;
    for(int i = 1; i <= m; i++){
        cin >> x >> l;
        //x--;
       // l--;
        if(x < l){
            depth = 0;
            a = x;
            b = min(x + k - 1, l);
            val = l;
            update(1, 0, BASE - 1);
        }else{
            depth = 0;
            a = max(x - k + 1, l);
            b = x;
            val = l;
            update(1, 0, BASE - 1);
        }
    }

 
    for(int i = 1; i <= n; i++){
        a = i;
        b = i;
        depth = 0;
        skacz[i][0] = query(1, 0, BASE - 1);
    }

 
    for(int dep = 1; dep < MAXK; dep++){
        for(int i = 1; i <= n; i++){
            depth = dep - 1;
            a = skacz[i][dep - 1].fi;
            b = skacz[i][dep - 1].sec;
            skacz[i][dep] = q2();
            a = skacz[i][dep].fi;
            b = skacz[i][dep].sec;
            depth = dep;
            upd(i);
        }
    }

    int q;
    cin >> q;
    
    int s, t;
    for(int i = 1; i <= q; i++){
        cin >> s >> t;
       // s--;
        //t--;
        getAns(s, t);
    }
 
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 289 ms 361588 KB Output is correct
2 Correct 224 ms 361556 KB Output is correct
3 Correct 202 ms 361640 KB Output is correct
4 Correct 204 ms 361720 KB Output is correct
5 Correct 204 ms 361396 KB Output is correct
6 Correct 205 ms 361608 KB Output is correct
7 Correct 210 ms 361680 KB Output is correct
8 Correct 206 ms 361616 KB Output is correct
9 Correct 196 ms 361556 KB Output is correct
10 Correct 201 ms 361512 KB Output is correct
11 Correct 206 ms 361556 KB Output is correct
12 Correct 204 ms 361552 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 289 ms 361588 KB Output is correct
2 Correct 224 ms 361556 KB Output is correct
3 Correct 202 ms 361640 KB Output is correct
4 Correct 204 ms 361720 KB Output is correct
5 Correct 204 ms 361396 KB Output is correct
6 Correct 205 ms 361608 KB Output is correct
7 Correct 210 ms 361680 KB Output is correct
8 Correct 206 ms 361616 KB Output is correct
9 Correct 196 ms 361556 KB Output is correct
10 Correct 201 ms 361512 KB Output is correct
11 Correct 206 ms 361556 KB Output is correct
12 Correct 204 ms 361552 KB Output is correct
13 Correct 216 ms 361832 KB Output is correct
14 Correct 207 ms 361808 KB Output is correct
15 Correct 212 ms 361808 KB Output is correct
16 Correct 206 ms 361808 KB Output is correct
17 Correct 203 ms 361804 KB Output is correct
18 Correct 213 ms 361940 KB Output is correct
19 Correct 209 ms 361828 KB Output is correct
20 Correct 195 ms 361808 KB Output is correct
21 Correct 200 ms 361880 KB Output is correct
22 Correct 202 ms 361812 KB Output is correct
23 Correct 207 ms 361912 KB Output is correct
24 Correct 212 ms 361828 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 616 ms 378972 KB Output is correct
2 Correct 624 ms 379124 KB Output is correct
3 Correct 680 ms 379220 KB Output is correct
4 Correct 585 ms 378728 KB Output is correct
5 Correct 693 ms 380240 KB Output is correct
6 Correct 675 ms 380500 KB Output is correct
7 Correct 702 ms 380248 KB Output is correct
8 Correct 610 ms 379216 KB Output is correct
9 Correct 622 ms 378960 KB Output is correct
10 Correct 644 ms 380244 KB Output is correct
11 Correct 652 ms 380484 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 634 ms 379676 KB Output is correct
2 Correct 724 ms 380844 KB Output is correct
3 Correct 704 ms 379940 KB Output is correct
4 Correct 698 ms 380752 KB Output is correct
5 Correct 730 ms 380752 KB Output is correct
6 Correct 728 ms 380840 KB Output is correct
7 Correct 697 ms 380832 KB Output is correct
8 Correct 724 ms 380972 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 747 ms 381040 KB Output is correct
2 Correct 706 ms 380124 KB Output is correct
3 Correct 674 ms 379300 KB Output is correct
4 Correct 672 ms 379432 KB Output is correct
5 Correct 534 ms 378704 KB Output is correct
6 Correct 632 ms 378748 KB Output is correct
7 Correct 672 ms 379952 KB Output is correct
8 Correct 202 ms 361552 KB Output is correct
9 Correct 204 ms 362128 KB Output is correct
10 Correct 751 ms 380408 KB Output is correct
11 Correct 791 ms 381040 KB Output is correct
12 Correct 694 ms 381012 KB Output is correct
13 Correct 678 ms 383144 KB Output is correct
14 Correct 88 ms 363348 KB Output is correct
15 Correct 96 ms 365984 KB Output is correct
16 Correct 553 ms 382372 KB Output is correct
17 Correct 660 ms 383064 KB Output is correct
18 Correct 643 ms 382864 KB Output is correct
19 Correct 633 ms 383064 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 289 ms 361588 KB Output is correct
2 Correct 224 ms 361556 KB Output is correct
3 Correct 202 ms 361640 KB Output is correct
4 Correct 204 ms 361720 KB Output is correct
5 Correct 204 ms 361396 KB Output is correct
6 Correct 205 ms 361608 KB Output is correct
7 Correct 210 ms 361680 KB Output is correct
8 Correct 206 ms 361616 KB Output is correct
9 Correct 196 ms 361556 KB Output is correct
10 Correct 201 ms 361512 KB Output is correct
11 Correct 206 ms 361556 KB Output is correct
12 Correct 204 ms 361552 KB Output is correct
13 Correct 216 ms 361832 KB Output is correct
14 Correct 207 ms 361808 KB Output is correct
15 Correct 212 ms 361808 KB Output is correct
16 Correct 206 ms 361808 KB Output is correct
17 Correct 203 ms 361804 KB Output is correct
18 Correct 213 ms 361940 KB Output is correct
19 Correct 209 ms 361828 KB Output is correct
20 Correct 195 ms 361808 KB Output is correct
21 Correct 200 ms 361880 KB Output is correct
22 Correct 202 ms 361812 KB Output is correct
23 Correct 207 ms 361912 KB Output is correct
24 Correct 212 ms 361828 KB Output is correct
25 Correct 616 ms 378972 KB Output is correct
26 Correct 624 ms 379124 KB Output is correct
27 Correct 680 ms 379220 KB Output is correct
28 Correct 585 ms 378728 KB Output is correct
29 Correct 693 ms 380240 KB Output is correct
30 Correct 675 ms 380500 KB Output is correct
31 Correct 702 ms 380248 KB Output is correct
32 Correct 610 ms 379216 KB Output is correct
33 Correct 622 ms 378960 KB Output is correct
34 Correct 644 ms 380244 KB Output is correct
35 Correct 652 ms 380484 KB Output is correct
36 Correct 634 ms 379676 KB Output is correct
37 Correct 724 ms 380844 KB Output is correct
38 Correct 704 ms 379940 KB Output is correct
39 Correct 698 ms 380752 KB Output is correct
40 Correct 730 ms 380752 KB Output is correct
41 Correct 728 ms 380840 KB Output is correct
42 Correct 697 ms 380832 KB Output is correct
43 Correct 724 ms 380972 KB Output is correct
44 Correct 747 ms 381040 KB Output is correct
45 Correct 706 ms 380124 KB Output is correct
46 Correct 674 ms 379300 KB Output is correct
47 Correct 672 ms 379432 KB Output is correct
48 Correct 534 ms 378704 KB Output is correct
49 Correct 632 ms 378748 KB Output is correct
50 Correct 672 ms 379952 KB Output is correct
51 Correct 202 ms 361552 KB Output is correct
52 Correct 204 ms 362128 KB Output is correct
53 Correct 751 ms 380408 KB Output is correct
54 Correct 791 ms 381040 KB Output is correct
55 Correct 694 ms 381012 KB Output is correct
56 Correct 678 ms 383144 KB Output is correct
57 Correct 88 ms 363348 KB Output is correct
58 Correct 96 ms 365984 KB Output is correct
59 Correct 553 ms 382372 KB Output is correct
60 Correct 660 ms 383064 KB Output is correct
61 Correct 643 ms 382864 KB Output is correct
62 Correct 633 ms 383064 KB Output is correct
63 Correct 537 ms 381580 KB Output is correct
64 Correct 617 ms 381896 KB Output is correct
65 Correct 556 ms 381576 KB Output is correct
66 Correct 575 ms 383060 KB Output is correct
67 Correct 651 ms 383056 KB Output is correct
68 Correct 589 ms 382868 KB Output is correct
69 Correct 601 ms 382844 KB Output is correct
70 Correct 549 ms 383060 KB Output is correct
71 Correct 605 ms 383056 KB Output is correct