Submission #890690

# Submission time Handle Problem Language Result Execution time Memory
890690 2023-12-21T18:31:47 Z EJIC_B_KEDAX Railway Trip 2 (JOI22_ho_t4) C++17
100 / 100
1790 ms 69952 KB
#ifdef LOCAL
    //#define _GLIBCXX_DEBUG
#endif
#include <bits/stdc++.h>
#include <random>

#ifndef LOCAL
    //#pragma GCC optimize("O3")
    //#pragma GCC optimize("Ofast")
    //#pragma GCC optimize("unroll-loops")
    #pragma GCC target("avx,avx2,bmi,bmi2,popcnt,lzcnt")
#endif
using namespace std;
using ll = long long;
using ld = long double;
#define x first
#define y second
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()

template<typename T>
struct segment_tree_default {
    std::vector<T> a{};
    void (*mrg)(T&, const T&, const T&);
    size_t size{};
    T e;
    segment_tree_default(int n, void (*merge)(T&, const T&, const T&), T neutral) {
        mrg = merge;
        e = neutral;
        size = 1;
        while (size < n) {
            size <<= 1;
        }
        a.resize(2 * size - 1, e);
    }
    segment_tree_default() = default;
    void update_element(int ind, const T& value) {
        ind += size - 1;
        a[ind] = value;
        while (ind) {
            ind = (ind - 1) / 2;
            mrg(a[ind], a[2 * ind + 1], a[2 * ind + 2]);
        }
    }
    void update_all(std::vector<T>& values) {
        while (size < values.size()) {
            size <<= 1;
        }
        a.resize(2 * size - 1);
        for (int i = size - 1; i < 2 * size - 1; i++) {
            if (i - size + 1 < values.size()) {
                a[i] = values[i - size + 1];
            } else {
                a[i] = e;
            }
        }
        for (int i = size - 2; i >= 0; i--) {
            mrg(a[i], a[2 * i + 1], a[2 * i + 2]);
        }
    }
    T get(int l, int r, int l1 = 0, int r1 = -1, int i = 0) {
        if (r1 == -1) {
            r1 += size;
        }
        if (l1 >= l && r1 <= r) {
            return a[i];
        }
        if (r1 < l || l1 > r) {
            return e;
        }
        T res;
        mrg(res, get(l, r, l1, (l1 + r1) / 2, 2 * i + 1), get(l, r, (l1 + r1) / 2 + 1, r1, 2 * i + 2));
        return res;
    }
};

void mn(int& to, const int& a, const int& b) {
    to = min(a, b);
}

void mx(int& to, const int& a, const int& b) {
    to = max(a, b);
}

struct segment {
    int v, l, r;
};

bool cmp (const segment& a, const segment& b) {
    if (a.l < b.l) {
        return true;
    }
    if (a.l > b.l) {
        return false;
    }
    return a.r < b.r;
}

bool operator < (const segment& a, const segment& b) {
    if (a.v < b.v) {
        return true;
    }
    if (a.v > b.v) {
        return false;
    }
    if (a.l < b.l) {
        return true;
    }
    if (a.l > b.l) {
        return false;
    }
    return a.r < b.r;
}

mt19937_64 mt(418);

void solve();
void init();

int32_t main() {
#ifndef LOCAL
    cin.tie(nullptr)->sync_with_stdio(false);
#endif
    // srand(time(0));
    init();
    cout << fixed << setprecision(20);
    int t = 1;
    // cin >> t;
    while (t--) {
        solve();
    }
}

const int N = 1 << 17, Q = 50500, M = 200200, LG = 17;
segment_tree_default<int> stl[LG], str[LG];
int a[M], b[M], szl = 0, szr = 0;
vector<int> start_l[N], start_r[N], end_l[N], end_r[N];
segment seg_l[M], seg_r[M];
set<segment> nwl, nwr;

void init() {
    for (int i = 0; i < LG; i++) {
        stl[i] = segment_tree_default<int>(N, mn, INT32_MAX);
        str[i] = segment_tree_default<int>(N, mx, INT32_MIN);
    }
}

void solve() {
    int n, k, m;
    cin >> n >> k >> m;
    for (int i = 0; i < m; i++) {
        cin >> a[i] >> b[i]; a[i]--; b[i]--;
    }
    for (int i = 0; i < m; i++) {
        if (a[i] < b[i]) {
            start_r[a[i]].push_back(szr);
            end_r[min(b[i], a[i] + k - 1)].push_back(szr);
            seg_r[szr++] = {-b[i], a[i], min(b[i], a[i] + k - 1)};
        } else {
            start_l[max(b[i], a[i] - k + 1)].push_back(szl);
            end_l[a[i]].push_back(szl);
            seg_l[szl++] = {b[i], max(b[i], a[i] - k + 1), a[i]};
        }
    }
    nwl.insert({n, 0, 0});
    nwr.insert({1, 0, 0});
    for (int i = 0; i < n; i++) {
        for (int j : start_l[i]) {
            nwl.insert(seg_l[j]);
        }
        // cout << min((*nwl.begin()).v, i) << ' ';
        stl[0].update_element(i, min((*nwl.begin()).v, i));
        for (int j : end_l[i]) {
            nwl.erase(seg_l[j]);
        }
    }
    // cout << '\n';
    for (int i = 0; i < n; i++) {
        for (int j : start_r[i]) {
            nwr.insert(seg_r[j]);
        }
        // cout << max(-(*nwr.begin()).v, i) << ' ';
        str[0].update_element(i, max(-(*nwr.begin()).v, i));
        for (int j : end_r[i]) {
            nwr.erase(seg_r[j]);
        }
    }
    // cout << '\n';
    // assert(n <= 10000);
    for (int l = 1; l < LG; l++) {
        for (int i = 0; i < n; i++) {
            stl[l].update_element(i, stl[l - 1].get(stl[l - 1].a[N - 1 + i], str[l - 1].a[N - 1 + i]));
            str[l].update_element(i, str[l - 1].get(stl[l - 1].a[N - 1 + i], str[l - 1].a[N - 1 + i]));
        }
    }
    // assert(n <= 10000);
    int q;
    cin >> q;
    while (q--) {
        int l, r;
        cin >> l >> r; l--; r--;
        int nw = 0, left = l, right = l;
        for (int i = LG - 1; i >= 0; i--) {
            int new_l = stl[i].get(left, right), new_r = str[i].get(left, right);
            if (r > new_r || r < new_l) {
                left = new_l;
                right = new_r;
                nw += 1 << i;
            }
        }
        int new_l = stl[0].get(left, right), new_r = str[0].get(left, right);
        // cout << nw << ' ' << new_l << ' ' << new_r << '\n';
        nw++;
        if (new_l <= r && r <= new_r) {
            cout << nw << '\n';
        } else {
            cout << "-1\n";
        }
    }
}

Compilation message

Main.cpp: In instantiation of 'segment_tree_default<T>::segment_tree_default(int, void (*)(T&, const T&, const T&), T) [with T = int]':
Main.cpp:143:60:   required from here
Main.cpp:31:21: warning: comparison of integer expressions of different signedness: 'size_t' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   31 |         while (size < n) {
      |                ~~~~~^~~
# Verdict Execution time Memory Grader output
1 Correct 24 ms 50520 KB Output is correct
2 Correct 24 ms 50268 KB Output is correct
3 Correct 25 ms 50268 KB Output is correct
4 Correct 25 ms 50264 KB Output is correct
5 Correct 30 ms 50264 KB Output is correct
6 Correct 24 ms 50128 KB Output is correct
7 Correct 23 ms 50264 KB Output is correct
8 Correct 23 ms 50200 KB Output is correct
9 Correct 28 ms 50316 KB Output is correct
10 Correct 25 ms 50336 KB Output is correct
11 Correct 24 ms 50268 KB Output is correct
12 Correct 24 ms 50268 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 24 ms 50520 KB Output is correct
2 Correct 24 ms 50268 KB Output is correct
3 Correct 25 ms 50268 KB Output is correct
4 Correct 25 ms 50264 KB Output is correct
5 Correct 30 ms 50264 KB Output is correct
6 Correct 24 ms 50128 KB Output is correct
7 Correct 23 ms 50264 KB Output is correct
8 Correct 23 ms 50200 KB Output is correct
9 Correct 28 ms 50316 KB Output is correct
10 Correct 25 ms 50336 KB Output is correct
11 Correct 24 ms 50268 KB Output is correct
12 Correct 24 ms 50268 KB Output is correct
13 Correct 58 ms 50268 KB Output is correct
14 Correct 52 ms 50424 KB Output is correct
15 Correct 36 ms 50268 KB Output is correct
16 Correct 48 ms 50268 KB Output is correct
17 Correct 61 ms 50688 KB Output is correct
18 Correct 54 ms 50660 KB Output is correct
19 Correct 49 ms 50264 KB Output is correct
20 Correct 43 ms 50268 KB Output is correct
21 Correct 32 ms 50260 KB Output is correct
22 Correct 53 ms 50268 KB Output is correct
23 Correct 47 ms 50436 KB Output is correct
24 Correct 58 ms 50420 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1080 ms 58228 KB Output is correct
2 Correct 1041 ms 59012 KB Output is correct
3 Correct 1154 ms 60384 KB Output is correct
4 Correct 1014 ms 58576 KB Output is correct
5 Correct 832 ms 65112 KB Output is correct
6 Correct 1052 ms 68872 KB Output is correct
7 Correct 690 ms 69392 KB Output is correct
8 Correct 671 ms 60576 KB Output is correct
9 Correct 628 ms 60608 KB Output is correct
10 Correct 1073 ms 64352 KB Output is correct
11 Correct 941 ms 64192 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1216 ms 60228 KB Output is correct
2 Correct 1383 ms 65892 KB Output is correct
3 Correct 1730 ms 61444 KB Output is correct
4 Correct 959 ms 69952 KB Output is correct
5 Correct 1049 ms 67152 KB Output is correct
6 Correct 1069 ms 67124 KB Output is correct
7 Correct 1455 ms 65092 KB Output is correct
8 Correct 1333 ms 65252 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1685 ms 62468 KB Output is correct
2 Correct 1790 ms 59584 KB Output is correct
3 Correct 1624 ms 55300 KB Output is correct
4 Correct 1678 ms 52616 KB Output is correct
5 Correct 1310 ms 49436 KB Output is correct
6 Correct 1543 ms 49020 KB Output is correct
7 Correct 1377 ms 62664 KB Output is correct
8 Correct 23 ms 48220 KB Output is correct
9 Correct 51 ms 48476 KB Output is correct
10 Correct 1182 ms 65812 KB Output is correct
11 Correct 1437 ms 68196 KB Output is correct
12 Correct 1538 ms 65840 KB Output is correct
13 Correct 1463 ms 68424 KB Output is correct
14 Correct 24 ms 48220 KB Output is correct
15 Correct 55 ms 48220 KB Output is correct
16 Correct 1141 ms 62036 KB Output is correct
17 Correct 1696 ms 62728 KB Output is correct
18 Correct 1655 ms 62712 KB Output is correct
19 Correct 1685 ms 62772 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 24 ms 50520 KB Output is correct
2 Correct 24 ms 50268 KB Output is correct
3 Correct 25 ms 50268 KB Output is correct
4 Correct 25 ms 50264 KB Output is correct
5 Correct 30 ms 50264 KB Output is correct
6 Correct 24 ms 50128 KB Output is correct
7 Correct 23 ms 50264 KB Output is correct
8 Correct 23 ms 50200 KB Output is correct
9 Correct 28 ms 50316 KB Output is correct
10 Correct 25 ms 50336 KB Output is correct
11 Correct 24 ms 50268 KB Output is correct
12 Correct 24 ms 50268 KB Output is correct
13 Correct 58 ms 50268 KB Output is correct
14 Correct 52 ms 50424 KB Output is correct
15 Correct 36 ms 50268 KB Output is correct
16 Correct 48 ms 50268 KB Output is correct
17 Correct 61 ms 50688 KB Output is correct
18 Correct 54 ms 50660 KB Output is correct
19 Correct 49 ms 50264 KB Output is correct
20 Correct 43 ms 50268 KB Output is correct
21 Correct 32 ms 50260 KB Output is correct
22 Correct 53 ms 50268 KB Output is correct
23 Correct 47 ms 50436 KB Output is correct
24 Correct 58 ms 50420 KB Output is correct
25 Correct 1080 ms 58228 KB Output is correct
26 Correct 1041 ms 59012 KB Output is correct
27 Correct 1154 ms 60384 KB Output is correct
28 Correct 1014 ms 58576 KB Output is correct
29 Correct 832 ms 65112 KB Output is correct
30 Correct 1052 ms 68872 KB Output is correct
31 Correct 690 ms 69392 KB Output is correct
32 Correct 671 ms 60576 KB Output is correct
33 Correct 628 ms 60608 KB Output is correct
34 Correct 1073 ms 64352 KB Output is correct
35 Correct 941 ms 64192 KB Output is correct
36 Correct 1216 ms 60228 KB Output is correct
37 Correct 1383 ms 65892 KB Output is correct
38 Correct 1730 ms 61444 KB Output is correct
39 Correct 959 ms 69952 KB Output is correct
40 Correct 1049 ms 67152 KB Output is correct
41 Correct 1069 ms 67124 KB Output is correct
42 Correct 1455 ms 65092 KB Output is correct
43 Correct 1333 ms 65252 KB Output is correct
44 Correct 1685 ms 62468 KB Output is correct
45 Correct 1790 ms 59584 KB Output is correct
46 Correct 1624 ms 55300 KB Output is correct
47 Correct 1678 ms 52616 KB Output is correct
48 Correct 1310 ms 49436 KB Output is correct
49 Correct 1543 ms 49020 KB Output is correct
50 Correct 1377 ms 62664 KB Output is correct
51 Correct 23 ms 48220 KB Output is correct
52 Correct 51 ms 48476 KB Output is correct
53 Correct 1182 ms 65812 KB Output is correct
54 Correct 1437 ms 68196 KB Output is correct
55 Correct 1538 ms 65840 KB Output is correct
56 Correct 1463 ms 68424 KB Output is correct
57 Correct 24 ms 48220 KB Output is correct
58 Correct 55 ms 48220 KB Output is correct
59 Correct 1141 ms 62036 KB Output is correct
60 Correct 1696 ms 62728 KB Output is correct
61 Correct 1655 ms 62712 KB Output is correct
62 Correct 1685 ms 62772 KB Output is correct
63 Correct 1532 ms 59060 KB Output is correct
64 Correct 1742 ms 60580 KB Output is correct
65 Correct 1666 ms 59708 KB Output is correct
66 Correct 1149 ms 65592 KB Output is correct
67 Correct 1508 ms 69712 KB Output is correct
68 Correct 1164 ms 65064 KB Output is correct
69 Correct 1028 ms 67124 KB Output is correct
70 Correct 1535 ms 65212 KB Output is correct
71 Correct 1651 ms 65100 KB Output is correct