Problem:
Range Count Query

数列a_1,a_2,..,a_Nが与えられます。

クエリでは、値がl以上r以下の項の個数を答えてください。

入力

N Q
a_1 a_2...a_N
l_1 r_1
l_2 r_2
:
l_q r_q

出力

ans_1
ans_2
:
ans_q

i行目には、i番目のクエリに対する答え、すなわちl_i \leq a_j \leq r_iなるjの個数を出力せよ。

制約

1 \leq N,Q \leq 10^5

1 \leq a_i \leq 10^9

1 \leq l_i \leq r_i \leq 10^9

入力例

6 3
8 6 9 1 2 1
2 8
1 7
3 5

出力例

3
4
0

Solution in Rust (judge verdict: Accepted)

// ---------- begin SegmentTree Point update Range query ----------
mod segment_tree {
    pub struct PURQ<T: Clone, F: Fn(T, T) -> T> {
        n: usize,
        a: Vec<T>,
        id: T,
        op: F,
    }
    #[allow(dead_code)]
    impl<T: Clone, F: Fn(T, T) -> T> PURQ<T, F> {
        pub fn new(n: usize, id: T, op: F) -> PURQ<T, F> {
            let mut k = 1;
            while k < n {
                k *= 2;
            }
            PURQ {
                n: k,
                a: vec![id.clone(); 2 * k],
                id: id,
                op: op,
            }
        }
        pub fn update(&mut self, x: usize, v: T) {
            let mut k = self.n + x;
            let a = &mut self.a;
            a[k] = v;
            k >>= 1;
            while k > 0 {
                a[k] = (self.op)(a[2 * k].clone(), a[2 * k + 1].clone());
                k >>= 1;
            }
        }
        pub fn update_tmp(&mut self, x: usize, v: T) {
            self.a[x + self.n] = v;
        }
        pub fn update_all(&mut self) {
            for k in (1..(self.n)).rev() {
                self.a[k] = (self.op)(self.a[2 * k].clone(), self.a[2 * k + 1].clone());
            }
        }
        pub fn find(&self, mut l: usize, mut r: usize) -> T {
            let mut p = self.id.clone();
            let mut q = self.id.clone();
            l += self.n;
            r += self.n;
            while l < r {
                if (l & 1) == 1 {
                    p = (self.op)(p, self.a[l].clone());
                    l += 1;
                }
                if (r & 1) == 1 {
                    r -= 1;
                    q = (self.op)(self.a[r].clone(), q);
                }
                l >>= 1;
                r >>= 1;
            }
            (self.op)(p, q)
        }
    }
}
// ---------- end SegmentTree Point update Range query ----------
//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::<Vec<_>>()
    };
    ($iter:expr, chars) => {
        read_value!($iter, String).chars().collect::<Vec<char>>()
    };
    ($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: [usize1; n],
        ask: [(usize1, usize); q],
    }
    let mut z = vec![];
    for a in a.iter() {
        z.push(*a);
    }
    for p in ask.iter() {
        z.push(p.0);
        z.push(p.1);
    }
    z.sort();
    z.dedup();
    let len = z.len();
    let mut cnt = vec![0; len];
    let mut seg = segment_tree::PURQ::new(len, 0, |a, b| a + b);
    for a in a {
        let k = z.binary_search(&a).unwrap();
        cnt[k] += 1;
        seg.update(k, cnt[k]);
    }
    for (l, r) in ask {
        let l = z.binary_search(&l).unwrap();
        let r = z.binary_search(&r).unwrap();
        let ans = seg.find(l, r);
        writeln!(out, "{}", ans).ok();
    }
}

fn main() {
    run();
}