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
fardin_abir: 2020-01-25 18:19:33

merge sort tree + binary search solved this....

codefresher: 2020-01-20 05:30:07

ac in one go...easy one...segment tree+binary search...

scolar_fuad: 2020-01-16 22:10:13

nice problem i got ac using segment tree + binary search

aria_ashrafi: 2019-12-25 15:45:00

Did anyone solve this using mo's algorithm ?!

mittalshubham: 2019-12-21 08:49:37

For those getting TLE by segment trees:
Do not declare the variables with maximum value globally instead, declare them dynamically based on the values of n and q.

amirali82: 2019-12-20 10:54:23

If someone can accept this with SegTree and (Cin / Cout) PLEASE tell me how!
Seriously, I would like to see the code.

Last edit: 2019-12-20 10:54:43
ioustreamh: 2019-12-16 09:29:01

got lots of TLE for cin and cout

medhruv7: 2019-11-12 20:32:15

Used iterative segtree. AC in one go. Using cout and cin

harry_shit: 2019-10-24 09:56:18

printf scanf -->AC
cin cout with fast i/o-->tle

peregrine: 2019-07-30 09:03:21

Solved it using cin + offline query


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