loj#P554. 「LibreOJ Round #8」MIN&MAX II
「LibreOJ Round #8」MIN&MAX II
Description
For an -order permutation , we set up an undirected simple graph with vertices numbered from to .
We create an edge between each vertice and the nearest vertices in each side which correspond a greater (or less) value than .
Formally,in this graph, , the edge exists iff at least one of the following four conditions hold:
- , and no exists such that ;
- , and no exists such that ;
- , and no exists such that ;
- , and no exists such that .
For a segment , define its corresponding permutation as an -order permutation with the same relative orders of elements as ; that is, for all , the boolean value is the same as the boolean value .
For instance, for the permutation , is a permutation of length with the same relative orders of elements as , i.e. .
The chromatic number of an undirected graph 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 .
Given a permutation of length , please find the chromatic number of .
Additionally, please answer queries in the form of asking for: the greatest chromatic number among those of all corresponding permutations of each subsegment of , ; and the number of subsegment that achieve this maximum number .
(Formally,for each given ,,calculate the maximum possible value of ,called ,and $\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 .
The second line contains positive integers .
The third line contains an integer .
The following lines each contains two positive integers , denoting a query.
Output Format
Output contains lines in total. The first one should contain a positive integer denoting the chromatic number of , followed by lines each containing two integers for each query.
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
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, .
Detailed constraints are as follows (blank grids denote the same constraints as mentioned above):
Subtask # | Score (percentage) | ||
---|---|---|---|
- | |||
- |