#P2839. [国家集训队] middle

[国家集训队] middle

Description

Given a sequence aa of length nn, let bb be the sorted version of aa. The median is defined as bn/2b_{n/2}, where aa and bb are 0-indexed and the division takes the floor.

You are given a sequence ss of length nn.

Answer QQ queries of the following type: among all subarrays of ss whose left endpoint is in [a,b][a, b] and right endpoint is in [c,d][c, d], find the maximum possible median.

Here a<b<c<da < b < c < d.

Positions are also 0-indexed.

I will use some method to force you to process the queries online.

Input Format

The first line contains the sequence length nn.

The next nn lines give the numbers of ss in order.

The next line contains QQ.

Then QQ lines follow, each containing a,b,c,da, b, c, d. Let the answer to the previous query be xx (if this is the first query, then x=0x=0).

Let the array $q=\{(a+x)\bmod n,(b+x)\bmod n,(c+x)\bmod n,(d+x)\bmod n\}$.

After sorting qq in ascending order, let the actual query be a=q0a=q_0, b=q1b=q_1, c=q2c=q_2, d=q3d=q_3.

The input is guaranteed to satisfy the conditions.

Output Format

Output QQ lines in order, each giving the answer to a query.

5
170337785
271451044
22430280
969056313
206452321
3
3 1 0 2
2 3 1 4
3 1 4 0
271451044
271451044
969056313

Hint

For 5%5\% of the testdata, n,Q100n, Q \leq 100.

For an additional 25%25\% of the testdata, n2000n \leq 2000.

For 100%100\% of the testdata, 1n200001 \leq n \leq 20000, 1Q250001 \leq Q \leq 25000, 1si1091 \leq s_i \leq 10^9.

Translated by ChatGPT 5