답안 #863114

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
863114 2023-10-19T15:32:59 Z 3omar_ahmed Osumnjičeni (COCI21_osumnjiceni) C++17
0 / 110
53 ms 54872 KB
#include <bits/stdc++.h>
using namespace std ;
#define int long long
#define endl '\n'
#define all(a) a.begin() , a.end()
#define alr(a) a.rbegin() , a.rend()

struct segtree {
    int sz;
    vector < int > tree, lazy;
    void init(int n) {
        sz = 1;
        while(sz <= n) sz <<= 1;
        lazy = tree = vector < int > (sz << 2, 1e9);
    }

    void prop(int node) {
        if(lazy[node] != 1e9) {
            tree[node * 2 + 1] = tree[node * 2 + 2] = lazy[node];
            lazy[node * 2 + 1] = tree[node * 2 + 2] = lazy[node];

            lazy[node] = 1e9;
        }
    }

    void update(int node, int lnode, int rnode, int l, int r, int val) {
        if(rnode <= l || r <= lnode) return;
        if(lnode >= l && rnode <= r) {
            tree[node] = lazy[node] = val;
            return;
        }
        int mid = (lnode + rnode) >> 1;
        prop(node);
        update(node * 2 + 1, lnode, mid, l, r, val);
        update(node * 2 + 2, mid, rnode, l, r, val);
        tree[node] = min(tree[node * 2 + 1], tree[node * 2 + 2]);
    }
    void update(int l, int r, int val) {
        update(0, 0, sz, l, r, val);
    }

    int query(int node, int lnode, int rnode, int l, int r) {
        if(rnode <= l || r <= lnode) return 1e9;
        if(lnode >= l && rnode <= r) {
            return tree[node];
        }
        int mid = (lnode + rnode) >> 1;
        prop(node);
        int a = query(node * 2 + 1, lnode, mid, l, r);
        int b = query(node * 2 + 2, mid, rnode, l, r);
        return min(a, b);
    }
    int query(int l, int r) {
        return query(0, 0, sz, l, r);
    }
};


signed main(){
    ios_base::sync_with_stdio(0); cin.tie(0);cout.tie(0);

    int n;
    cin >> n;
    n += 1;
    vector < pair < int , int >> a(n);
    for(int i = 0 ; i + 1 < n ; i++) {
        cin >> a[i].first >> a[i].second;
    }    
    a[n - 1] = {1, n};
    vector < vector < int >> nxt(20, vector < int > (n + 1, -1));
    nxt[0][n - 1] = n - 1;

    segtree s; s.init(n + 2);
    for(int i = n - 2 ; i >= 0 ; i--) {
        nxt[0][i] = min(nxt[0][i + 1], n - 2);
        nxt[0][i] = min(n - 2, s.query(a[i].first, a[i].second + 1));
        s.update(a[i].first, a[i].second + 1, i - 1);
    }

    for(int i = 1 ; i < 20 ; i++) {
        for(int j = 0 ; j < n ; j++) {
            if(nxt[i - 1][j] != -1)
                nxt[i][j] = nxt[i - 1][nxt[i - 1][j] + 1];
        }
    }

    int q;
    cin >> q;
    while(q--) {
        int l, r;
        cin >> l >> r;
        l -= 1, r -= 1;
        int ans = 0;
        for(int i = 19 ; i >= 0 ; i--) {
            if(nxt[i][l] <= r && nxt[i][l] != -1) {
                ans += (1ll << i);
                l = nxt[i][l] + 1;
            }
        }
        ans += (l <= r);
        cout << ans << endl;
    }
    return 0 ;
}
# 결과 실행 시간 메모리 Grader output
1 Incorrect 47 ms 53248 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 3 ms 1880 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 3 ms 1880 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 53 ms 54872 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 47 ms 53248 KB Output isn't correct
2 Halted 0 ms 0 KB -