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

#CC0000071. Đếm số lớn hơn ngưỡng với truy vấn XOR (KQUERYO - K-Query Online)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Đếm số lớn hơn ngưỡng với truy vấn XOR (KQUERYO - K-Query Online)

KQUERYO - K-Query Online

Source: SPOJ

Version: Phuoc Hung OJ Extended

Problem Statement

Given an array, each query provides encoded integers a,b,ca,b,c. Decode l=a⊕lastl=a\oplus last, r=b⊕lastr=b\oplus last, k=c⊕lastk=c\oplus last, where lastlast is the previous answer (initially 00). Clamp ll to at least 11 and rr to at most nn. If l>rl>r, answer 00; otherwise count values strictly greater than kk in [l,r][l,r]. Set lastlast to this answer.

Input

The first line contains nn; the second contains nn array values; the third contains qq; then each of the qq lines contains encoded integers a,b,ca,b,c.

Output

Print one count per query; each printed count is used to decode the next query.

Subtasks

Original constraints: 1≤n≤300001\le n\le30000; 1≤q≤2000001\le q\le200000; 1≤ai≤1091\le a_i\le10^9. Queries are XOR-encoded; out-of-range indices follow the rules above.

  • 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 for decoded threshold kk; the index-adjustment rule still applies.
  • 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 for decoded threshold kk; the index-adjustment rule still applies.
  • 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 for decoded threshold kk; the index-adjustment rule still applies.

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 decoded query is [2,3][2,3] with threshold 55, giving 11. In the second query, XOR with last=1last=1 yields l=r=2l=r=2 and k=6k=6, giving 11 again.