CF1508F.Optimal Encoding

NOI/NOI+/CTSC

通过率:0%

时间限制:7.00s

内存限制:1024MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Touko's favorite sequence of numbers is a permutation a1,a2,…,ana_1, a_2, \dots, a_n of 1,2,…,n1, 2, \dots, n, and she wants some collection of permutations that are similar to her favorite permutation.

She has a collection of qq intervals of the form [li,ri][l_i, r_i] with 1≤li≤ri≤n1 \le l_i \le r_i \le n. To create permutations that are similar to her favorite permutation, she coined the following definition:

  • A permutation b1,b2,…,bnb_1, b_2, \dots, b_n allows an interval [l′,r′][l', r'] to holds its shape if for any pair of integers (x,y)(x, y) such that l′≤x<y≤r′l' \le x \lt y \le r', we have bx<byb_x \lt b_y if and only if ax<aya_x \lt a_y.
  • A permutation b1,b2,…,bnb_1, b_2, \dots, b_n is kk-similar if bb allows all intervals [li,ri][l_i, r_i] for all 1≤i≤k1 \le i \le k to hold their shapes.

Yuu wants to figure out all kk-similar permutations for Touko, but it turns out this is a very hard task; instead, Yuu will encode the set of all kk-similar permutations with directed acylic graphs (DAG). Yuu also coined the following definitions for herself:

  • A permutation b1,b2,…,bnb_1, b_2, \dots, b_n satisfies a DAG G′G' if for all edge u→vu \to v in G′G', we must have bu<bvb_u \lt b_v.
  • A kk-encoding is a DAG GkG_k on the set of vertices 1,2,…,n1, 2, \dots, n such that a permutation b1,b2,…,bnb_1, b_2, \dots, b_n satisfies GkG_k if and only if bb is kk-similar.

Since Yuu is free today, she wants to figure out the minimum number of edges among all kk-encodings for each kk from 11 to qq.

塔子最喜欢的数字序列是 1,2,…,n1, 2, \dots, n 的一个排列 a1,a2,…,ana_1, a_2, \dots, a_n,她希望得到一些与她最喜欢的排列“相似”的排列集合。

她拥有 qq 个形如 [li,ri][l_i, r_i] 的区间(满足 1≤li≤ri≤n1 \le l_i \le r_i \le n)。为了定义与她最喜欢的排列“相似”的排列,她引入了如下概念:

  • 排列 b1,b2,…,bnb_1, b_2, \dots, b_n 允许区间 [l′,r′][l', r'] 保持其形状,当且仅当:对任意整数对 (x,y)(x, y) 满足 l′≤x<y≤r′l' \le x \lt y \le r',均有 bx<byb_x \lt b_y 当且仅当 ax<aya_x \lt a_y。
  • 排列 b1,b2,…,bnb_1, b_2, \dots, b_n 是 kk-相似的,当且仅当 bb 允许所有区间 [li,ri][l_i, r_i](其中 1≤i≤k1 \le i \le k)均保持其形状。

悠希望能为塔子找出所有 kk-相似排列,但结果发现这是一项非常困难的任务;于是悠转而用有向无环图(DAG) 来编码所有 kk-相似排列的集合。悠还为自己定义了如下概念:

  • 排列 b1,b2,…,bnb_1, b_2, \dots, b_n 满足 DAG G′G',当且仅当:对 G′G' 中每一条边 u→vu \to v,均有 bu<bvb_u \lt b_v。
  • 一个 kk-编码 是定义在顶点集 {1,2,…,n}\{1, 2, \dots, n\} 上的一个 DAG GkG_k,使得排列 b1,b2,…,bnb_1, b_2, \dots, b_n 满足 GkG_k 当且仅当 bb 是 kk-相似的。

由于悠今天比较空闲,她希望对每个 k=1,2,…,qk = 1, 2, \dots, q,求出所有 kk-编码中边数的最小值。

输入格式

The first line contains two integers nn and qq (1≤n≤25 0001 \le n \le 25\,000, 1≤q≤100 0001 \le q \le 100\,000).

The second line contains nn integers a1,a2,…,ana_1, a_2, \dots, a_n which form a permutation of 1,2,…,n1, 2, \dots, n.

The ii-th of the following qq lines contains two integers lil_i and rir_i. (1≤li≤ri≤n1 \le l_i \le r_i \le n).

第一行包含两个整数 nn 和 qq(1≤n≤25 0001 \le n \le 25\,000,1≤q≤100 0001 \le q \le 100\,000)。

第二行包含 nn 个整数 a1,a2,…,ana_1, a_2, \dots, a_n,它们构成 1,2,…,n1, 2, \dots, n 的一个排列。

接下来的 qq 行中,第 ii 行包含两个整数 lil_i 和 rir_i(1≤li≤ri≤n1 \le l_i \le r_i \le n)。

输出格式

Print qq lines. The kk-th of them should contain a single integer — The minimum number of edges among all kk-encodings.

输出 qq 行。其中第 kk 行应包含一个整数——所有 kk-编码中边数的最小值。

输入输出样例

  • 输入#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 11-similar permutations must allow the interval [1,3][1, 3] to hold its shape. Therefore, the set of all 11-similar permutations is [3,4,2,1],[3,4,1,2],[2,4,1,3],[2,3,1,4]{[3, 4, 2, 1], [3, 4, 1, 2], [2, 4, 1, 3], [2, 3, 1, 4]}. The optimal encoding of these permutations is

  • All 22-similar permutations must allow the intervals [1,3][1, 3] and [2,4][2, 4] to hold their shapes. Therefore, the set of all 22-similar permutations is [3,4,1,2],[2,4,1,3]{[3, 4, 1, 2], [2, 4, 1, 3]}. The optimal encoding of these permutations is

  • All 33-similar permutations must allow the intervals [1,3][1, 3], [2,4][2, 4], and [1,4][1, 4] to hold their shapes. Therefore, the set of all 33-similar permutations only includes [2,4,1,3][2, 4, 1, 3]. The optimal encoding of this permutation is

对于第一个测试用例:

  • 所有 11-相似排列必须允许区间 [1,3][1, 3] 保持其形状。因此,所有 11-相似排列的集合为 [3,4,2,1],[3,4,1,2],[2,4,1,3],[2,3,1,4]{[3, 4, 2, 1], [3, 4, 1, 2], [2, 4, 1, 3], [2, 3, 1, 4]}。这些排列的最优编码为

  • 所有 22-相似排列必须允许区间 [1,3][1, 3] 和 [2,4][2, 4] 均保持其形状。因此,所有 22-相似排列的集合为 [3,4,1,2],[2,4,1,3]{[3, 4, 1, 2], [2, 4, 1, 3]}。这些排列的最优编码为

  • 所有 33-相似排列必须允许区间 [1,3][1, 3]、[2,4][2, 4] 和 [1,4][1, 4] 均保持其形状。因此,所有 33-相似排列的集合仅包含 [2,4,1,3][2, 4, 1, 3]。该排列的最优编码为

输入解题思路,AI测评打分。不知道怎么写?

首页