CF1508F.Optimal Encoding
普及/提高-
通过率:0%
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
Touko's favorite sequence of numbers is a permutation a1,a2,…,an of 1,2,…,n , and she wants some collection of permutations that are similar to her favorite permutation.
She has a collection of q intervals of the form [li,ri] with 1≤li≤ri≤n . To create permutations that are similar to her favorite permutation, she coined the following definition:
- A permutation b1,b2,…,bn allows an interval [l′,r′] to holds its shape if for any pair of integers (x,y) such that l′≤x<y≤r′ , we have bx<by if and only if ax<ay .
- A permutation b1,b2,…,bn is k -similar if b allows all intervals [li,ri] for all 1≤i≤k to hold their shapes.
Yuu wants to figure out all k -similar permutations for Touko, but it turns out this is a very hard task; instead, Yuu will encode the set of all k -similar permutations with directed acylic graphs (DAG). Yuu also coined the following definitions for herself:
- A permutation b1,b2,…,bn satisfies a DAG G′ if for all edge u→v in G′ , we must have bu<bv .
- A k -encoding is a DAG Gk on the set of vertices 1,2,…,n such that a permutation b1,b2,…,bn satisfies Gk if and only if b is k -similar.
Since Yuu is free today, she wants to figure out the minimum number of edges among all k -encodings for each k from 1 to q .
输入格式
The first line contains two integers n and q ( 1≤n≤25000 , 1≤q≤100000 ).
The second line contains n integers a1,a2,…,an which form a permutation of 1,2,…,n .
The i -th of the following q lines contains two integers li and ri . ( 1≤li≤ri≤n ).
输出格式
Print q lines. The k -th of them should contain a single integer — The minimum number of edges among all k -encodings.
输入输出样例
输入#1
4 3 2 4 1 3 1 3 2 4 1 4
输出#1
2 4 3
输入#2
8 4 3 7 4 8 1 5 2 6 3 6 1 6 3 8 1 8
输出#2
3 5 9 7
输入#3
10 10 10 5 1 2 7 3 9 4 6 8 2 2 4 5 6 8 4 10 4 4 2 7 2 2 7 8 3 7 2 10
输出#3
0 1 3 6 6 9 9 9 9 8
说明/提示
For the first test case:
- All 1 -similar permutations must allow the interval [1,3] to hold its shape. Therefore, the set of all 1 -similar permutations is {[3,4,2,1],[3,4,1,2],[2,4,1,3],[2,3,1,4]} . The optimal encoding of these permutations is
- All 2 -similar permutations must allow the intervals [1,3] and [2,4] to hold their shapes. Therefore, the set of all 2 -similar permutations is {[3,4,1,2],[2,4,1,3]} . The optimal encoding of these permutations is
- All 3 -similar permutations must allow the intervals [1,3] , [2,4] , and [1,4] to hold their shapes. Therefore, the set of all 3 -similar permutations only includes [2,4,1,3] . The optimal encoding of this permutation is