Submission #829422

# Submission time Handle Problem Language Result Execution time Memory
829422 2023-08-18T10:32:54 Z green_gold_dog Railway Trip 2 (JOI22_ho_t4) C++17
0 / 100
828 ms 129052 KB
//#pragma GCC optimize("Ofast")
//#pragma GCC target("avx,avx2,sse,sse2,sse3,ssse3,sse4,abm,popcnt,mmx")
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef double db;
typedef long double ldb;
typedef complex<double> cd;

constexpr ll INF64 = 9'000'000'000'000'000'000, INF32 = 2'000'000'000, MOD = 1'000'000'007, LOG = 18;
constexpr db PI = acos(-1);
constexpr bool IS_FILE = false, IS_TEST_CASES = false;

random_device rd;
mt19937 rnd32(rd());
mt19937_64 rnd64(rd());

template<typename T>
bool assign_max(T& a, T b) {
        if (b > a) {
                a = b;
                return true;
        }
        return false;
}

template<typename T>
bool assign_min(T& a, T b) {
        if (b < a) {
                a = b;
                return true;
        }
        return false;
}

template<typename T>
T square(T a) {
        return a * a;
}

template<>
struct std::hash<pair<ll, ll>> {
        ll operator() (pair<ll, ll> p) const {
                return ((__int128)p.first * MOD + p.second) % INF64;
        }
};

struct segment_tree_min {
        vector<ll> tree;
        ll size;
        segment_tree_min(vector<ll> arr) {
                size = 1;
                while (size < arr.size()) {
                        size *= 2;
                }
                tree.resize(size * 2, 0);
                arr.resize(size, 0);
                build(1, 0, size, arr);
        }
        void build(ll v, ll l, ll r, vector<ll>& arr) {
                if (r - l == 1) {
                        tree[v] = arr[l];
                        return;
                }
                ll mid = (l + r) / 2;
                build(v * 2, l, mid, arr);
                build(v * 2 + 1, mid, r, arr);
                tree[v] = min(tree[v * 2], tree[v * 2 + 1]);
        }
        ll get(ll l, ll r) {
                r++;
                return get(1, 0, size, l, r);
        }
        ll get(ll v, ll l, ll r, ll ql, ll qr) {
                if (ql <= l && r <= qr) {
                        return tree[v];
                }
                if (qr <= l || r <= ql) {
                        return INF32;
                }
                ll mid = (l + r) / 2;
                return min(get(v * 2, l, mid, ql, qr), get(v * 2 + 1, mid, r, ql, qr));
        }
};

struct segment_tree_max {
        vector<ll> tree;
        ll size;
        segment_tree_max(vector<ll> arr) {
                size = 1;
                while (size < arr.size()) {
                        size *= 2;
                }
                tree.resize(size * 2, 0);
                arr.resize(size, 0);
                build(1, 0, size, arr);
        }
        void build(ll v, ll l, ll r, vector<ll>& arr) {
                if (r - l == 1) {
                        tree[v] = arr[l];
                        return;
                }
                ll mid = (l + r) / 2;
                build(v * 2, l, mid, arr);
                build(v * 2 + 1, mid, r, arr);
                tree[v] = max(tree[v * 2], tree[v * 2 + 1]);
        }
        ll get(ll l, ll r) {
                r++;
                return get(1, 0, size, l, r);
        }
        ll get(ll v, ll l, ll r, ll ql, ll qr) {
                if (ql <= l && r <= qr) {
                        return tree[v];
                }
                if (qr <= l || r <= ql) {
                        return 0;
                }
                ll mid = (l + r) / 2;
                return max(get(v * 2, l, mid, ql, qr), get(v * 2 + 1, mid, r, ql, qr));
        }
};

void solve() {
        ll n, k, m;
        cin >> n >> k >> m;
        vector<vector<pair<ll, bool>>> ami(n + 1), ama(n + 1);
        vector<pair<ll, ll>> arr(n);
        for (ll i = 0; i < m; i++) {
                cin >> arr[i].first >> arr[i].second;
                arr[i].first--;
                arr[i].second--;
                if (arr[i].first < arr[i].second) {
                        ama[arr[i].first].emplace_back(arr[i].second, true);
                        ama[min(n, arr[i].first + k)].emplace_back(arr[i].second, false);
                } else {
                        ami[max(0ll, arr[i].first - k + 1)].emplace_back(arr[i].second, true);
                        ami[arr[i].first + 1].emplace_back(arr[i].second, false);
                }
        }
        multiset<ll> mi, ma;
        vector<ll> ls, rs;
        for (ll i = 0; i < n; i++) {
                mi.insert(i);
                ma.insert(i);
                for (auto[j, b] : ama[i]) {
                        if (b) {
                                ma.insert(j);
                        } else {
                                ma.erase(ma.find(j));
                        }
                }
                for (auto[j, b] : ami[i]) {
                        if (b) {
                                mi.insert(j);
                        } else {
                                mi.erase(mi.find(j));
                        }
                }
                ls.push_back(*mi.begin());
                rs.push_back(*ma.rbegin());
                mi.erase(mi.find(i));
                ma.erase(ma.find(i));
        }
        vector<vector<ll>> p2l(1, ls), p2r(1, rs);
        vector<segment_tree_min> p2mi(1, segment_tree_min(ls));
        vector<segment_tree_max> p2ma(1, segment_tree_max(rs));
        for (ll i = 0; i < LOG; i++) {
                vector<ll> nl, nr;
                for (ll j = 0; j < n; j++) {
                        nl.push_back(p2mi.back().get(p2l.back()[j], p2r.back()[j]));
                        nr.push_back(p2ma.back().get(p2l.back()[j], p2r.back()[j]));
                }
                p2l.push_back(nl);
                p2r.push_back(nr);
                p2mi.emplace_back(nl);
                p2ma.emplace_back(nr);
        }
        ll q;
        cin >> q;
        for (ll i = 0; i < q; i++) {
                ll ans = 1;
                ll s, t;
                cin >> s >> t;
                s--;
                t--;
                pair<ll, ll> no = make_pair(s, s);
                for (ll j = LOG; j >= 0; j--) {
                        if (p2mi[j].get(no.first, no.second) > t || p2ma[j].get(no.first, no.second) < t) {
                                ans += (1 << j);
                                no = make_pair(p2mi[j].get(no.first, no.second), p2ma[j].get(no.first, no.second));
                        }
                }
                if (ans == (1 << (LOG + 1))) {
                        cout << -1 << '\n';
                } else {
                        cout << ans << '\n';
                }
        }
}

int main() {
        if (IS_FILE) {
                freopen("", "r", stdin);
                freopen("", "w", stdout);
        }
        ios_base::sync_with_stdio(false);
        cin.tie(0);
        cout.tie(0);
        ll t = 1;
        if (IS_TEST_CASES) {
                cin >> t;
        }
        for (ll i = 0; i < t; i++) {
                solve();
        }
}

Compilation message

Main.cpp: In constructor 'segment_tree_min::segment_tree_min(std::vector<long long int>)':
Main.cpp:55:29: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   55 |                 while (size < arr.size()) {
      |                        ~~~~~^~~~~~~~~~~~
Main.cpp: In constructor 'segment_tree_max::segment_tree_max(std::vector<long long int>)':
Main.cpp:93:29: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   93 |                 while (size < arr.size()) {
      |                        ~~~~~^~~~~~~~~~~~
Main.cpp: In function 'int main()':
Main.cpp:206:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  206 |                 freopen("", "r", stdin);
      |                 ~~~~~~~^~~~~~~~~~~~~~~~
Main.cpp:207:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  207 |                 freopen("", "w", stdout);
      |                 ~~~~~~~^~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 724 KB Output is correct
2 Correct 3 ms 724 KB Output is correct
3 Correct 2 ms 768 KB Output is correct
4 Correct 2 ms 724 KB Output is correct
5 Correct 2 ms 724 KB Output is correct
6 Correct 2 ms 724 KB Output is correct
7 Runtime error 3 ms 1248 KB Execution killed with signal 6
8 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 724 KB Output is correct
2 Correct 3 ms 724 KB Output is correct
3 Correct 2 ms 768 KB Output is correct
4 Correct 2 ms 724 KB Output is correct
5 Correct 2 ms 724 KB Output is correct
6 Correct 2 ms 724 KB Output is correct
7 Runtime error 3 ms 1248 KB Execution killed with signal 6
8 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 628 ms 125304 KB Output is correct
2 Correct 633 ms 125196 KB Output is correct
3 Correct 664 ms 126180 KB Output is correct
4 Correct 609 ms 124812 KB Output is correct
5 Runtime error 35 ms 24364 KB Execution killed with signal 11
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 828 ms 129052 KB Output is correct
2 Runtime error 36 ms 23344 KB Execution killed with signal 11
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 52 ms 24804 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 724 KB Output is correct
2 Correct 3 ms 724 KB Output is correct
3 Correct 2 ms 768 KB Output is correct
4 Correct 2 ms 724 KB Output is correct
5 Correct 2 ms 724 KB Output is correct
6 Correct 2 ms 724 KB Output is correct
7 Runtime error 3 ms 1248 KB Execution killed with signal 6
8 Halted 0 ms 0 KB -