luogu#P10371. 「LAOI-4」石头

    ID: 9830 远端评测题 2500ms 512MiB 尝试: 0 已通过: 0 难度: 7 上传者: 标签>洛谷原创O2优化链表单调栈洛谷比赛

「LAOI-4」石头

Problem Description

There is a permutation aa of length nn. At the beginning, you may choose any one number to paint white. Then, at each next step, you may paint white the smallest number that is adjacent to any already-white number. Clearly, after nn steps, all numbers will be painted white.

Now we call a pair (i,j)(i,j) a good pair if it satisfies the following:

  • 1ijn1\leq i\leq j\leq n.
  • There exists a kk such that: if you start painting white from aia_i, then aja_j will be painted white at step kk; and if you start painting white from aja_j, then aia_i will also be painted white at step kk.

Find the number of good pairs.

Input Format

Because the input is too large, a helper generator is provided.

int a[/*数组大小*/];
namespace GenHelper
{
    unsigned z1, z2, z3, z4, b;
    unsigned rand_()
    {
        b = ((z1 << 6) ^ z1) >> 13;
        z1 = ((z1 & 4294967294U) << 18) ^ b;
        b = ((z2 << 2) ^ z2) >> 27;
        z2 = ((z2 & 4294967288U) << 2) ^ b;
        b = ((z3 << 13) ^ z3) >> 21;
        z3 = ((z3 & 4294967280U) << 7) ^ b;
        b = ((z4 << 3) ^ z4) >> 12;
        z4 = ((z4 & 4294967168U) << 13) ^ b;
        return (z1 ^ z2 ^ z3 ^ z4);
    }
}
void srand_(unsigned x, int n)
{
    using namespace GenHelper;
    z1 = x;
    z2 = (~x) ^ 0x233333333U;
    z3 = x ^ 0x1234598766U;
    z4 = (~x) + 51;
    for (int i = 1; i <= n; ++i)
        a[i] = i;
    if (!x)
        return;
    for (int i = 1; i <= n; ++i)
    {
        int j = rand_() % i + 1, k;
        k = a[i], a[i] = a[j], a[j] = k;
    }
}

The input contains two integers nn and ss, representing the permutation length and the random seed, respectively.

You must call srand_(s,n) exactly once. After that, aia_i is already stored in a[i]. Note that the index starts from 11. Do not forget to set the array size.

Output Format

Output one positive integer in one line, representing the number of good pairs.

5 114514
9
10 113037
23
20 73555
49

Hint

Sample Explanation

For sample group #1, a={4,3,1,5,2}a=\{4,3,1,5,2\}. The good pairs are: $(1,1),(1,3),(1,5),(2,2),(2,3),(2,4),(3,3),(4,4),(5,5)$.

Constraints

"This problem uses bundled tests."

Subtask ID nn Special property Score
11 103\le10^3 None 1515
22 105\le10^5 3030
33 107\le10^7 A\text{A} 55
44 None 5050

For 100%100\% of the testdata, it is guaranteed that 1n1071\le n\le 10^7, 0s1145140\leq s\leq 114514, and aa is a permutation of nn.

Special property A\text{A}: aia_i is strictly increasing, and in this case s=0s=0.

Translated by ChatGPT 5