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

#SRD0000021. Truy vấn ngưỡng trực tuyến (K-query Online)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Truy vấn ngưỡng trực tuyến (K-query Online)

K-query Online

Source: SPOJ

Version: Phuoc Hung OJ Extended

Problem Statement

You are given an immutable positive integer array a1,…,ana_1,\ldots,a_n. Queries are encoded using the preceding answer and must be processed online. Each query provides three integers a,b,ca,b,c. Let last be the previous query answer, initially zero. Decode

$$i=a\mathbin{\mathrm{xor}}\mathrm{last},\quad j=b\mathbin{\mathrm{xor}}\mathrm{last},\quad k=c\mathbin{\mathrm{xor}}\mathrm{last}.$$

Then set i=max⁡(i,1)i=\max(i,1) and j=min⁡(j,n)j=\min(j,n). If i>ji>j, answer 00. Otherwise count elements strictly greater than kk in [i,j][i,j]. Replace last with this answer. xor means bitwise exclusive OR, not exponentiation.

Input

The first line gives nn, the next gives the array, and the next gives qq. Each of the following qq lines holds three encoded nonnegative integers a,b,ca,b,c.

Output

Print the count after each query, or 0 if the adjusted interval is empty.

Subtasks

  • Subtask 1 (10 points): 1≤n≤601\le n\le 60; 1≤q≤801\le q\le 80; 1≤ai≤1091\le a_i\le10^9; encoded values are nonnegative signed 32-bit integers; apply the specified endpoint adjustment after decoding.
  • Subtask 2 (20 points): 1≤n≤20001\le n\le 2000; 1≤q≤25001\le q\le 2500; 1≤ai≤1091\le a_i\le10^9; encoded values are nonnegative signed 32-bit integers; apply the specified endpoint adjustment after decoding.
  • Subtask 3 (30 points): 1≤n≤100001\le n\le 10000; 1≤q≤300001\le q\le 30000; 1≤ai≤1091\le a_i\le10^9; encoded values are nonnegative signed 32-bit integers; apply the specified endpoint adjustment after decoding.
  • Subtask 4 (40 points): 1≤n≤300001\le n\le 30000; 1≤q≤2000001\le q\le 200000; 1≤ai≤1091\le a_i\le10^9; encoded values are nonnegative signed 32-bit integers; apply the specified endpoint adjustment after decoding.

Examples

Input

6
8 9 3 5 1 9
5
2 3 5
3 3 7
0 0 11
0 0 2
3 7 4

Output

1
1
0
0
2

Explanation

The first query has last = 0, so it examines [2,3][2,3] with threshold 55 and returns 11. The second query must first be XOR-decoded using that answer. Decoded endpoints outside the array are adjusted; an empty resulting interval returns 00.