Submission #656585

# Submission time Handle Problem Language Result Execution time Memory
656585 2022-11-08T02:07:32 Z Lobo Event Hopping (BOI22_events) C++17
25 / 100
1500 ms 12588 KB
#include<bits/stdc++.h>
using namespace std;
const long long inf = (long long) 1e18 + 10;
const int inf1 = (int) 1e9 + 10;
#define int long long
#define dbl long double
#define endl '\n'
#define sc second
#define fr first
#define mp make_pair
#define pb push_back
#define all(x) x.begin(), x.end()

const int maxn = 2e5+10;

int n, q, idord[maxn], lf[maxn], rg[maxn], nx[maxn][25], pv[maxn][25];
pair<int,int> trmn[4*maxn],trmx[4*maxn];

void buildmx(int no, int l, int r) {
    trmx[no] = mp(-inf,0);
    if(l == r) return;
    int lc=2*no,rc=2*no+1,mid=(l+r)/2;
    buildmx(lc,l,mid);
    buildmx(rc,mid+1,r);
}
void updmx(int no, int l, int r, int pos, pair<int,int> val) {
    if(l > pos || r < pos) return;
    if(l == r) {
        trmx[no] = max(trmx[no],val);
        return;
    }
    int lc=2*no,rc=2*no+1,mid=(l+r)/2;
    updmx(lc,l,mid,pos,val);
    updmx(rc,mid+1,r,pos,val);
    trmx[no] = max(trmx[lc],trmx[rc]);
}

pair<int,int> qrymx(int no, int l, int r, int L, int R) {
    if(l > R || r < L) return mp(-inf,0);
    if(l >= L && r <= R) return trmx[no];
    int lc=2*no,rc=2*no+1,mid=(l+r)/2;
    return max(qrymx(lc,l,mid,L,R),qrymx(rc,mid+1,r,L,R));
}

void buildmn(int no, int l, int r) {
    trmn[no] = mp(inf,0);
    if(l == r) return;
    int lc=2*no,rc=2*no+1,mid=(l+r)/2;
    buildmn(lc,l,mid);
    buildmn(rc,mid+1,r);
}
void updmn(int no, int l, int r, int pos, pair<int,int> val) {
    if(l > pos || r < pos) return;
    if(l == r) {
        trmn[no] = min(trmn[no],val);
        return;
    }
    int lc=2*no,rc=2*no+1,mid=(l+r)/2;
    updmn(lc,l,mid,pos,val);
    updmn(rc,mid+1,r,pos,val);
    trmn[no] = min(trmn[lc],trmn[rc]);
}

pair<int,int> qrymn(int no, int l, int r, int L, int R) {
    if(l > R || r < L) return mp(inf,0);
    if(l >= L && r <= R) return trmn[no];
    int lc=2*no,rc=2*no+1,mid=(l+r)/2;
    return min(qrymn(lc,l,mid,L,R),qrymn(rc,mid+1,r,L,R));
}

void solve() {
    cin >> n >> q;
    vector<pair<pair<int,int>,int>> ord;
    vector<int> cc;
    for(int i = 1; i <= n; i++) {
        int l, r; cin >> l >> r;
        ord.pb(mp(mp(r,l),i));
        lf[i] = l;
        rg[i] = r;
        cc.pb(lf[i]);
        cc.pb(rg[i]);
    }
    sort(all(cc));
    cc.erase(unique(all(cc)),cc.end());
    int mxv = cc.size();
    for(int i = 1; i <= n; i++) {
        lf[i] = upper_bound(all(cc),lf[i])-cc.begin();
        rg[i] = upper_bound(all(cc),rg[i])-cc.begin();
    }
    // sort(all(ord));
    buildmx(1,1,mxv);
    for(int i = 1; i <= n; i++) {
        updmx(1,1,mxv,lf[i],mp(rg[i],i));
    }

    for(int i = 1; i <= n; i++) {
        nx[i][0] = 0;

        // for(int j = 1; j <= n; j++) {
        //     if(lf[j] <= rg[i] && rg[j] > rg[i]) {
        //         if(nx[i][0] == 0 || rg[j] > rg[nx[i][0]]) nx[i][0] = j;
        //     }
        // }
        auto valnx = qrymx(1,1,mxv,1,rg[i]);
        if(valnx.fr == rg[i]) nx[i][0] = 0;
        else nx[i][0] = valnx.sc;
        pv[i][0] = 0;
        for(int j = 1; j <= n; j++) {
            if(j == i || !(lf[i] <= rg[j] && rg[j] <= rg[i]) || !(lf[j] < lf[i])) continue;
            if(pv[i][0] == 0 || lf[j] < lf[pv[i][0]]) pv[i][0] = j;
        }

        // cout << i << " " << nx[i][0] << " " << pv[i][0] << endl;
    }

    for(int j = 1; j <= 20; j++) {
        for(int i = 1; i <= n; i++) {
            nx[i][j] = nx[nx[i][j-1]][j-1];
            pv[i][j] = pv[pv[i][j-1]][j-1];
        }
    }

    while(q--) {
        int s,e; cin >> s >> e;

        int ans = 0;
        for(int j = 20; j >= 0; j--) {
            if(nx[s][j] != 0 && rg[nx[s][j]] < rg[e]) {
                ans+= (1<<j);
                s = nx[s][j];
            }
        }
        for(int j = 20; j >= 0; j--) {
            if(pv[e][j] != 0 && lf[pv[e][j]] > lf[s]) {
                ans+= (1<<j);
                e = pv[e][j];
            }
        }
        if(s == e) {
            cout << ans << endl;
        }
        else if(lf[e] <= rg[s] && rg[s] <= rg[e]) {
            cout << ans+1 << endl;
        }
        else if(nx[s][0] != 0 && lf[e] <= rg[nx[s][0]] && rg[nx[s][0]] <= rg[e]) {
            cout << ans+2 << endl;
        }
        else if(pv[e][0] != 0 && lf[pv[e][0]] <= rg[s] && rg[s] <= rg[pv[e][0]]) {
            cout << ans+2 << endl;
        }
        else {
            cout << "impossible" << endl;
        }
    }
}

int32_t main() {
    ios::sync_with_stdio(false); cin.tie(0);

    // freopen("in.in", "r", stdin);
    // freopen("out.out", "w", stdout);
    int tt = 1;
    // cin >> tt;
    while(tt--) {
        solve();
    }

}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Execution timed out 1591 ms 12076 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 328 KB Output is correct
3 Correct 4 ms 856 KB Output is correct
4 Correct 5 ms 860 KB Output is correct
5 Correct 4 ms 852 KB Output is correct
6 Correct 5 ms 852 KB Output is correct
7 Correct 7 ms 852 KB Output is correct
8 Correct 5 ms 852 KB Output is correct
9 Correct 2 ms 724 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 328 KB Output is correct
3 Correct 4 ms 856 KB Output is correct
4 Correct 5 ms 860 KB Output is correct
5 Correct 4 ms 852 KB Output is correct
6 Correct 5 ms 852 KB Output is correct
7 Correct 7 ms 852 KB Output is correct
8 Correct 5 ms 852 KB Output is correct
9 Correct 2 ms 724 KB Output is correct
10 Correct 0 ms 328 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 4 ms 796 KB Output is correct
13 Correct 4 ms 860 KB Output is correct
14 Correct 4 ms 852 KB Output is correct
15 Correct 5 ms 852 KB Output is correct
16 Correct 6 ms 852 KB Output is correct
17 Correct 4 ms 852 KB Output is correct
18 Correct 2 ms 728 KB Output is correct
19 Correct 145 ms 3844 KB Output is correct
20 Correct 110 ms 3648 KB Output is correct
21 Correct 118 ms 3888 KB Output is correct
22 Correct 121 ms 4012 KB Output is correct
23 Correct 158 ms 4048 KB Output is correct
24 Correct 118 ms 4100 KB Output is correct
25 Correct 112 ms 3764 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 328 KB Output is correct
3 Correct 4 ms 856 KB Output is correct
4 Correct 5 ms 860 KB Output is correct
5 Correct 4 ms 852 KB Output is correct
6 Correct 5 ms 852 KB Output is correct
7 Correct 7 ms 852 KB Output is correct
8 Correct 5 ms 852 KB Output is correct
9 Correct 2 ms 724 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 4 ms 852 KB Output is correct
13 Correct 4 ms 852 KB Output is correct
14 Correct 4 ms 852 KB Output is correct
15 Correct 5 ms 852 KB Output is correct
16 Correct 6 ms 852 KB Output is correct
17 Correct 4 ms 852 KB Output is correct
18 Correct 2 ms 724 KB Output is correct
19 Execution timed out 1563 ms 12588 KB Time limit exceeded
20 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1582 ms 12116 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Execution timed out 1591 ms 12076 KB Time limit exceeded
3 Halted 0 ms 0 KB -