Editorial for Chia đoạn
Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.
Submitting an official solution before solving the problem yourself is a bannable offence.
Lưu ý: Các code mẫu dưới đây chỉ mang tính tham khảo và có thể không AC được bài tập này
Code mẫu của ladpro98
#include <bits/stdc++.h> const int N = 100005; using namespace std; long long a[N]; int n, q, lim, F[N][20]; int BS(int l, int r, long long S) { int m, k; while (l<=r){ m = (l+r) >> 1; if (S - a[m] <= lim) { k = m; r = m-1; } else l = m+1; } return (k + 1); } int main() { //freopen("DIVSEQQ.inp","r",stdin); scanf("%d %d %d\n", &n, &q, &lim); int i, j, u, v, res, l2 = log2(n); for(i=1; i<=n; i++) scanf("%d", &a[i]); for(i=1; i<=n; i++) a[i] += a[i-1]; for(i=1; i<=n; i++) F[i][0] = BS(0,i-1,a[i]); for(j=1; j<=l2; j++) F[0][j] = 1; for(i=1; i<=n; i++) for(j=1; j<=l2; j++) F[i][j] = max(F[F[i][j-1]-1][j-1],1); for(i=1; i<=q; i++) { scanf("%d %d\n", &u, &v); res = u; for(j=l2; j>=0; j--) { if (res <= 0) break; if (1 << j <= v) { res = F[res][j] - 1; v -= 1 << j; } } printf("%d\n", max(res+1,1)); } return 0; }
Comments