#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][MAX_N]; int cnt_rp[MAX_N][MAX_N]; int rp[MAX_N+1]; int main(){ int N,K,L;cin >> N >> K >> L; for(int i=0;i> A[i];A[i]--;} for(int i=0;i= K) k++; cnt[i][j] = k+1; } cnt_rp[i][0] = cnt[i][0]; for(int j=1;j> Q; while(Q--){ int l,r;cin >> l >> r;l--;r--; int ans_lef = -1,ans_rig = N-1; 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)-cnt[mid]; if(cnt_rp[mid][r]-(cnt_itr == 0 ? 0:cnt_rp[mid][cnt_itr-1])-(r-cnt_itr+1)*l >= L) ans_rig = mid; else ans_lef = mid; } cout << ans_rig+1 << "\n"; } }