LuoGu: CF797E Array Queries
CF: E. Array Queries
对于所有 $k \le \sqrt n$ ,可以 $O(n \sqrt n)$ 预处理;对于 $k > \sqrt n$ ,暴力算,复杂度 $O(\sqrt n)$ 。
查看代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
| #include <cstdio> #include <cmath> using namespace std; template <class Type> void read(Type &x) { char c; bool flag = false; while ((c = getchar()) < '0' || c > '9') c == '-' && (flag = true); x = c - '0'; while ((c = getchar()) >= '0' && c <= '9') x = (x << 3) + (x << 1) + c - '0'; flag && (x = ~x + 1); } template <class Type> void write(Type x) { x < 0 && (putchar('-'), x = ~x + 1); x > 9 && (write(x / 10), 0); putchar(x % 10 + '0'); } const int N = 1e5 + 10, M = 320; int n, m, b, w[N], f[N][M]; int dfs (int p, int k) { if (p > n) return 0; if (k <= b && f[p][k]) return f[p][k]; int t = dfs(p + w[p] + k, k) + 1; k <= b && (f[p][k] = t); return t; } int main () { read(n); for (int i = 1; i <= n; i++) read(w[i]); b = sqrt(n); read(m); for (int p, k; m; m--) { read(p), read(k); write(dfs(p, k)), puts(""); } return 0; }
|