Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma GCC optimize("O3")
- #pragma GCC optimize("unroll-loops")
- #pragma GCC target("avx2,avx,fma,bmi2")
- #include <bits/stdc++.h>
- #include <immintrin.h>
- #include <ext/pb_ds/assoc_container.hpp>
- using namespace std;
- using namespace __gnu_pbds;
- #define endl '\n'
- #define int long long
- #define all(arr) arr.begin(), arr.end()
- #define multitest() int _gorilla_silverback; cin >> _gorilla_silverback; while (_gorilla_silverback --> 0)
- #define ls(id) (id << 1 | 1)
- #define rs(id) ((id << 1) + 2)
- #define sqr(x) ((x) * (x))
- #define dlg(x) (31 - __builtin_clz(x))
- #define ulg(x) (32 - __builtin_clz(x))
- #define inf 1e9
- typedef pair<int, int> ipair;
- typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> treap;
- mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
- const int MXN = 5e5 + 5;
- const int LOG = 20;
- int n, q;
- int p[LOG][MXN];
- int val[MXN];
- pair<int, int> st[MXN << 2];
- void build(int l, int r, int x)
- {
- if (l == r)
- {
- st[x] = {val[l], l};
- return;
- }
- int mid = (l + r) >> 1;
- build(l, mid, 2*x);
- build(mid + 1, r, 2*x + 1);
- st[x] = max(st[2*x], st[2*x + 1]);
- }
- pair<int, int> get(int l, int r, int x, int lx, int rx)
- {
- if (l > rx || r < lx) return {-inf, -1};
- if (l >= lx && r <= rx) return st[x];
- int mid = (l + r) >> 1;
- return max(get(l, mid, 2*x, lx, rx), get(mid + 1, r, 2*x + 1, lx, rx));
- }
- void solve()
- {
- cin >> n >> q;
- for (int i = 1; i <= n; i++)
- {
- cin >> val[i];
- val[i] = val[i] + i;
- }
- build(1, n, 1);
- for (int i = 1; i <= n; i++)
- {
- p[0][i] = get(1, n, 1, i, val[i]).second;
- }
- for (int i = 1; i < LOG; i++)
- {
- for (int j = 1; j <= n; j++)
- {
- p[i][j] = p[i - 1][p[i - 1][j]];
- }
- }
- while (q--)
- {
- int x, y;
- cin >> x >> y;
- if (val[x] >= y)
- {
- cout << 1 << '\n';
- continue;
- }
- int res = 0;
- for (int i = LOG - 1; i >= 0; i--)
- {
- if (val[p[i][x]] >= y) continue;
- x = p[i][x];
- res += (1LL << i);
- }
- if (val[p[0][x]] < y)
- {
- cout << -1 << '\n';
- continue;
- }
- cout << res + 2 << '\n';
- }
- }
- int32_t main() {
- ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
- int T;
- cin >> T;
- while (T--)
- {
- solve();
- }
- }
- // LIKE AND SUBSCRIBE !!
- // LINK OF CODE DOWN BELOW !!!!
- // THANK YOU SIRS
- // AUTHOR: INDIAN LEAKER YT
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement