Submission #697796

# Submission time Handle Problem Language Result Execution time Memory
697796 2023-02-11T06:00:10 Z vjudge1 Event Hopping (BOI22_events) C++14
25 / 100
1500 ms 52168 KB
#include <bits/stdc++.h>
#define ll long long
#define pii pair <ll, ll>
#define st first
#define nd second
#define rep(i, n, m) for (ll i = (n); i <= (m); i ++)
#define rrep(i, n, m) for (ll i = (n); i >= (m); i --)
using namespace std;

const long long INF = 1e18;
const long long N = 1e5 + 5;

ll n, q, s[N], e[N];
vector <int> cmp, out[2 * N], in[2 * N];
vector <int> d[N];

const ll Nmax = 5005;
int dist[Nmax][Nmax];
void bfs(int st, int *f) {
    rep(i, 1, n) f[i] = 1e9;
    f[st] = 0;
    queue <int> q;
    q.push(st);
    while (q.size()) {
        int u = q.front(); q.pop();
        for (int v: d[u]) if (f[v] > f[u] + 1) {
            f[v] = f[u] + 1;
            q.push(v);
        }
    }
}

namespace sub4 {

    pii a[N];
    ll pos[N], f[N];
    void solve() {
        rep(i, 1, n) a[i] = {e[i], i};
        sort(a + 1, a + 1 + n);
        rep(i, 1, n) pos[a[i].nd] = i;
        while (q --) {
            int u, v;
            cin >> u >> v;
            int i = pos[u];
            int j = pos[v];
            int l = j;

            if (i > j && a[j].st < a[i].st) {
                cout << "impossible\n";
                continue;
            }

            if (i > j) {
                cout << 1 << '\n';
                continue;
            }

            while (l + 1 <= n && a[l + 1].st == a[j].st) l ++;

            f[l + 1] = 1e9;
            //cout << i << ' ' << j << '\n';
            rrep(k, l, i) {
                int id = a[k].nd;
                f[k] = min(f[k + 1], s[id]);
            }
            int ans = 0;
            while (i < j) {
                ans ++;
                int cur = a[i].st, pre = i;
                while (i < l && cur >= f[i + 1]) {
                    i ++;
                }
                if (i > j) {
                    int id = a[j].nd;
                    if (cur >= s[v] && cur <= e[v])
                        break;
                    ans ++;
                    break;
                }
                if (i == pre) break;

            }

            //cout << i << ' ' << j << '\n';

            if (i >= j)
                cout << ans << '\n';
            else
                cout << "impossible\n";
        }
    }
}

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

    cin >> n >> q;
    rep(i, 1, n) {
        cin >> s[i] >> e[i];
        cmp.push_back(s[i]);
        cmp.push_back(e[i]);
    }

    sort(cmp.begin(), cmp.end());
    cmp.resize(unique(cmp.begin(), cmp.end()) - cmp.begin());

    auto find = [&] (ll x) {
        return lower_bound(cmp.begin(), cmp.end(), x) - cmp.begin() + 1;
    };

    rep(i, 1, n) {
        s[i] = find(s[i]);
        e[i] = find(e[i]);
        in[s[i]].push_back(i);
        out[e[i]].push_back(i);
    }

    if (q <= 100) {
        sub4 :: solve();
        return 0;
    }

    set <int> save;
    rep(timer, 1, 2 * n) {
        for (int i: in[timer]) save.insert(i);
        for (int i: out[timer]) {
            for (int j: save)
                d[i].push_back(j);
        }

        for (int i: out[timer])
            save.erase(i);
    }

    rep(i, 1, n) bfs(i, dist[i]);

    while (q --) {
        int u, v;
        cin >> u >> v;
        if (dist[u][v] >= (int)1e9)
            cout << "impossible\n";
        else
            cout << dist[u][v] << '\n';
    }

    return 0;
}

Compilation message

events.cpp: In function 'void sub4::solve()':
events.cpp:74:25: warning: unused variable 'id' [-Wunused-variable]
   74 |                     int id = a[j].nd;
      |                         ^~
# Verdict Execution time Memory Grader output
1 Correct 7 ms 11988 KB Output is correct
2 Execution timed out 1574 ms 45624 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 7 ms 11988 KB Output is correct
2 Correct 7 ms 11988 KB Output is correct
3 Correct 8 ms 12116 KB Output is correct
4 Correct 7 ms 12120 KB Output is correct
5 Correct 7 ms 12208 KB Output is correct
6 Correct 7 ms 12112 KB Output is correct
7 Correct 7 ms 12128 KB Output is correct
8 Correct 8 ms 12216 KB Output is correct
9 Correct 7 ms 12116 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 11988 KB Output is correct
2 Correct 7 ms 11988 KB Output is correct
3 Correct 8 ms 12116 KB Output is correct
4 Correct 7 ms 12120 KB Output is correct
5 Correct 7 ms 12208 KB Output is correct
6 Correct 7 ms 12112 KB Output is correct
7 Correct 7 ms 12128 KB Output is correct
8 Correct 8 ms 12216 KB Output is correct
9 Correct 7 ms 12116 KB Output is correct
10 Correct 6 ms 12120 KB Output is correct
11 Correct 6 ms 11988 KB Output is correct
12 Correct 7 ms 12220 KB Output is correct
13 Correct 7 ms 12212 KB Output is correct
14 Correct 7 ms 12116 KB Output is correct
15 Correct 7 ms 12116 KB Output is correct
16 Correct 7 ms 12132 KB Output is correct
17 Correct 7 ms 12116 KB Output is correct
18 Correct 7 ms 12088 KB Output is correct
19 Execution timed out 1578 ms 52168 KB Time limit exceeded
20 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 7 ms 11988 KB Output is correct
2 Correct 7 ms 11988 KB Output is correct
3 Correct 8 ms 12116 KB Output is correct
4 Correct 7 ms 12120 KB Output is correct
5 Correct 7 ms 12208 KB Output is correct
6 Correct 7 ms 12112 KB Output is correct
7 Correct 7 ms 12128 KB Output is correct
8 Correct 8 ms 12216 KB Output is correct
9 Correct 7 ms 12116 KB Output is correct
10 Correct 7 ms 11992 KB Output is correct
11 Correct 7 ms 11988 KB Output is correct
12 Correct 8 ms 12116 KB Output is correct
13 Correct 7 ms 12116 KB Output is correct
14 Correct 7 ms 12116 KB Output is correct
15 Correct 8 ms 12116 KB Output is correct
16 Correct 8 ms 12116 KB Output is correct
17 Correct 8 ms 12120 KB Output is correct
18 Correct 7 ms 12088 KB Output is correct
19 Correct 150 ms 25796 KB Output is correct
20 Correct 97 ms 25028 KB Output is correct
21 Correct 104 ms 22528 KB Output is correct
22 Correct 116 ms 25804 KB Output is correct
23 Correct 108 ms 25712 KB Output is correct
24 Correct 149 ms 25668 KB Output is correct
25 Correct 74 ms 19964 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 1562 ms 45304 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 7 ms 11988 KB Output is correct
2 Execution timed out 1574 ms 45624 KB Time limit exceeded
3 Halted 0 ms 0 KB -