Advertisement
Guest User

Untitled

a guest
Aug 14th, 2024
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.60 KB | None | 0 0
  1. #pragma GCC optimize("O3")
  2. #pragma GCC optimize("unroll-loops")
  3. #pragma GCC target("avx2,avx,fma,bmi2")
  4.  
  5. #include <bits/stdc++.h>
  6. #include <immintrin.h>
  7. #include <ext/pb_ds/assoc_container.hpp>
  8. using namespace std;
  9. using namespace __gnu_pbds;
  10.  
  11. #define endl '\n'
  12. #define int long long
  13. #define all(arr) arr.begin(), arr.end()
  14. #define multitest() int _gorilla_silverback; cin >> _gorilla_silverback; while (_gorilla_silverback --> 0)
  15. #define ls(id) (id << 1 | 1)
  16. #define rs(id) ((id << 1) + 2)
  17. #define sqr(x) ((x) * (x))
  18. #define dlg(x) (31 - __builtin_clz(x))
  19. #define ulg(x) (32 - __builtin_clz(x))
  20. #define inf 1e9
  21.  
  22. typedef pair<int, int> ipair;
  23. typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> treap;
  24. mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  25.  
  26. const int MXN = 5e5 + 5;
  27. const int LOG = 20;
  28.  
  29. int n, q;
  30. int p[LOG][MXN];
  31. int val[MXN];
  32. pair<int, int> st[MXN << 2];
  33.  
  34. void build(int l, int r, int x)
  35. {
  36. if (l == r)
  37. {
  38. st[x] = {val[l], l};
  39. return;
  40. }
  41. int mid = (l + r) >> 1;
  42. build(l, mid, 2*x);
  43. build(mid + 1, r, 2*x + 1);
  44. st[x] = max(st[2*x], st[2*x + 1]);
  45. }
  46. pair<int, int> get(int l, int r, int x, int lx, int rx)
  47. {
  48. if (l > rx || r < lx) return {-inf, -1};
  49. if (l >= lx && r <= rx) return st[x];
  50. int mid = (l + r) >> 1;
  51. return max(get(l, mid, 2*x, lx, rx), get(mid + 1, r, 2*x + 1, lx, rx));
  52. }
  53.  
  54. void solve()
  55. {
  56. cin >> n >> q;
  57. for (int i = 1; i <= n; i++)
  58. {
  59. cin >> val[i];
  60. val[i] = val[i] + i;
  61. }
  62. build(1, n, 1);
  63. for (int i = 1; i <= n; i++)
  64. {
  65. p[0][i] = get(1, n, 1, i, val[i]).second;
  66. }
  67. for (int i = 1; i < LOG; i++)
  68. {
  69. for (int j = 1; j <= n; j++)
  70. {
  71. p[i][j] = p[i - 1][p[i - 1][j]];
  72. }
  73. }
  74. while (q--)
  75. {
  76. int x, y;
  77. cin >> x >> y;
  78. if (val[x] >= y)
  79. {
  80. cout << 1 << '\n';
  81. continue;
  82. }
  83. int res = 0;
  84. for (int i = LOG - 1; i >= 0; i--)
  85. {
  86. if (val[p[i][x]] >= y) continue;
  87. x = p[i][x];
  88. res += (1LL << i);
  89. }
  90. if (val[p[0][x]] < y)
  91. {
  92. cout << -1 << '\n';
  93. continue;
  94. }
  95. cout << res + 2 << '\n';
  96. }
  97. }
  98.  
  99. int32_t main() {
  100. ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
  101. int T;
  102. cin >> T;
  103. while (T--)
  104. {
  105. solve();
  106. }
  107.  
  108. }
  109. // LIKE AND SUBSCRIBE !!
  110. // LINK OF CODE DOWN BELOW !!!!
  111. // THANK YOU SIRS
  112. // AUTHOR: INDIAN LEAKER YT
Tags: CODECHEF
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement