답안 #673898

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
673898 2022-12-22T11:09:54 Z stanislavpolyn Event Hopping (BOI22_events) C++17
10 / 100
1500 ms 2536 KB
#include <bits/stdc++.h>

#define fr(i, a, b) for (int i = (a); i <= (b); ++i)
#define rf(i, a, b) for (int i = (a); i >= (b); --i)
#define fe(x, y) for (auto& x : y)

#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define mt make_tuple

#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
#define pw(x) (1LL << (x))

using namespace std;

mt19937_64 rng(228);

#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
template <typename T>
using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define fbo find_by_order
#define ook order_of_key

template <typename T>
bool umn(T& a, T b) {
    return a > b ? a = b, 1 : 0;
}
template <typename T>
bool umx(T& a, T b) {
    return a < b ? a = b, 1 : 0;
}

using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
template <typename T>
using ve = vector<T>;

const int N = 1e5 + 5;

int n, q;
pii a[N];

ve<int> order;
ve<int> st;
int to[N];

set<pii> R;

int main() {
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#else
    ios::sync_with_stdio(0);
    cin.tie(0);
#endif

    cin >> n >> q;

    fr (i, 1, n) {
        cin >> a[i].fi >> a[i].se;
        order.pb(i);
    }

    sort(all(order), [](int i, int j) {
        return a[i].fi < a[j].fi || (a[i].fi == a[j].fi && a[i].se > a[j].se);
    });

    fe (i, order) {
        if (sz(st) && a[st.back()].se >= a[i].se) {
            to[i] = st.back();
            continue;
        }
        st.pb(i);
    }


    fr (i, 1, sz(st) - 1) {
        assert(a[st[i]].fi > a[st[i - 1]].fi);
        assert(a[st[i]].se > a[st[i - 1]].se);
    }

    {
        int mx = -1e9;
        int mxI = -1;
        int ptr = 0;

        fe (i, st) {
            while (ptr < sz(st) && a[st[ptr]].fi <= a[i].se) {
                if (umx(mx, a[st[ptr]].se)) {
                    mxI = st[ptr];
                }
                ptr++;
            }

            if (mx >= a[i].se) {
                to[i] = mxI;
            } else {
                assert(0);
                to[i] = -1;
            }
        }
    }

//    fr (i, 1, n) {
//        cout << i << " " << to[i] << "\n";
//    }
//    cout << "\n";
//    fe (x, st) {
//        cout << x << " " << a[x].fi << " " << a[x].se << "\n";
//    }

    fr (i, 1, q) {
        int s, e;
        cin >> s >> e;

        if (s == e) {
            cout << "0\n";
            continue;
        }

        if (a[s].se > a[e].se) {
            cout << "impossible\n";
            continue;
        }

        int v = s;
        int ans = 0;
        while (1) {
            if (a[to[v]].se == a[v].se || a[to[v]].se > a[e].se || a[v].se >= a[e].fi) {
                break;
            }
            v = to[v];
            ans++;
        }

        int add = 0;
        bool bad = 0;
        while (1) {
            if (a[v].se >= a[e].fi) {
                ans++;
                break;
            }

            int mx = -1e9;
            int mxI = -1;
            fr (i, 1, n) {
                if (a[i].fi <= a[v].se && a[i].se >= a[v].se) {
                    if (a[i].se <= a[e].se) {
                        if (umx(mx, a[i].se)) {
                            mxI = i;
                        }
                    }
                }
            }

            if (mx == a[v].se) {
                bad = 1;
                break;
            }
            add++;
            v = mxI;
            ans++;
        }

//        assert(add <= 2);

        if (a[v].se >= a[e].fi) {
            cout << ans << "\n";
        } else {
            cout << "impossible\n";
        }
    }

    return 0;
}

Compilation message

events.cpp: In function 'int main()':
events.cpp:144:14: warning: variable 'bad' set but not used [-Wunused-but-set-variable]
  144 |         bool bad = 0;
      |              ^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Execution timed out 1586 ms 2536 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 35 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 35 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 37 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 549 ms 852 KB Output is correct
20 Execution timed out 1593 ms 928 KB Time limit exceeded
21 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 35 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 36 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 0 ms 340 KB Output is correct
19 Correct 136 ms 2416 KB Output is correct
20 Execution timed out 1583 ms 2004 KB Time limit exceeded
21 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1574 ms 2508 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Execution timed out 1586 ms 2536 KB Time limit exceeded
3 Halted 0 ms 0 KB -