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
abhi2402: 2019-06-17 08:49:04

C++ PBDS + Offline querying gets it in 0.27s.

ab_biswas09: 2019-03-18 23:49:20

After So Many TLEs finally AC
Use scanf and printf

vivek_rathi: 2019-03-17 08:07:30

Use scanf and printf otherwise TLE

davidgalehouse: 2019-02-17 02:12:03

Easier than DQUERY in my opinion. Less creativity required in applying the BIT.

abhinav_jain02: 2019-02-01 10:07:19

Try to use BIT instead of segment trees. It works faster and has a smaller code.

a_98: 2018-11-23 14:03:30

BIT does it in 0.17 without fast IO.

yaseenmollik: 2018-11-15 21:35:34

First problem using Offline queries with segment tree :D

phoemur: 2018-10-23 02:08:31

BIT + Offline Query = 0.20s
SegTree + Offline Query = 0.31s
MergeSort Tree + Online Query = TLE

alexandro5432: 2018-10-12 16:09:24

silxikys time limit is 0.184s for each test. Your solution worked in 0.4s for all the tests summed up

silxikys: 2018-08-27 05:31:43

Can be solved using sqrt-decomposition of array and coordinate compression, my sol is 0.4 seconds. Not sure what the 0.184s time limit means.


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