이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define ii pair <int, int>
#define app push_back
#define all(a) a.begin(), a.end()
#define bp __builtin_popcountll
#define ll long long
#define mp make_pair
#define f first
#define s second
#define Time (double)clock()/CLOCKS_PER_SEC
#define debug(x) std::cout << #x << ": " << x << '\n';
const int N = 1e5+7, LG = 20;
int n, k, q;
int l[N][LG], r[N][LG], a[N];
signed main() {
    #ifdef HOME
    freopen("input.txt", "r", stdin);
    #else
    #define endl '\n'
    ios_base::sync_with_stdio(0); cin.tie(0);
    #endif
    cin >> n >> k >> q;
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
    }
    for (int i = 0; i < N; ++i) {
        for (int j = 0; j < LG; ++j) {
            l[i][j] = 0;
            r[i][j] = n-1;
        }   
    }   
    
    {
    vector <int> s;
    for (int i = 0; i < n; ++i) {
        while (s.size() && a[s.back()] <= a[i]) {
            r[s.back()][0] = i;
            s.pop_back();
        }   
        s.app(i);
    }
    }
    
    {
    vector <int> s;
    for (int i = n - 1; i >= 0; --i) {
        while (s.size() && a[s.back()] <= a[i]) {
            l[s.back()][0] = i;
            s.pop_back();
        }   
        s.app(i);
    }   
    }
    
    for (int bit = 1; bit < LG; ++bit) {
        for (int i = 0; i < n; ++i) {
            l[i][bit] = min(l[l[i][bit-1]][bit-1], l[r[i][bit-1]][bit-1]);
            r[i][bit] = max(r[l[i][bit-1]][bit-1], r[r[i][bit-1]][bit-1]);
        }   
    }   
    while (q--) {
        int u, v;
        cin >> u >> v;
        --u; --v;
        if (v < u) {
            swap(u, v);
        }
        //debug(u);
        //debug(v);
        int ans = 0;
        int lu = u, ru = u;
        for (int i = LG - 1; i >= 0; --i) {
            int l1 = min(l[lu][i], l[ru][i]);
            int r1 = max(r[lu][i], r[ru][i]);
            if (r1 < v) {
                ans += 1 << i;
                lu = l1;
                ru = r1;
            }   
        }   
        //debug(lu);
        //debug(ru);
        //debug(ans);
        int lv = v, rv = v;
        for (int i = LG - 1; i >= 0; --i) {
            int l1 = min(l[lv][i], l[rv][i]);
            int r1 = max(r[lv][i], r[rv][i]);
            if (l1 > ru) {                      
                ans += 1 << i;
                lv = l1;
                rv = r1;
            }   
        }   
        cout << ans << 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... |