KQUERY - K-query

no tags 

{assign var="code" value="KQUERY"} {if $par==""} {assign var=par value=$locale} {/if}

{if $par=="vn"} {literal}

Truy vấn-k

Cho một dãy n phần tử a1, a2, ..., an và một số các truy vấn-k. Một truy vấn-k là một bộ ba (i, j, k) (1 ≤ i ≤ j ≤ n). Với mỗi truy vấn-k (i, j, k), bạn phải trả về số phần tử lớn hơn k nằm trong dãy con ai, ai+1, ..., aj.

Dữ liệu

  • Dòng 1: n (1 ≤ n ≤ 30000).
  • Dòng 2: n số a1, a2, ..., an (1 ≤ ai ≤ 109).
  • Dòng 3: q (1 ≤ q ≤ 200000), số truy vấn-k.
  • Trong q dòng tiếp theo, mỗi dòng chứa 3 số i, j, k thể hiện một truy vấn-k (1 ≤ i ≤ j ≤ n, 1 ≤ k ≤ 109).

Kết quả

  • Với mỗi truy vấn-k (i, j, k), in ra số phần tử lớn hơn k trong dãy con ai, ai+1, ..., aj trên một dòng.

     

Ví dụ

Dữ liệu
5
5 1 2 3 4
3
2 4 1
4 4 4
1 5 2 

Kết quả
2
0
3 

{/literal}{elseif ($par=="en" || $par=="")}{literal}

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 

{/literal} {/if}


hide comments
V Manikantan: 2016-10-05 15:43:01

Persistent segment trees works comfortably

blackjack123: 2016-10-03 22:42:02

my 100 th

RISHAV KUMAR: 2016-10-01 00:31:40

Has anyone done it in java using BIT..? I'm getting TLE, while the same code in c++ is getting passed. such a pain..

Last edit: 2016-10-01 00:31:57
divide_by_1: 2016-08-29 06:32:59

Did some one got AC with sqrt decomposition ?

buttman: 2016-07-18 16:19:16

time limit should be increased a bit.

askerov: 2016-07-07 17:19:18

I wrote Segment Tree, but it doesn't work. I have got TLE. What is Time limit: 0.184s? only build gets 0.2s

proficient: 2016-05-25 16:49:56

Anyone got AC with O( Q log^2 (N) ) ? I get TLE even with fast IO, typedef and inline functions.

||N0VICE||: 2016-02-27 09:53:47

Very strict Time Limit...
finally solved using segment tree(QlogQ + Qlogn) with fast io

Vinicius Zibetti Resko [UNITAU]: 2016-02-17 21:38:02

Very strict TL, my q * log (n+q) solution gives TLE even with fast I/O.

Vipul: 2016-02-17 13:47:01

nice problem
O(q log^2(n))(even with fast I/O).....TLE
O(q log n)(with segment tree).....TLE
O(q log n)(with BIT)....AC


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