zoukankan      html  css  js  c++  java
  • codeforces 652C C. Foe Pairs(尺取法+线段树查询一个区间覆盖线段)

    题目链接:

    C. Foe Pairs

    time limit per test
    1 second
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output
     

    You are given a permutation p of length n. Also you are given m foe pairs (ai, bi) (1 ≤ ai, bi ≤ n, ai ≠ bi).

    Your task is to count the number of different intervals (x, y) (1 ≤ x ≤ y ≤ n) that do not contain any foe pairs. So you shouldn't count intervals (x, y) that contain at least one foe pair in it (the positions and order of the values from the foe pair are not important).

    Consider some example: p = [1, 3, 2, 4] and foe pairs are {(3, 2), (4, 2)}. The interval (1, 3) is incorrect because it contains a foe pair(3, 2). The interval (1, 4) is also incorrect because it contains two foe pairs (3, 2) and (4, 2). But the interval (1, 2) is correct because it doesn't contain any foe pair.

    Input
     

    The first line contains two integers n and m (1 ≤ n, m ≤ 3·10^5) — the length of the permutation p and the number of foe pairs.

    The second line contains n distinct integers pi (1 ≤ pi ≤ n) — the elements of the permutation p.

    Each of the next m lines contains two integers (ai, bi) (1 ≤ ai, bi ≤ n, ai ≠ bi) — the i-th foe pair. Note a foe pair can appear multiple times in the given list.

    Output
     

    Print the only integer c — the number of different intervals (x, y) that does not contain any foe pairs.

    Note that the answer can be too large, so you should use 64-bit integer type to store it. In C++ you can use the long long integer type and in Java you can use long integer type.

    Examples
     
    input
    4 2
    1 3 2 4
    3 2
    2 4
    output
    5
    input
    9 5
    9 7 2 3 1 4 6 5 8
    1 6
    4 5
    2 7
    7 2
    2 7
    output
    20
    Note

    In the first example the intervals from the answer are (1, 1), (1, 2), (2, 2), (3, 3) and (4, 4).

    题意:

    给一个数组,问有多少对(i,j)满足[a[i],a[j]]中不完整包含任何一个数对;

    思路:

    暴力是的复杂度太高,我先把这些数对都处理成原数组的位置,然后把它们搞进线段树里,就是把右端点当成左端点插入线段树时的位置,查询一个区间[i,j]时是否包含一个完整的线段可以看[i,j]中最大的左端点是多大,如果最大的左端点>=i时,我们就知道[i,j]中至少包含一个完整的线段;然后再枚举左端点,尺取法找右端点;然后把长度都加起来就是结果啦;

    AC代码:

    /*2014300227    652C - 11    GNU C++11    Accepted    311 ms    18796 KB*/
    #include <bits/stdc++.h>
    using namespace std;
    const int N=3e5+4;
    typedef long long ll;
    int n,a[N],fa[N],m,l,r;
    struct Tree
    {
        int l,r,ans;
    };
    Tree tree[4*N];
    void Pushup(int node)
    {
        tree[node].ans=max(tree[2*node].ans,tree[2*node+1].ans);
    }
    void build(int node,int L,int R)
    {
        tree[node].l=L;
        tree[node].r=R;
        if(L==R)
        {
            tree[node].ans=0;
            return ;
        }
        int mid=(L+R)>>1;
        build(2*node,L,mid);
        build(2*node+1,mid+1,R);
        Pushup(node);
    }
    void update(int node,int num,int pos)
    {
        if(tree[node].l==tree[node].r&&tree[node].r==pos)
        {
            tree[node].ans=max(tree[node].ans,num);
            return ;
        }
        int mid=(tree[node].l+tree[node].r)>>1;
        if(pos<=mid)update(2*node,num,pos);
        else update(2*node+1,num,pos);
        Pushup(node);
    }
    int query(int node,int L,int R)
    {
        if(L<=tree[node].l&&R>=tree[node].r)
        {
            return tree[node].ans;
        }
        int mid=(tree[node].l+tree[node].r)>>1;
        if(R<=mid)return query(2*node,L,R);
        else if(L>mid)return query(2*node+1,L,R);
        else return max(query(2*node,L,mid),query(2*node+1,mid+1,R));
    }
    struct PO
    {
        int l,r;
    }po[N];
    int main()
    {
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
            fa[a[i]]=i;
        }
        build(1,1,n);
        for(int i=0;i<m;i++)
        {
            scanf("%d%d",&l,&r);
            po[i].l=min(fa[l],fa[r]);
            po[i].r=max(fa[l],fa[r]);
            update(1,po[i].l,po[i].r);//更新;
        }
        ll ans=0;
        int l=1,r=1;
        for(int i=1;i<=n;i++)
        {
            l=i;
            while(r<=n)
            {
                int q=query(1,i,r);//查询[i,r]中的最大值,即是包含于这个区间的线段最大的左端点;
                if(q<i)r++;//r为以l为左端点满足要求的最长的区间的右端点+1;
                else break;
            }
            ans+=(ll)(r-l);
        }
        cout<<ans<<"
    ";
        return 0;
    }
  • 相关阅读:
    Ajax请求ashx 返回 json 格式数据常见问题
    netbeans 优化设置
    Java基础static的探究
    mybatis中:selectKey返回最近插入记录的id
    Java map 详解
    Statement和PreparedStatement的异同
    css3 @media支持ie8用respond.js 解决IE6~8的响应式布局问题
    apply和call
    兼容性问题:backgroud-size支持IE8浏览器的方法
    json对象和json字符串之间的转化
  • 原文地址:https://www.cnblogs.com/zhangchengc919/p/5395647.html
Copyright © 2011-2022 走看看