Educational Codeforces Round 10 C. Foe Pairs

给你一个n的排列(n<=3*10^5),然后给你m个禁止的组合(m<=3*10^5),问你有多少个区间不包含任何一个禁止的组合。

思路:

1.O(n+m)

考虑每个位置,他的最远的右区间是多少,我们可以在输入m个禁止组合的时候,维护位置较小的那个数的最远位置,这样我们便知道了那些出现过的每个较小位置对应的最右位置。

然后逆序更新一遍每个位置的最右位置就行了,因为如果在他右边的位置的最远位置已经确定了,那么在他左边的最远位置的数也一定不能取到这个最远位置。


#include <set>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <cmath>
#include <vector>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define L(i) i<<1
#define R(i) i<<1|1
#define INF  0x3f3f3f3f
#define pi acos(-1.0)
#define eps 1e-12
#define maxn 300100
#define MOD 1000000007

int n,m;
int a[maxn];
int pos[maxn];
int cnt[maxn];

int main()
{
    int t;
    //scanf("%d",&t);
    while(scanf("%d%d",&n,&m) != EOF)
    {
        for(int i = 1; i <= n; i++)
        {
            scanf("%d",&a[i]);
            pos[a[i]] = i;
            cnt[i] = n+1;
        }
        for(int i = 0; i < m; i++)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            if(pos[x] > pos[y])
                cnt[pos[y]] = min(cnt[pos[y]],pos[x]);
            else
                cnt[pos[x]] = min(cnt[pos[x]],pos[y]);
        }
        long long ans = 0;
        for(int i = n-1; i > 0; i--)
            cnt[i] = min(cnt[i],cnt[i+1]);
//        for(int i = 1; i <= n; i++)
//            printf("%d %d\n",i,cnt[i]);
        for(int i = 1; i <= n; i++)
            ans += (long long)(cnt[i] - i);
        printf("%I64d\n",ans);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值