struct Scanner { r: R, buf: Vec } impl Scanner { fn new(r: R) -> Scanner { Scanner { r, buf: Vec::with_capacity(8000) } } fn next(&mut self) -> &str { self.buf.clear(); loop { let (done, used) = { let available = self.r.fill_buf().unwrap(); match available.iter().position(|b| !b.is_ascii_whitespace()) { Some(i) => (true, i), None => (false, available.len()), } }; self.r.consume(used); if done || used == 0 { break; } } loop { let (done, used) = { let available = self.r.fill_buf().unwrap(); match available.iter().position(|b| b.is_ascii_whitespace()) { Some(i) => { self.buf.extend_from_slice(&available[..i]); (true, i) } None => { self.buf.extend_from_slice(&available); (false, available.len()) } } }; self.r.consume(used); if done || used == 0 { unsafe { return std::str::from_utf8_unchecked(&self.buf); } } } } } macro_rules! parse { ($s:expr, Chars) => ($s.chars().collect::>()); ($s:expr, Bytes) => ($s.bytes().collect::>()); ($s:expr, Usize1) => (parse!($s, usize) - 1); ($s:expr, $t:ty) => ($s.parse::<$t>().unwrap()); } macro_rules! read { ($sc:expr, [$t:tt; $n:expr]) => ( (0..$n).map(|_| read!($sc, $t)).collect::>() ); ($sc:expr, ($($t:tt),*)) => (($(read!($sc, $t)),*)); ($sc:expr, $t:ident) => (parse!($sc.next(), $t)); ($sc:expr, $($t:tt),*) => (($(read!($sc, $t)),*)); } // reference: https://github.com/atcoder/ac-library struct SegmentTree { n: usize, len: usize, height: usize, op: fn(T, T) -> T, e: fn() -> T, node: Vec, } impl SegmentTree { fn new(n: usize, op: fn(T, T) -> T, e: fn() -> T) -> SegmentTree { let (mut len, mut height) = (1, 1); while len < n { len *= 2; height += 1; } let node = vec![e(); 2 * len]; SegmentTree { n, len, height, op, e, node } } fn from(v: &[T], op: fn(T, T) -> T, e: fn() -> T) -> SegmentTree { let mut st = SegmentTree::new(v.len(), op, e); for i in 0..v.len() { st.node[i + st.len] = v[i]; } for i in (1..st.len).rev() { st.update(i); } st } fn update(&mut self, k: usize) { self.node[k] = (self.op)(self.node[2 * k], self.node[2 * k + 1]); } fn set(&mut self, mut p: usize, x: T) { assert!(p < self.n); p += self.len; self.node[p] = x; for i in 1..self.height { self.update(p >> i) }; } fn prod(&self, mut l: usize, mut r: usize) -> T { assert!(l <= r && r <= self.n); let (mut sml, mut smr) = ((self.e)(), (self.e)()); l += self.len; r += self.len; while l < r { if l & 1 != 0 { sml = (self.op)(sml, self.node[l]); l += 1; } if r & 1 != 0 { r -= 1; smr = (self.op)(self.node[r], smr); } l >>= 1; r >>= 1; } (self.op)(sml, smr) } } fn main() { use std::io::Write; let (stdin, stdout) = (std::io::stdin(), std::io::stdout()); let mut scanner = Scanner::new(stdin.lock()); let mut writer = std::io::BufWriter::new(stdout.lock()); macro_rules! scan { ($($t:tt)*) => (read!(scanner, $($t)*)); } macro_rules! println { ($($arg:tt)*) => (writeln!(writer, $($arg)*).ok()); } const INF: i64 = 1 << 60; use std::cmp::{max, min}; let n = scan!(usize); let x = scan!([i64; n]); let q = scan!(usize); let queries = scan!([(Usize1, Usize1, i64); q]); let mut low_st = SegmentTree::new(n, |v1, v2| max(v1, v2), || -INF); let mut high_st = SegmentTree::from(&x, |v1, v2| min(v1, v2), || INF); let mut query_order = (0..q).collect::>(); query_order.sort_by_key(|&i| queries[i].2); let mut x_order = (0..n).collect::>(); x_order.sort_by_key(|&i| x[i]); let mut ans = vec![0; q]; let mut k = 0; for i in query_order { let (l, r, y) = queries[i]; while k < n && x[x_order[k]] <= y { let j = x_order[k]; low_st.set(j, x[j]); high_st.set(j, std::i64::MAX); k += 1; } ans[i] = min( y - low_st.prod(l, r + 1), high_st.prod(l, r + 1) - y, ); } for ans in ans { println!("{}", ans); } }