Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- // tiom4eg's precompiler options
- // POGGERS POGGERS POGGERS POGGERS POGGERS POGGERS POGGERS
- // IO settings
- #define fastIO ios_base::sync_with_stdio(false); cin.tie(0)
- // Quick types
- #define ll long long
- #define ld long double
- //#define ull unsigned long long
- #define pii pair <int, int>
- #define vi vector <int>
- #define mi vector <vector <int>>
- // Quick functions
- #define endl "\n"
- #define F first
- #define S second
- #define all(a) a.begin(), a.end()
- #define sz(a) (int)(a.size())
- #define pb push_back
- #define mp make_pair
- // Quick fors
- #define FOR(i, a, b) for (int i = a; i < b; ++i)
- #define FORS(i, a, b, c) for (int i = a; i < b; i += c)
- #define RFOR(i, a, b) for (int i = a; i >= b; --i)
- #define EACH(e, a) for (auto& e : a)
- // Pragmas
- #ifndef TIOM4EG
- #pragma GCC optimize("O3") // let the chaos begin!
- #pragma GCC target("avx,avx2,tune=native")
- #pragma GCC comment(linker, "/stack:200000000")
- #endif
- // PBDS
- #include <ext/pb_ds/assoc_container.hpp>
- #include <ext/pb_ds/tree_policy.hpp>
- #define pbds tree <int, null_type, less <int>, rb_tree_tag, tree_order_statistics_node_update>
- using namespace __gnu_pbds;
- // POGGERS POGGERS POGGERS POGGERS POGGERS POGGERS POGGERS
- using namespace std;
- mt19937 rng(chrono::duration_cast<chrono::milliseconds>(chrono::system_clock::now().time_since_epoch()).count());
- //#define int long long
- constexpr int INF = 1e9 + 7, MD = 998244353, MAX = 200007, LG = 19, R = 1 << LG, MOD = 1000000007, MOD2 = 1e9 + 9, B = 256;
- const ll INFLL = 1e18 + 7;
- int t[2 * R];
- void upd(int l, int r) {
- for (l += R, r += R; l < r; l >>= 1, r >>= 1) {
- if (l & 1) t[l++]++;
- if (~r & 1) t[r--]++;
- }
- if (l == r) t[l]++;
- }
- int get(int p) {
- int s = 0;
- for (p += R; p; s += t[p], p >>= 1) {}
- return s;
- }
- signed main() {
- fastIO;
- int n, m; cin >> n >> m;
- vi a(n); EACH(e, a) cin >> e;
- vector <pii> qs;
- FOR(i, 0, n - 1) if (a[i + 1] - a[i] > m) {
- if (a[i + 1] - a[i] >= 2 * m) return !(cout << -1);
- int l = (a[i] + 1) % m, r = (a[i + 1] - m + 1) % m;
- if (l < r) qs.pb({l, r});
- else {
- qs.pb({l, m});
- if (r) qs.pb({0, r});
- }
- }
- vi xs;
- xs.pb(0), xs.pb(m);
- EACH(e, qs) xs.pb(e.F), xs.pb(e.S);
- sort(all(xs)), xs.erase(unique(all(xs)), xs.end());
- EACH(e, qs) upd(lower_bound(all(xs), e.F) - xs.begin(), lower_bound(all(xs), e.S) - (xs.begin() + 1));
- FOR(i, 0, sz(xs) - 1) if (!get(i)) return !(cout << xs[i]);
- cout << -1;
- }
Advertisement
Add Comment
Please, Sign In to add comment