PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#CC0000068. Đếm số lớn hơn ngưỡng trên đoạn (KQUERY - K-query)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

4 lộ trình
Xem thêm 1 lộ trình

Đếm số lớn hơn ngưỡng trên đoạn (KQUERY - K-query)

KQUERY - K-query

Source: SPOJ

Version: Phuoc Hung OJ Extended

Problem Statement

Given an array a1,…,ana_1,\ldots,a_n and qq queries (l,r,k)(l,r,k), count the elements with value strictly greater than kk in the inclusive index range [l,r][l,r]. The array is static.

Input

The first line contains nn; the second contains nn values; the third contains qq. Each subsequent line contains l,r,kl,r,k.

Output

For each query, print the number of qualifying elements on a separate line.

Subtasks

Original constraints: 1≤n≤300001\le n\le30000; 1≤q≤2000001\le q\le200000; 1≤ai,k≤1091\le a_i,k\le10^9; 1≤l≤r≤n1\le l\le r\le n.

  • Subtask 1 (20 points): 1≤n≤201\le n\le20; 1≤q≤201\le q\le20; 1≤ai,k≤1001\le a_i,k\le100; 1≤l≤r≤n1\le l\le r\le n.
  • Subtask 2 (30 points): 1≤n≤20001\le n\le2000; 1≤q≤20001\le q\le2000; 1≤ai,k≤1000001\le a_i,k\le100000; 1≤l≤r≤n1\le l\le r\le n.
  • Subtask 3 (50 points): 1≤n≤300001\le n\le30000; 1≤q≤2000001\le q\le200000; 1≤ai,k≤10000000001\le a_i,k\le1000000000; 1≤l≤r≤n1\le l\le r\le n.

Examples

Input

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

Output

2
0
3

Explanation

The three queries return 22, 00, and 33 respectively; values equal to kk do not count.