def solve(io) n, q = io.get2 a = io.get_a(n) a2 = a.map_with_index { |ai, i| {ai, i} } a2.sort_by! { |ai, _| -ai } b = Array.new(n, -1) rbt = RedBlackTree(Int32).new a2.each do |_, i| b[i] = rbt.rsearch { |j| j < i } || -1 rbt.add(i) end b2 = b.map_with_index { |bi, i| {bi, i} } b2.sort_by! { |bi, _| bi } r = Array.new(q) do |i| ti, li, ri = io.get3; li -= 1; ri -= 1 {li, ri, i} end r.sort_by! { |li, _, _| li } j = 0 ft = FenwickTree(Int32).new(n) ans = Array.new(q, 0) r.each do |li, ri, i| while b2[j][0] < li ft.add(b2[j][1], 1) j += 1 end ans[i] = ft[li..ri] end ans.each do |v| io.put v end end class ProconIO def initialize(@ins : IO = STDIN, @outs : IO = STDOUT) @buf = IO::Memory.new("") end def get(k : T.class = Int32) forall T get_v(k) end macro define_get {% for i in (2..9) %} def get({{ *(1..i).map { |j| "k#{j}".id } }}) { {{ *(1..i).map { |j| "get(k#{j})".id } }} } end {% end %} end define_get macro define_getn {% for i in (2..9) %} def get{{i}}(k : T.class = Int32) forall T get({{ *(1..i).map { "k".id } }}) end {% end %} end define_getn def get_a(n : Int, k : T.class = Int32) forall T Array.new(n) { get_v(k) } end def get_c(n : Int, k : T.class = Int32) forall T get_a(n, k) end macro define_get_c {% for i in (2..9) %} def get_c(n : Int, {{ *(1..i).map { |j| "k#{j}".id } }}) a = Array.new(n) { get({{ *(1..i).map { |j| "k#{j}".id } }}) } { {{ *(1..i).map { |j| "a.map { |e| e[#{j-1}] }".id } }} } end {% end %} end define_get_c macro define_getn_c {% for i in (2..9) %} def get{{i}}_c(n : Int, k : T.class = Int32) forall T get_c(n, {{ *(1..i).map { "k".id } }}) end {% end %} end define_getn_c def get_m(r : Int, c : Int, k : T.class = Int32) forall T Array.new(r) { get_a(c, k) } end macro define_put {% for i in (1..9) %} def put({{ *(1..i).map { |j| "v#{j}".id } }}, *, delimiter = " ") {% for j in (1..i) %} print_v(v{{j}}, delimiter) {% if j < i %}@outs << delimiter{% end %} {% end %} @outs.puts end {% end %} end define_put def put_e(*vs) put(*vs) exit end def put_f(*vs) put(*vs) @outs.flush end private def get_v(k : Int32.class); get_token.to_i32; end private def get_v(k : Int64.class); get_token.to_i64; end private def get_v(k : UInt32.class); get_token.to_u32; end private def get_v(k : UInt64.class); get_token.to_u64; end private def get_v(k : Float64.class); get_token.to_f64; end private def get_v(k : String.class); get_token; end private def get_token loop do token = @buf.gets(' ', chomp: true) break token unless token.nil? @buf = IO::Memory.new(@ins.read_line) end end private def print_v(v, dlimiter) @outs << v end private def print_v(v : Enumerable, delimiter) v.each_with_index do |e, i| @outs << e @outs << delimiter if i < v.size - 1 end end end struct Int def cdiv(b : Int) (self + b - 1) // b end def bit?(i : Int) bit(i) == 1 end def set_bit(i : Int) self | (self.class.new(1) << i) end def reset_bit(i : Int) self & ~(self.class.new(1) << i) end {% if compare_versions(env("CRYSTAL_VERSION") || "0.0.0", "0.35.0") < 0 %} def digits(base = 10) raise ArgumentError.new("Invalid base #{base}") if base < 2 raise ArgumentError.new("Can't request digits of negative number") if self < 0 return [0] if self == 0 num = self digits_count = (Math.log(self.to_f + 1) / Math.log(base)).ceil.to_i ary = Array(Int32).new(digits_count) while num != 0 ary << num.remainder(base).to_i num = num.tdiv(base) end ary end {% end %} {% if compare_versions(env("CRYSTAL_VERSION") || "0.0.0", "0.34.0") < 0 %} def bit_length : Int32 x = self < 0 ? ~self : self if x.is_a?(Int::Primitive) Int32.new(sizeof(self) * 8 - x.leading_zeros_count) else to_s(2).size end end {% end %} end struct Float64 def near?(x) (self - x).abs <= (self.abs < x.abs ? x.abs : self.abs) * EPSILON end end struct Number {% if compare_versions(env("CRYSTAL_VERSION") || "0.0.0", "1.1.0") < 0 %} def zero? self == 0 end def positive? self > 0 end def negative? self < 0 end {% end %} {% if compare_versions(env("CRYSTAL_VERSION") || "0.0.0", "0.36.0") < 0 %} def self.additive_identity zero end def self.multiplicative_identity new(1) end {% end %} end class Array macro new_md(*args, &block) {% if !block %} {% for arg, i in args[0...-2] %} Array.new({{arg}}) { {% end %} Array.new({{args[-2]}}, {{args[-1]}}) {% for arg in args[0...-2] %} } {% end %} {% else %} {% for arg, i in args %} Array.new({{arg}}) { |_i{{i}}| {% end %} {% for block_arg, i in block.args %} {{block_arg}} = _i{{i}} {% end %} {{block.body}} {% for arg in args %} } {% end %} {% end %} end end module Math {% if compare_versions(env("CRYSTAL_VERSION") || "0.0.0", "1.2.0") < 0 %} def isqrt(value : Int::Primitive) raise ArgumentError.new "Input must be non-negative integer" if value < 0 return value if value < 2 res = value.class.zero bit = res.succ << (res.leading_zeros_count - 2) bit >>= value.leading_zeros_count & ~0x3 while (bit != 0) if value >= res + bit value -= res + bit res = (res >> 1) + bit else res >>= 1 end bit >>= 2 end res end {% end %} end macro min_u(a, b) {{a}} = Math.min({{a}}, {{b}}) end macro max_u(a, b) {{a}} = Math.max({{a}}, {{b}}) end macro zip(a, *b, &block) {{a}}.zip({{*b}}) {{block}} end class FenwickTree(T) def initialize(@n : Int32) @b = Array.new(@n + 1, T.additive_identity) end def [](i : Int) self[i..i] end def [](start : Int, count : Int) get(start + count) - get(start) end def [](r : Range) sc = Indexable.range_to_index_and_count(r, @n) raise ArgumentError.new("Invalid range") if sc.nil? self[*sc] end def add(i : Int, val : T) i += 1 while i <= @n @b[i] += val i += i & -i end end @b : Array(T) private def get(i : Int) s = T.additive_identity while i > 0 s += @b[i] i -= i & -i end s end end class RedBlackTree(T) def initialize @cmp = ->(a : T, b : T) { a <=> b } @root = NilNode(T).instance @size = 0 end def empty? @size == 1 end getter size : Int32 def first first_node(@root).key end def last last_node(@root).key end def each(&block : T -> _) x = first_node(@root) until x.nil_node? yield x.key x = succ_node(x) end end def reverse_each(&block : T -> _) x = last_node(@root) until x.nil_node? yield x.key x = pred_node(x) end end def includes?(key : T) x = find_node(@root, key) !x.nil_node? end def search(&block : T -> Bool) x = search_node(@root, block) x.nil_node? ? nil : x.key end def rsearch(&block : T -> Bool) x = rsearch_node(@root, block) x.nil_node? ? nil : x.key end def add(key : T) x = Node.new(key) insert_helper(x) x.color = :red while x != @root && x.parent.red? if x.parent == x.parent.parent.left y = x.parent.parent.right if !y.nil_node? && y.red? x.parent.color = :black y.color = :black x.parent.parent.color = :red x = x.parent.parent else if x == x.parent.right x = x.parent left_rotate(x) end x.parent.color = :black x.parent.parent.color = :red right_rotate(x.parent.parent) end else y = x.parent.parent.left if !y.nil_node? && y.red? x.parent.color = :black x.color = :black x.parent.parent.color = :red x = x.parent.parent else if x == x.parent.left x = x.parent right_rotate(x) end x.parent.color = :black x.parent.parent.color = :red left_rotate(x.parent.parent) end end end @root.color = :black end def remove(key : T) z = find_node(@root, key) return false if z.nil_node? y = z.left.nil_node? || z.right.nil_node? ? z : succ_node(z) x = y.left.nil_node? ? y.right : y.left x.parent = y.parent if y.parent.nil_node? @root = x else if y == y.parent.left y.parent.left = x else y.parent.right = x end end z.key = y.key if y != z remove_fixup(x) if y.black? @size -= 1 true end enum Color Red Black end class Node(T) def initialize(@key : T, @color : Color = :red) @left = NilNode(T).instance @right = NilNode(T).instance @parent = NilNode(T).instance end property key : T property color : Color property left : Node(T) property right : Node(T) property parent : Node(T) delegate black?, red?, to: @color def nil_node? false end end class NilNode(T) < Node(T) def initialize @key = uninitialized T @color = :black @left = uninitialized Node(T) @right = uninitialized Node(T) @parent = uninitialized Node(T) end def self.instance instance = self.new instance.left = instance.right = instance.parent = instance end def nil_node? true end end @cmp : (T, T) -> Int32 @root : Node(T) private def cmp(a : Node(T), b : Node(T)) @cmp.call(a.key, b.key) end private def first_node(x : Node(T)) until x.left.nil_node? x = x.left end x end private def last_node(x : Node(T)) until x.right.nil_node? x = x.right end x end private def succ_node(x : Node(T)) return first_node(x.right) unless x.right.nil_node? y = x.parent while !y.nil_node? && x == y.right x, y = y, y.parent end y end private def pred_node(x : Node(T)) return last_node(x.left) unless x.left.nil_node? y = x.parent while !y.nil_node? && x == y.left x, y = y, y.parent end y end private def find_node(x : Node(T), key : T) while !x.nil_node? && x.key != key x = @cmp.call(key, x.key).negative? ? x.left : x.right end x end private def search_node(x : Node(T), block : T -> Bool) last : Node(T) = NilNode(T).instance loop do if block.call(x.key) last = x if x.left.nil_node? break last else x = x.left end else if x.right.nil_node? break last else x = x.right end end end end private def rsearch_node(x : Node(T), block : T -> Bool) last : Node(T) = NilNode(T).instance loop do if block.call(x.key) last = x if x.right.nil_node? break last else x = x.right end else if x.left.nil_node? break last else x = x.left end end end end private def insert_helper(z : Node(T)) x, y = @root, NilNode(T).instance until x.nil_node? x, y = cmp(z, x).negative? ? x.left : x.right, x end z.parent = y if y.nil_node? @root = z else cmp(z, y).negative? ? y.left = z : y.right = z end @size += 1 end private def remove_fixup(x : Node(T)) while x != @root && x.black? if x == x.parent.left w = x.parent.right if w.red? w.color = :black x.parent.color = :red left_rotate(x.parent) w = x.parent.right end if w.left.black? && w.right.black? w.color = :red x = x.parent else if w.right.black? w.left.color = :black w.color = :red right_rotate(w) w = x.parent.right end w.color = x.parent.color x.parent.color = :black w.right.color = :black left_rotate(x.parent) x = @root end else w = x.parent.left if w.red? w.color = :black x.parent.color = :red right_rotate(x.parent) w = x.parent.left end if w.right.black? && w.left.black? w.color = :red x = x.parent else if w.left.black? w.right.color = :black w.color = :red left_rotate(w) w = x.parent.left end w.color = x.parent.color x.parent.color = :black w.left.color = :black right_rotate(x.parent) x = @root end end end x.color = :black end private def left_rotate(x : Node(T)) raise "x.right is nil" if x.right.nil_node? y = x.right x.right = y.left y.left.parent = x unless y.left.nil_node? y.parent = x.parent if x.parent.nil_node? @root = y elsif x == x.parent.left x.parent.left = y else x.parent.right = y end y.left = x x.parent = y end private def right_rotate(x : Node(T)) raise "x.left is nil" if x.left.nil_node? y = x.left x.left = y.right y.parent = x.parent if x.parent.nil_node? @root = y elsif x == x.parent.left x.parent.left = y else x.parent.right = y end y.right = x x.parent = y end end solve(ProconIO.new)