#P50127. 「LibreOJ Round #8」MIN&MAX II

「LibreOJ Round #8」MIN&MAX II

Description

For an nn-order permutation pp, we set up an undirected simple graph G(p)G(p) with nn vertices numbered from 11 to nn. We create an edge between each vertice ii and the nearest vertices in each side which correspond a greater (or less) pp value than pip_i.
Formally,in this graph, u<v\forall u<v, the edge (u,v)(u, v) exists iff at least one of the following four conditions hold:

  • pu<pvp_u<p_v, and no u<i<vu<i<v exists such that pu<pip_u<p_i;
  • pu>pvp_u>p_v, and no u<i<vu<i<v exists such that pu>pip_u>p_i;
  • pu<pvp_u<p_v, and no u<i<vu<i<v exists such that pi<pvp_i<p_v;
  • pu>pvp_u>p_v, and no u<i<vu<i<v exists such that pi>pvp_i>p_v.

For a segment [l,r][l,r], define its corresponding permutation p[l:r]p[l:r] as an (rl+1)(r-l+1)-order permutation with the same relative orders of elements as pl,pl+1,,prp_l,p_{l+1},\cdots,p_r; that is, for all 1i<jrl+11 \leq i < j \leq r-l+1, the boolean value [pl1+i<pl1+j][p_{l-1+i} < p_{l-1+j}] is the same as the boolean value [(p[l:r])i<(p[l:r])j][(p[l:r])_i < (p[l:r])_j].
For instance, for the permutation q={1,4,2,5,3}q=\{1,4,2,5,3\}, q[2:4]q[2:4] is a permutation of length 33 with the same relative orders of elements as {4,2,5}\{4,2,5\}, i.e. {2,1,3}\{2,1,3\}.

The chromatic number of an undirected graph GG is the smallest number of colors needed to give each vertex a color such that every edge connects two vertices with different colors. We call this c(G)c(G).

Given a permutation pp of length nn, please find the chromatic number of G(p)G(p).
Additionally, please answer qq queries in the form of [l,r][l,r] asking for: the greatest chromatic number among those of all corresponding permutations of each subsegment of [l,r][l,r], maxc\mathrm{maxc}; and the number of subsegment that achieve this maximum number cnt\mathrm{cnt}.
(Formally,for each given l,rl,r,llrr\forall l\le l'\le r' \le r,calculate the maximum possible value of c(G(p[l:r]))c(G(p[l':r'])),called maxc\mathrm{maxc},and llrr[c(G(p[l:r]))=maxc]=cnt\sum_{l\leqslant l'\leqslant r'\leqslant r}[c(G(p[l':r']))=\mathrm{maxc}]=\mathrm{cnt} .)

Input format

The first line contains a positive integer nn.

The second line contains nn positive integers p1,p2,,pnp_1,p_2,\cdots ,p_n.

The third line contains an integer qq.

The following qq lines each contains two positive integers l,rl,r, denoting a query.

Output Format

Output contains q+1q+1 lines in total. The first one should contain a positive integer denoting the chromatic number of G(p)G(p), followed by qq lines each containing two integers maxc,cnt\mathrm{maxc},\mathrm{cnt} for each query.

Sample 1

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

The following picture describes G(p)G(p) and a way of coloring:

sampleexp.png

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

Constraints

For all test cases, 1n3×105,0q3×1051\le n\le 3\times 10^5,0\le q\le 3\times 10^5.

Detailed constraints are as follows (blank grids denote the same constraints as mentioned above):

Subtask # Score (percentage) nn qq
11 1010 10\le 10
22 1515 100\le 100 104\le 10^4
33 2000\le 2000
44 - =0=0
55 5\le 5
66 3030 -