Submission #878597

# Submission time Handle Problem Language Result Execution time Memory
878597 2023-11-24T21:51:31 Z Tymond Railway Trip 2 (JOI22_ho_t4) C++17
0 / 100
552 ms 451892 KB
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define sec second

const int BASE = 1024 * 1024;
const int MAXK = 21;

// 0 -> max, 1 -> min
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 = BASE; i < BASE + n; i++){
        for(int j = 0; j < MAXK; j++){
            tree[i][j] = {i - BASE, i - BASE};
        }
    }
    
    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, 0};
    }
    
    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);
    
    pair<int, int> ret = {min(lewo.fi, prawo.fi), max(lewo.sec, prawo.sec)};
    
    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);
    
    return ret;
}

pair<int, int> q2(){
    a = a + BASE;
    b = b + BASE;
    
    pair<int, int> ret = {1e9, -1e9};
    while(a / 2 != b / 2){
        if(!(a & 1)){
            ret.fi = min(ret.fi, tree[a + 1][depth].fi);
            ret.sec = max(ret.sec, tree[a + 1][depth].sec);
        }
        
        if(b & 1){
            ret.fi = min(ret.fi, tree[b - 1][depth].fi);
            ret.sec = max(ret.sec, tree[b - 1][depth].sec);
        }
        
        a = a / 2;
        b = b / 2;
    }
    
    return ret;
}

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){
            ans = ans + (1 << i);
            akt.fi = min(zapyt.fi, akt.fi);
            akt.sec = max(akt.sec, zapyt.sec);
        }
    }
    
    if(cel > akt.second || cel < akt.first){
        cout << -1 << '\n';
        return;
    }
    
    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;
        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;
        skacz[i][0] = query(1, 0, BASE - 1);
    }

    for(int dep = 1; dep < MAXK; dep++){
        for(int i = 1; i <= n; i++){
            depth = dep;
            a = skacz[i][dep - 1].fi;
            b = skacz[i][dep - 1].sec;
            skacz[i][dep] = q2();
        }
      /*  cout << dep << ' ' << (1 << dep) << '\n';
        for(int i = 1; i <= n; i++){
            a = i;
            b = i;
            depth = dep;
            pair<int, int> akt = query(1, 0, BASE - 1);
            cout << akt.fi << ' ' << akt.sec << '\n';
        }
        cout << "----\n";*/
    }
    
    int q;
    cin >> q;
    
    int s, t;
    for(int i = 1; i <= q; i++){
        cin >> s >> t;
        
        getAns(s, t);
    }

    return 0;
}
# Verdict Execution time Memory Grader output
1 Runtime error 329 ms 384076 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 329 ms 384076 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 421 ms 450456 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 436 ms 451036 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 552 ms 451892 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 329 ms 384076 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -