Codeforces 193.D Two Segments

D. Two Segments
time limit per test
5 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Nick has some permutation consisting of p integers from 1 to n. A segment [l, r] (l ≤ r) is a set of elements pi satisfying l ≤ i ≤ r.

Nick calls a pair of segments [a0, a1] and [b0, b1] (1 ≤ a0 ≤ a1 < b0 ≤ b1 ≤ n) good if all their (a1 - a0 + b1 - b0 + 2) elements, when sorted in ascending order, form an arithmetic progression with a difference of 1. That is, when they sorted in ascending order, the elements are in the form {x, x + 1, x + 2, ..., x + m - 1}, for some x and m.

Your task is to find the number of distinct pairs of good segments in the given permutation. Two pairs of segments are considered distinct if the sets of elements contained in these pairs of segments are distinct. For example, any segment [l, r(l < r) can be represented as a pair of segments, as [l, i] and [i + 1, r] (l ≤ i ≤ r). As all these pairs consist of the same set of elements, they are considered identical.

See the notes accompanying the sample tests for clarification.

Input

The first line contains integer n (1 ≤ n ≤ 3·105) — the permutation size. The second line contains n space-separated distinct integers pi, (1 ≤ pi ≤ n).

Output

Print a single integer — the number of good pairs of segments of permutation p.

Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64dspecifier.

Examples
input
Copy
3
1 2 3
output
3
input
Copy
5
1 4 5 3 2
output
10
input
Copy
5
5 4 3 1 2
output
10
Note

In the first sample the following pairs of segments are good: ([1, 1], [2, 2]); ([2, 2], [3, 3]); ([1, 2], [3, 3]). Pair of segments ([1, 1], [2, 3]) is by definition equivalent to pair ([1, 2], [3, 3]), since both of them covers the same set of elements, namely {1, 2, 3}.

In the third sample the following pairs of segments are good: ([4, 4], [5, 5]); ([3, 3],[4, 5]); ([2, 2],[3, 5]); ([1, 1],[2, 5]); ([3, 3],[5, 5]); ([2, 3],[5, 5]); ([1, 3],[5, 5]); ([2, 2],[3, 3]); ([1, 1],[2, 3]); ([1, 1],[2, 2]).

题目大意:选取两个不相交的区间,使得这两个区间里的数在一起排序后是连续的自然数.如果自然数相同,分的区间不同,则认为是同一种方案,问有多少种方案.

分析:个人认为挺难的一道题.

   因为数相同则认为是同一种方案,所以可以在值域上思考.如果值[l,r]合法,那么[l,r]至多被分成两段.可以考虑一种做法:枚举右端点r,每次统计1~r之间的i,有多少[i,r]被分成1段或2段.

   统计段数是一个比较麻烦的问题,[l,r]可以被分成若干段,因为只考虑1段和2段,所以维护段数的最小值和次小值(都是正整数)以及它们的数量.

   接下来考虑插入一个数x后对段数有什么影响(增量法).令x左边的数为l,右边的数为r.如果它们≤x,那么就已经被插入了.很容易发现这么一个规律:如果l,r都没有被插入,那么x将组成一个新的区间;如果l,r都被插入了,那么x将使区间数-1,否则区间数保持不变.

   在枚举右端点时,线段树维护的是在1~右端点中的i与右端点组成区间[i,右端点]的段数的最小值和次小值以及它们的数量,也就是说它维护的是一段区间的i的信息.弄清楚线段树维护的到底是啥非常重要.

   令l,r中较大的为max,较小的为min.如果l和r都没有插入.那么在[1,x]上+1.如果min没有插入,那么max+1到x 与 x组成区间分成的段数都要+1.为什么呢?max与x相邻,而max是比较大的那个数,左边的数一定比max小,所以max + 1到x的数都不可能与x相邻. 如果min和max都插入了.那么i在[1,min] 都要-1,i在[max + 1,i]都要+1.

   查询的话统计min1,min2≤2的sum就好了.

   需要注意的地方:1.因为在插入的时候涉及到区间[x,x]的修改,所以在建树的时候min1初始化为0.

           2.pushup的时候先统计左半区间的答案,千万不要交叉统计!不然可能会出现min2 < min1的情况.

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

const int maxn = 300010,inf = 0x7ffffff;

typedef long long ll;

int n,a[maxn],pos[maxn],sum1[maxn << 2],sum2[maxn << 2],min1[maxn << 2],tag[maxn << 2],min2[maxn << 2];
ll ans;

void solve(int o,int v,int c)
{
    if (v < min1[o])
    {
        min2[o] = min1[o];
        sum2[o] = sum1[o];
        min1[o] = v;
        sum1[o] = c;
    }
    else if(v == min1[o])
        sum1[o] += c;
    else if(v < min2[o])
    {
        min2[o] = v;
        sum2[o] = c;
    }
    else if (v == min2[o])
        sum2[o] += c;
}

void pushup(int o)
{
    sum1[o] = sum1[o * 2];
    min1[o] = min1[o * 2];
    sum2[o] = sum2[o * 2];
    min2[o] = min2[o * 2];
    solve(o,min2[o * 2 + 1],sum2[o * 2 + 1]);
    solve(o,min1[o * 2 + 1],sum1[o * 2 + 1]);
}

void pushdown(int o)
{
    if (tag[o])
    {
        tag[o * 2] += tag[o];
        tag[o * 2 + 1] += tag[o];
        min1[o * 2] += tag[o];
        min1[o * 2 + 1] += tag[o];
        min2[o * 2] += tag[o];
        min2[o * 2 + 1] += tag[o];
        tag[o] = 0;
    }
}

void build(int o,int l,int r)
{
    min1[o] = 0;
    sum1[o] = (r - l + 1);
    min2[o] = inf;
    sum2[o] = 0;
    if (l == r)
        return;
    int mid = (l + r) >> 1;
    build(o * 2,l,mid);
    build(o * 2 + 1,mid + 1,r);
    pushup(o);
}

void update(int o,int l,int r,int x,int y,int v)
{
    if (x <= l && r <= y)
    {
        min1[o] += v;
        min2[o] += v;
        tag[o] += v;
        return;
    }
    pushdown(o);
    int mid = (l + r) >> 1;
    if (x <= mid)
        update(o * 2,l,mid,x,y,v);
    if (y > mid)
        update(o * 2 + 1,mid + 1,r,x,y,v);
    pushup(o);
}

void query(int o,int l,int r,int x,int y)
{
    if (x <= l && r <= y)
    {
        if (min1[o] <= 2)
            ans += sum1[o];
        if (min2[o] <= 2)
            ans += sum2[o];
        return;
    }
    pushdown(o);
    int mid = (l + r) >> 1;
    if (x <= mid)
        query(o * 2,l,mid,x,y);
    if (y > mid)
        query(o * 2 + 1,mid + 1,r,x,y);
}

int main()
{
    scanf("%d",&n);
    for (int i = 1; i <= n; i++)
    {
        scanf("%d",&a[i]);
        pos[a[i]] = i;
    }
    build(1,1,n);
    for (int i = 1; i <= n; i++)
    {
        int l = -1,r = -1;
        if (pos[i] > 1 && i >= a[pos[i] - 1])
            l = a[pos[i] - 1];
        if (pos[i] < n && i >= a[pos[i] + 1])
            r = a[pos[i] + 1];
        if (l > r)
            swap(l,r);
        if (r == -1)
            update(1,1,n,1,i,1);
        else if(l == -1)
            update(1,1,n,r + 1,i,1);
        else
        {
            update(1,1,n,r + 1,i,1);
            update(1,1,n,1,l,-1);
        }
        query(1,1,n,1,i);
    }
    cout << ans - n << endl;

    return 0;
}

 

 

posted @ 2018-02-19 16:07  zbtrs  阅读(200)  评论(0编辑  收藏  举报