#include #pragma GCC target("avx2") #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") using namespace std; constexpr int MAX_N = 5000; int A[MAX_N]; int cnt[MAX_N+1][MAX_N+1]; int cnt_rp[MAX_N+1][MAX_N+1]; int rp[MAX_N+1]; int main(){ cin.tie(0);ios::sync_with_stdio(false); int N,K,L;cin >> N >> K >> L; for(int i=0;i> A[i]; for(int i=1;i<=N;i++){ int k = 0; for(int j=0;j= K) k++; cnt[i][j+1] = k; cnt_rp[i][j+1] = cnt_rp[i][j]+k; } } int Q;cin >> Q; while(Q--){ int l,r;cin >> l >> r; int ans_lef = 0,ans_rig = N; while(ans_rig - ans_lef > 1){ int mid = (ans_lef+ans_rig)/2; int cnt_itr = upper_bound(cnt[mid]+l,cnt[mid]+r+1,l-1)-cnt[mid]; (cnt_rp[mid][r]-cnt_rp[mid][cnt_itr-1]-(r-cnt_itr+1)*(l-1) >= L ? ans_rig:ans_lef) = mid; } cout << ans_rig << "\n"; } }