KQUERY - K-query

no tags 

Given a sequence of n numbers a1, a2, ..., an and a number of k- queries. A k-query is a triple (i, j, k) (1 ≤ i ≤ j ≤ n). For each k-query (i, j, k), you have to return the number of elements greater than k in the subsequence ai, ai+1, ..., aj.

Input

  • Line 1: n (1 ≤ n ≤ 30000).
  • Line 2: n numbers a1, a2, ..., an (1 ≤ ai ≤ 109).
  • Line 3: q (1 ≤ q ≤ 200000), the number of k- queries.
  • In the next q lines, each line contains 3 numbers i, j, k representing a k-query (1 ≤ i ≤ j ≤ n, 1 ≤ k ≤ 109).

Output

  • For each k-query (i, j, k), print the number of elements greater than k in the subsequence ai, ai+1, ..., aj in a single line.

Example

Input
5
5 1 2 3 4
3
2 4 1
4 4 4
1 5 2 

Output
2
0
3 

hide comments
Rishabh Agrawal: 2016-01-30 00:25:08

Does anyone know of an accepted solution using MO's algorithm? I submitted using segment tree and got AC, but want to know if it can be done using MO's and how?

hello_world_v2: 2015-12-25 09:28:51

segment tree+ sorting in ascending+ offline processing of queries +fast i/o still TLE
@vinorom @Shashank Tiwari how did u get it??

Anirudh: 2015-12-21 08:09:35

Fast I/O is a must here.

Shivam Mitra: 2015-12-15 21:43:20

segment tree with fast i/o gets accepted. For fast i/o in c++, refer https://discuss.codechef.com/questions/6677/cool-fast-io-implementation

Additya: 2015-10-29 15:45:40

Bit with Mo's algorithm gives TLE. Is the TL too strict?

Abhinandan Agarwal: 2015-10-20 00:04:28

remove spoilers please,
note merge sort tree gives TLE.

prakhar: 2015-10-14 14:32:36

Segment Tree ---- TLE
MO's Algorithm(Square root Decomposition) ---- TLE
@vinorom how you did it with segment tree ??
Anyone with segment tree or an other method ??

vinorom: 2015-09-13 08:24:47

segment tree with async IO get AC (0.34s). How do others get 0.10s?

Shashank Tiwari: 2015-08-29 03:26:18

BIT + sorting in descending + offline processing of queries ...
Not an easy one for me but learned something new..

(Tjandra Satria Gunawan)(曾毅昆): 2015-08-28 20:11:12

Interesting! :) even my fastest solution at KQUERYO (http://www.spoj.com/problems/KQUERYO/) getting TLE here :o


Added by:Duc
Date:2008-10-26
Time limit:0.184s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ERL JS-RHINO NODEJS PERL6 VB.NET
Resource:© VNOI