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
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.

supriyanta: 2018-08-09 21:56:20

BIT + offline query.
no need to use fast io etc etc.....

karan_yadav: 2018-08-02 12:12:48

A really Ingenious solution is provided by Raziman T.V., A must read I'd say.
Follow the link - https://www.quora.com/How-will-you-solve-the-K-Query-using-segment-trees

wjli: 2018-07-28 04:40:00

online query can be answered thru persistent segment tree.

kissu_pari_na: 2018-07-10 03:34:11

At first try using Merge Sort Tree, got TLE, then solved using segment tree + offline query

Last edit: 2018-07-10 03:34:37
horizon121: 2018-05-26 21:17:55

Interesting!!Learned a lot . My first using SegTree+Offline query . Good one

ayush926: 2018-05-24 20:30:30

BIT+Offline+Fast I/O ---> 0.25s

devarshi09: 2018-05-23 12:39:37

Time limit is too strict for merge sort tree!


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