Advertisement
tiom4eg

2006 C

Jun 20th, 2022
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.29 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. // tiom4eg's precompiler options
  3. // POGGERS POGGERS POGGERS POGGERS POGGERS POGGERS POGGERS
  4. // Flags
  5. #define MAXMIN
  6. #define ENDL
  7. //#define AVX2
  8. //#define PBDS
  9. // IO settings
  10. #define fastIO ios_base::sync_with_stdio(false); cin.tie(0)
  11. // Quick types
  12. #define ll long long
  13. #define ld double
  14. #define ull unsigned long long
  15. #define pii pair <int, int>
  16. #define pll pair <ll, ll>
  17. #define vi vector <int>
  18. #define mi vector <vector <int> >
  19. // Quick functions
  20. #ifdef ENDL
  21. #define endl "\n"
  22. #endif
  23. #define F first
  24. #define S second
  25. #define all(a) a.begin(), a.end()
  26. #define sz(a) (int)(a.size())
  27. #define pb push_back
  28. #define mp make_pair
  29. #ifdef MAXMIN
  30. #define min(a, b) ((a < b) ? (a) : (b))
  31. #define max(a, b) ((a > b) ? (a) : (b))
  32. #endif
  33. // Quick fors
  34. #define FOR(i, a, b) for (int i = a; i < b; ++i)
  35. #define RFOR(i, a, b) for (int i = a; i >= b; --i)
  36. #define EACH(e, a) for (auto& e : a)
  37. // Pragmas
  38. #pragma GCC optimize("O3,unroll-loops") // let the chaos begin!
  39. #ifdef AVX2
  40. #pragma GCC target("avx,avx2,bmi,bmi2,popcnt,lzcnt,tune=native")
  41. #endif
  42. //#pragma GCC comment(linker, "/stack:200000000")
  43. // PBDS
  44. #ifdef PBDS
  45. #include <ext/pb_ds/assoc_container.hpp>
  46. #include <ext/pb_ds/tree_policy.hpp>
  47. #define ordered_set tree <int, null_type, less <int>, rb_tree_tag, tree_order_statistics_node_update>
  48. #define ook order_of_key
  49. #define fbo find_by_order
  50. using namespace __gnu_pbds;
  51. #endif
  52. // POGGERS POGGERS POGGERS POGGERS POGGERS POGGERS POGGERS
  53. using namespace std;
  54. mt19937 rng(std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count());
  55. //#define int long long
  56.  
  57. /// BEGIN BOILERPLATE CODE
  58.  
  59. /** Interface */
  60.  
  61. inline int readChar();
  62. template <class T = int> inline T readInt();
  63. template <class T> inline void writeInt( T x, char end = 0 );
  64. inline void writeChar( int x );
  65. inline void writeWord( const char *s );
  66.  
  67. /** Read */
  68.  
  69. static const int buf_size = 4096;
  70.  
  71. inline int getChar() {
  72.     static char buf[buf_size];
  73.     static int len = 0, pos = 0;
  74.     if (pos == len)
  75.         pos = 0, len = fread(buf, 1, buf_size, stdin);
  76.     if (pos == len)
  77.         return -1;
  78.     return buf[pos++];
  79. }
  80.  
  81. inline int readChar() {
  82.     int c = getChar();
  83.     while (c <= 32)
  84.         c = getChar();
  85.     return c;
  86. }
  87.  
  88. template <class T>
  89. inline T readInt() {
  90.     int s = 1, c = readChar();
  91.     T x = 0;
  92.     if (c == '-')
  93.         s = -1, c = getChar();
  94.     while ('0' <= c && c <= '9')
  95.         x = x * 10 + c - '0', c = getChar();
  96.     return s == 1 ? x : -x;
  97. }
  98.  
  99. /** Write */
  100.  
  101. static int write_pos = 0;
  102. static char write_buf[buf_size];
  103.  
  104. inline void writeChar( int x ) {
  105.     if (write_pos == buf_size)
  106.         fwrite(write_buf, 1, buf_size, stdout), write_pos = 0;
  107.     write_buf[write_pos++] = x;
  108. }
  109.  
  110. template <class T>
  111. inline void writeInt( T x, char end ) {
  112.     if (x < 0)
  113.         writeChar('-'), x = -x;
  114.  
  115.     char s[24];
  116.     int n = 0;
  117.     while (x || !n)
  118.         s[n++] = '0' + x % 10, x /= 10;
  119.     while (n--)
  120.         writeChar(s[n]);
  121.     if (end)
  122.         writeChar(end);
  123. }
  124.  
  125. inline void writeWord( const char *s ) {
  126.     while (*s)
  127.         writeChar(*s++);
  128. }
  129.  
  130. struct Flusher {
  131.     ~Flusher() {
  132.         if (write_pos)
  133.             fwrite(write_buf, 1, write_pos, stdout), write_pos = 0;
  134.     }
  135. } flusher;
  136.  
  137. /// END BOILERPLATE CODE
  138.  
  139. const int INF = 1e9, MAX = 5009, M = 1e9 + 9, R = 1 << 19;
  140.  
  141. struct segtree { // -1 on segment, nearest > 1 on segment
  142.     struct node { int s, d; };
  143.     vector <node> t;
  144.     void merge(int v) { t[v].s = max(t[2 * v].s, t[2 * v + 1].s); }
  145.     void tag(int v, int x, int l) { t[v].s = max(1, t[v].s - x), t[v].d += x; }
  146.     void push(int v, int l) {
  147.         if (!t[v].d) return;
  148.         tag(2 * v, t[v].d, l / 2), tag(2 * v + 1, t[v].d, l / 2);
  149.         t[v].d = 0;
  150.     }
  151.     void build(vi& a) {
  152.         t.assign(2 * R, {1, 0});
  153.         FOR(i, 0, sz(a)) t[R + i].s = a[i];
  154.         RFOR(i, R - 1, 1) merge(i);
  155.     }
  156.     void upd(int ql, int qr, int v = 1, int nl = 0, int nr = R) {
  157.         if (qr <= nl || nr <= ql) return;
  158.         if (ql <= nl && nr <= qr) {
  159.             tag(v, 1, nr - nl);
  160.             return;
  161.         }
  162.         push(v, nr - nl);
  163.         int nm = (nl + nr) / 2;
  164.         upd(ql, qr, v * 2, nl, nm), upd(ql, qr, v * 2 + 1, nm, nr);
  165.         merge(v);
  166.     }
  167.     pii get(int ql, int v = 1, int nl = 0, int nr = R) {
  168.         if (ql >= nr || t[v].s == 1) return {-1, -1};
  169.         if (nl + 1 == nr && t[R + nl].s > 1) return {nl, t[R + nl].s};
  170.         push(v, nr - nl);
  171.         int nm = (nl + nr) / 2;
  172.         pii lres = get(ql, 2 * v, nl, nm);
  173.         if (lres.F >= 0) return lres;
  174.         else return get(ql, 2 * v + 1, nm, nr);
  175.     }
  176. } t;
  177.  
  178. signed main() {
  179.     fastIO;
  180.     int n, q; n = readInt(), q = readInt();
  181.     vi a(n); FOR(i, 0, n) a[i] = readInt();
  182.     t.build(a);
  183.     FOR(i, 0, q) {
  184.         int tp, l, r; tp = readInt(), l = readInt() - 1, r = readInt();
  185.         if (tp == 1) t.upd(l, r);
  186.         else {
  187.             int x; x = readInt();
  188.             int p = l - 1;
  189.             while (x) {
  190.                 pii rq = t.get(p + 1);
  191.                 if (rq.F == -1 || rq.F >= r) break;
  192.                 p = rq.F, x /= rq.S;
  193.             }
  194.             if (x) writeInt(-1, '\n');
  195.             else writeInt(p + 1, '\n');
  196.         }
  197.     }
  198. }
  199.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement