use core::cmp::{max, min}; use proconio::input; use std::collections::BTreeSet; const INF: i128 = 1_000_000_000_000_000_000 + 1000; fn main() { input! { n: i32, x: [i32; n], q: i32, ques: [(i32, i32, i64); q], } let mut count_same = vec![0; n as usize]; for i in 0..(n - 1) { count_same[(i + 1) as usize] = count_same[i as usize] + if x[i as usize] == x[(i + 1) as usize] { 1 } else { 0 } } const MAX_K: i32 = 70; let mut range = vec![vec![n; (MAX_K + 1) as usize]; n as usize]; for i in 0..n { let mut set = BTreeSet::new(); for j in (max(0, i - MAX_K + 1)..i + 1).rev() { set.insert(x[j as usize]); range[i as usize][set.len()] = j; } for k in (set.len() as i32)..MAX_K + 1 { range[i as usize][k as usize] = 0; } } for (l, r, s) in ques { let l = l - 1; let dist = r - l; if dist <= 62 && 1i64 << (dist - 1) <= s { println!("-1"); continue; } let same = count_same[(r - 1) as usize] - count_same[l as usize]; if 61 <= same || s < (1i64 << same) { println!("0"); continue; } for k in 2..MAX_K { let mut dp = vec![0i128; (dist + 1) as usize]; let mut dp_sum = vec![0i128; (dist + 1 + 1) as usize]; dp[0] = 1; dp_sum[1] = 1; for i in l..r { dp[(i + 1 - l) as usize] = min( INF, dp_sum[(i + 1 - l) as usize] - dp_sum[(max(l, range[i as usize][k as usize]) - l) as usize], ); dp_sum[(i + 1 - l + 1) as usize] = dp_sum[(i + 1 - l) as usize] + dp[(i + 1 - l) as usize]; } if dp[(r - l) as usize] > s as i128 { println!("{}", k - 1); break; } } } }