tiom4eg

openol A 100

Jan 16th, 2024
816
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.53 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. // tiom4eg's precompiler options
  3. // POGGERS POGGERS POGGERS POGGERS POGGERS POGGERS POGGERS
  4. // IO settings
  5. #define fastIO ios_base::sync_with_stdio(false); cin.tie(0)
  6. // Quick types
  7. #define ll long long
  8. #define ld long double
  9. //#define ull unsigned long long
  10. #define pii pair <int, int>
  11. #define vi vector <int>
  12. #define mi vector <vector <int>>
  13. // Quick functions
  14. #define endl "\n"
  15. #define F first
  16. #define S second
  17. #define all(a) a.begin(), a.end()
  18. #define sz(a) (int)(a.size())
  19. #define pb push_back
  20. #define mp make_pair
  21. // Quick fors
  22. #define FOR(i, a, b) for (int i = a; i < b; ++i)
  23. #define FORS(i, a, b, c) for (int i = a; i < b; i += c)
  24. #define RFOR(i, a, b) for (int i = a; i >= b; --i)
  25. #define EACH(e, a) for (auto& e : a)
  26. // Pragmas
  27. #ifndef TIOM4EG
  28. #pragma GCC optimize("O3") // let the chaos begin!
  29. #pragma GCC target("avx,avx2,tune=native")
  30. #pragma GCC comment(linker, "/stack:200000000")
  31. #endif
  32. // PBDS
  33. #include <ext/pb_ds/assoc_container.hpp>
  34. #include <ext/pb_ds/tree_policy.hpp>
  35. #define pbds tree <int, null_type, less <int>, rb_tree_tag, tree_order_statistics_node_update>
  36. using namespace __gnu_pbds;
  37. // POGGERS POGGERS POGGERS POGGERS POGGERS POGGERS POGGERS
  38. using namespace std;
  39. mt19937 rng(chrono::duration_cast<chrono::milliseconds>(chrono::system_clock::now().time_since_epoch()).count());
  40. //#define int long long
  41. constexpr int INF = 1e9 + 7, MD = 998244353, MAX = 200007, LG = 19, R = 1 << LG, MOD = 1000000007, MOD2 = 1e9 + 9, B = 256;
  42. const ll INFLL = 1e18 + 7;
  43.  
  44. int t[2 * R];
  45. void upd(int l, int r) {
  46.     for (l += R, r += R; l < r; l >>= 1, r >>= 1) {
  47.         if (l & 1) t[l++]++;
  48.         if (~r & 1) t[r--]++;
  49.     }
  50.     if (l == r) t[l]++;
  51. }
  52. int get(int p) {
  53.     int s = 0;
  54.     for (p += R; p; s += t[p], p >>= 1) {}
  55.     return s;
  56. }
  57.  
  58. signed main() {
  59.     fastIO;
  60.     int n, m; cin >> n >> m;
  61.     vi a(n); EACH(e, a) cin >> e;
  62.     vector <pii> qs;
  63.     FOR(i, 0, n - 1) if (a[i + 1] - a[i] > m) {
  64.         if (a[i + 1] - a[i] >= 2 * m) return !(cout << -1);
  65.         int l = (a[i] + 1) % m, r = (a[i + 1] - m + 1) % m;
  66.         if (l < r) qs.pb({l, r});
  67.         else {
  68.             qs.pb({l, m});
  69.             if (r) qs.pb({0, r});
  70.         }
  71.     }
  72.     vi xs;
  73.     xs.pb(0), xs.pb(m);
  74.     EACH(e, qs) xs.pb(e.F), xs.pb(e.S);
  75.     sort(all(xs)), xs.erase(unique(all(xs)), xs.end());
  76.     EACH(e, qs) upd(lower_bound(all(xs), e.F) - xs.begin(), lower_bound(all(xs), e.S) - (xs.begin() + 1));
  77.     FOR(i, 0, sz(xs) - 1) if (!get(i)) return !(cout << xs[i]);
  78.     cout << -1;
  79. }
  80.  
Advertisement
Add Comment
Please, Sign In to add comment