// 1-indexedなBIT // 座標xへの加算、[1,x]の和, ([1,x]の和)>=s となる最小のxの探索 // ---------- begin fenwick tree ---------- #[allow(dead_code)] mod fenwick { type T = i64; const ZERO: T = 0; pub struct Tree { a: Vec, } impl Tree { pub fn new(n: usize) -> Tree { Tree { a: vec![ZERO; n + 1], } } pub fn add(&mut self, mut x: usize, v: T) { while x < self.a.len() { self.a[x] += v; x += x & (!x + 1); } } pub fn sum(&self, mut x: usize) -> T { debug_assert!(x < self.a.len()); let mut res = ZERO; while x > 0 { res += self.a[x]; x -= x & (!x + 1); } res } pub fn search(&self, s: T) -> usize { debug_assert!(self.sum(self.a.len() - 1) >= s); let mut k = 1; while 2 * k < self.a.len() { k *= 2; } let mut x = 0; let mut w = ZERO; while k > 0 { if k + x < self.a.len() && w + self.a[x + k] < s { w += self.a[x + k]; x += k; } k >>= 1; } x + 1 } } } // ---------- end fenwick tree ---------- //https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8 より macro_rules! input { (source = $s:expr, $($r:tt)*) => { let mut iter = $s.split_whitespace(); input_inner!{iter, $($r)*} }; ($($r:tt)*) => { let s = { use std::io::Read; let mut s = String::new(); std::io::stdin().read_to_string(&mut s).unwrap(); s }; let mut iter = s.split_whitespace(); input_inner!{iter, $($r)*} }; } macro_rules! input_inner { ($iter:expr) => {}; ($iter:expr, ) => {}; ($iter:expr, $var:ident : $t:tt $($r:tt)*) => { let $var = read_value!($iter, $t); input_inner!{$iter $($r)*} }; } macro_rules! read_value { ($iter:expr, ( $($t:tt),* )) => { ( $(read_value!($iter, $t)),* ) }; ($iter:expr, [ $t:tt ; $len:expr ]) => { (0..$len).map(|_| read_value!($iter, $t)).collect::>() }; ($iter:expr, chars) => { read_value!($iter, String).chars().collect::>() }; ($iter:expr, usize1) => { read_value!($iter, usize) - 1 }; ($iter:expr, $t:ty) => { $iter.next().unwrap().parse::<$t>().expect("Parse error") }; } // ここまで use std::io::Write; fn run() { let out = std::io::stdout(); let mut out = std::io::BufWriter::new(out.lock()); input! { n: usize, q: usize, a: [i64; n], p: [(u8, usize, usize, i64); q], } let mut ai = Vec::with_capacity(n); for (i, a) in a.iter().enumerate() { ai.push((*a, i + 1)); } ai.sort(); let a = ai; let mut b = Vec::with_capacity(q); for (i, &(_, l, r, x)) in p.iter().enumerate() { b.push((x, l, r, i)); } b.sort(); let mut ans = vec![0; q]; let mut s = fenwick::Tree::new(n); let mut cnt = fenwick::Tree::new(n); let mut i = n; for &(x, l, r, k) in b.iter().rev() { while i > 0 && a[i - 1].0 > x { let (a, k) = a[i - 1]; s.add(k, a); cnt.add(k, 1); i -= 1; } let v = s.sum(r) - s.sum(l - 1) - x * (cnt.sum(r) - cnt.sum(l - 1)); ans[k] = v; } for v in ans { writeln!(out, "{}", v).unwrap(); } } fn main() { run(); }