Advertisement
tiom4eg

openol H 79/100

Jan 16th, 2023
792
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.71 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,unroll-loops") // let the chaos begin!
  29. #pragma GCC target("avx,bmi,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 ordered_set tree <int, null_type, less <int>, rb_tree_tag, tree_order_statistics_node_update>
  36. #define ook order_of_key
  37. #define fbo find_by_order
  38. using namespace __gnu_pbds;
  39. // POGGERS POGGERS POGGERS POGGERS POGGERS POGGERS POGGERS
  40. using namespace std;
  41. mt19937 rng(chrono::duration_cast<chrono::milliseconds>(chrono::system_clock::now().time_since_epoch()).count());
  42. //#define int long long
  43. const int INF = 1e9 + 100007, INFLL = 2e18 + 7, MAX = 200160, MD = 998244353, LG = 18, B = 8192, R = 1 << 18;
  44.  
  45. struct segtree {
  46.     struct node { int mn, d; };
  47.     vector <node> t;
  48.     void merge(int v) { t[v].mn = min(t[2 * v].mn, t[2 * v + 1].mn); }
  49.     void tag(int v, int x) { t[v].mn += x, t[v].d += x; }
  50.     void push(int v) {
  51.         if (v >= R || !t[v].d) return;
  52.         tag(2 * v, t[v].d), tag(2 * v + 1, t[v].d);
  53.         t[v].d = 0;
  54.     }
  55.     void build() { t.assign(2 * R, {INF, 0}); }
  56.     void zero(int p, int v = 1, int nl = 0, int nr = R) {
  57.         push(v);
  58.         if (p < nl || nr <= p) return;
  59.         if (nl + 1 == nr) {
  60.             t[v].mn = 0;
  61.             return;
  62.         }
  63.         int nm = (nl + nr) / 2;
  64.         zero(p, 2 * v, nl, nm), zero(p, 2 * v + 1, nm, nr);
  65.         merge(v);
  66.     }
  67.     void upd(int x) { t[1].d += x, push(1); }
  68.     pii get(int v = 1, int nl = 0, int nr = R) {
  69.         push(v);
  70.         if (nl + 1 == nr) return {t[v].mn, nl};
  71.         int nm = (nl + nr) / 2;
  72.         if (t[2 * v].mn < 0) return get(2 * v, nl, nm);
  73.         else return get(2 * v + 1, nm, nr);
  74.     }
  75. } t;
  76.  
  77. int a[MAX];
  78. pii up[MAX][LG];
  79.  
  80. signed main() {
  81.     fastIO;
  82.     int n; cin >> n;
  83.     vector <pii> bl;
  84.     bl.pb({-1, -1});
  85.     FOR(i, 0, n) {
  86.         cin >> a[i];
  87.         if (a[i] != bl.back().F) bl.pb({a[i], i});
  88.         else ++bl.back().S;
  89.     }
  90.     int m = sz(bl);
  91.     t.build();
  92.     RFOR(i, m - 2, 1) {
  93.         t.zero(i), t.upd((bl[i + 1].F - bl[i].F) - (bl[i + 1].S - bl[i].S));
  94.         pii cur = t.get();
  95.         if (cur.F < 0) up[i][0] = {cur.S + 1, -cur.F};
  96.     }
  97.  
  98.     FOR(k, 1, LG) FOR(i, 1, m - 1) if (up[i][k - 1].F && up[up[i][k - 1].F][k - 1].F) up[i][k] = make_pair(up[up[i][k - 1].F][k - 1].F, up[i][k - 1].S + up[up[i][k - 1].F][k - 1].S);
  99.     int q; cin >> q;
  100.     while (q--) {
  101.         int l, r; cin >> l >> r, --l, --r;
  102.         if (a[l] == a[r]) {
  103.             cout << 0 << endl;
  104.             continue;
  105.         }
  106.         int lp = lower_bound(all(bl), make_pair(a[l], l)) - bl.begin(), rp = lower_bound(all(bl), make_pair(a[r], r)) - bl.begin();
  107.         int ans = a[r] - a[l] + bl[lp].S - l;
  108.         RFOR(i, LG - 1, 0) if (up[lp][i].F && up[lp][i].F < rp) ans += up[lp][i].S, lp = up[lp][i].F;
  109.         cout << ans << endl;
  110.     }
  111. }
  112.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement