This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <iostream>
#include <stack>
using namespace std;
const int MAX_TRAJET = 2e5+42, MAX_POW = 19, MAX_FEUILLE = 262144, INF = 1e9;
int nbPos, distMax;
int nbTrajet;
int arbre[MAX_FEUILLE*2][2][MAX_POW];
int get(int deb, int fin, int id, int type) {
    deb += MAX_FEUILLE; fin += MAX_FEUILLE;
    int rep = INF;
    if(type == 1) rep = 0;
    while(deb <= fin) {
        if(deb % 2 == 1) {
            if(type == 0) rep = min(rep, arbre[deb++][0][id]);
            else rep = max(rep, arbre[deb++][1][id]);
        }
        if(fin % 2 == 0) {
            if(type == 0) rep = min(rep, arbre[fin--][0][id]);
            else rep = max(rep, arbre[fin--][1][id]);
        }
        deb /= 2;
        fin /= 2;
    }
    return rep;
}
void update(int pos, int val, int type, int id) {
    pos += MAX_FEUILLE;
    arbre[pos][type][id] = val;
    for(pos /= 2; pos > 0; pos /= 2) {
        int gauche = arbre[pos*2][type][id];
        int droite = arbre[pos*2+1][type][id];
        if(type == 0)
            arbre[pos][type][id] = min(gauche, droite);
        else
            arbre[pos][type][id] = max(gauche, droite);
    }
}
pair<int, int> posMax[MAX_TRAJET][MAX_POW];
void toutUpdate(int exp) {
    for(int pos = 0; pos < nbPos; pos++) {
        update(pos, posMax[pos][exp].first, 0, exp);
        update(pos, posMax[pos][exp].second, 1, exp);
    }
}
pair<int, int> calcInter(int exp, int deb, int fin) {
    int nouvDeb = get(deb, min(nbPos-1, fin + distMax - 1), exp, 0);
    int nouvFin = get(max(0, deb - distMax + 1), fin, exp, 1);
    return {nouvDeb, nouvFin};
}
void input() {
    for(int i = 0; i < MAX_FEUILLE*2; i++) {
        for(int j = 0; j < MAX_POW; j++) {
            arbre[i][0][j] = INF;
            arbre[i][1][j] = 0;
        }    
    }
    for(int i = 0; i < MAX_TRAJET; i++) 
        posMax[i][0].first = posMax[i][0].second = i;
    cin >> nbPos >> distMax >> nbTrajet;
    for(int i = 0; i < nbTrajet; i++) {
        int deb, fin;
        cin >> deb >> fin;
        deb--; fin--;
        if(deb <= fin) 
            posMax[deb][0].second = max(posMax[deb][0].second, fin);
        else
            posMax[deb][0].first = min(posMax[deb][0].first, fin);
    }        
    toutUpdate(0);
    for(int pos = 0; pos < nbPos; pos++) {
        posMax[pos][0].first = posMax[pos][0].second = pos;
        pair<int, int> ah = calcInter(0, pos, pos);
        posMax[pos][0].first = ah.first; posMax[pos][0].second = ah.second;
    }
}
void calcBl() {
    for(int exp = 1; exp < MAX_POW; exp++) {
        for(int pos = 0; pos < nbPos; pos++) {
            pair<int, int> ah = calcInter(exp-1, posMax[pos][exp-1].first, posMax[pos][exp-1].second);
            posMax[pos][exp].first = ah.first; posMax[pos][exp].second = ah.second;
        }
        toutUpdate(exp);
    }
}
int req(int depart, int arrive) {
    int deb = depart, fin = depart;
    int rep = 0;
    for(int exp = MAX_POW-1; exp >= 0; exp--) {
        pair<int, int> nouv = calcInter(exp, deb, fin);
        if(arrive > nouv.second || arrive < nouv.first) {
            rep += (1 << exp);
            deb = nouv.first; fin = nouv.second;
        }
    }
    if(rep+1 == (1 << MAX_POW))
        return -1;
    return rep+1;
}
int nbReq;
int main() {
    input();
    calcBl();
    cin >> nbReq;
    for(int i = 0; i < nbReq; i++) {
        int a,b;
        cin >> a >> b;
        cout << req(a-1, b-1) << endl;
    }
}
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... |