Submission #810314

# Submission time Handle Problem Language Result Execution time Memory
810314 2023-08-06T08:22:43 Z phoenix Railway Trip 2 (JOI22_ho_t4) C++17
0 / 100
2000 ms 30264 KB
#include<bits/stdc++.h>

using namespace std;
using ll = long long;

const int N = 1e5 + 10;

template<typename T> 
struct SparseTable {
    vector<vector<T>> st;
    T (*F) (T, T);
    int len;
    void init(vector<T> &a, T(*f)(T, T)) {
        len = (int)a.size();
        int mxpow = 32 - __builtin_clz(len);
        F = f;
        st.resize(mxpow);
        st[0] = a;
        for(int k = 1; k < mxpow; k++) {
            st[k].resize(len - (1 << k) + 1);
            for(int i = 0; i < (int)st[k].size(); i++) 
                st[k][i] = F(st[k - 1][i], st[k - 1][i + (1 << (k - 1))]);
        }
    }
    T get(int l, int r) {
        if(l > r || l < 0 || r >= len) {
            cerr << "Wrong range in Sparse Table\n";
            exit(0);
        }
        int k = 31 - __builtin_clz(r - l + 1);
        return F(st[k][l], st[k][r - (1 << k) + 1]);
    }
};

int n, k, m;
int A[N], B[N];
int rb[N];
SparseTable<pair<int, int>> st;

void calculate() {
    static vector<int> op[N], cl[N];
    for(int i = 0; i < m; i++) {
        op[A[i]].push_back(B[i]);
        cl[min(B[i] + 1, A[i] + k)].push_back(B[i]);
    }
    multiset<int> ms;
    for(int i = 1; i <= n; i++) {
        for(int x : cl[i]) 
            ms.erase(ms.find(x));
        for(int x : op[i]) 
            ms.insert(x);
        rb[i] = (ms.empty() ? i : *(--ms.end()));
    }
    vector<pair<int, int>> t(n + 1);
    for(int i = 1; i <= n; i++) {
        t[i] = {rb[i], i};
    }
    st.init(t, [](pair<int, int> x, pair<int, int> y) {return (x > y ? x : y);});
}

int f[N][20];
void build() {
    for(int i = n; i >= 1; i--) {
        f[i][0] = i;
        for(int k = 1; k < 20; k++) {
            int p = st.get(f[i][k - 1], rb[f[i][k - 1]]).second;
            f[i][k] = f[p][k - 1];

        }
        // cout << i << ": ";
        // for(int k = 0; k < 4; k++) 
        //     cout << f[i][k] << ' ';
        // cout << '\n';
        // cout << '\n';
    }
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    cin >> n >> k >> m;
    for(int i = 0; i < m; i++) {
        cin >> A[i] >> B[i];
    }
    calculate();
    build();
    // for(int i = 1; i <= n; i++) 
    //     cout << rb[i] << ' ';
    // cout << '\n';
    // return 0;
    int q;
    cin >> q;
    while(q--) {
        int s, t, ans = 0;
        cin >> s >> t;
        for(int k = 19; k >= 0; k--) {
            if(rb[f[s][k]] < t) s = f[f[s][k]][1], ans += (1 << k);
        }
        // s = f[s][1], ans++;
        // cout << s << ' ' << ans << '\n';
        if(rb[s] < t) ans = -1;
        else ans++;
        cout << ans << '\n';
    }
}
# Verdict Execution time Memory Grader output
1 Execution timed out 2040 ms 4948 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 2040 ms 4948 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 2050 ms 9980 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 28 ms 21468 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 98 ms 30264 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 2040 ms 4948 KB Time limit exceeded
2 Halted 0 ms 0 KB -