zoukankan      html  css  js  c++  java
  • Educational Codeforces Round 10 C. Foe Pairs 水题

    C. Foe Pairs

    题目连接:

    http://www.codeforces.com/contest/652/problem/C

    Description

    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·105) — 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.

    Sample Input

    4 2
    1 3 2 4
    3 2
    2 4

    Sample Output

    5

    Hint

    题意

    给你一个n个数的排列

    然后给你m组数

    然后问你一共有多少个区间不包含这m组数。

    题解:

    对于每一个位置,我们直接暴力算出这个位置最多往左边延展多少。

    但是显然这样子会有重复的,我们在扫的时候,维护一个当前最多往左边延展多少就好了

    这样就不会有重复的了

    代码

    #include<bits/stdc++.h>
    using namespace std;
    const int maxn = 3e5+7;
    
    int n,m,p[maxn],t[maxn];
    int c[maxn];
    int main()
    {
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&p[i]);
            t[p[i]]=i;
        }
        for(int i=1;i<=m;i++)
        {
            int x,y;scanf("%d%d",&x,&y);
            int a = 0,b = 0;
            a=max(t[x],t[y]),b=min(t[x],t[y]);
            c[a]=max(c[a],b);
        }
        long long ans = 0;
        int l = 0;
        for(int i=1;i<=n;i++)
        {
            l=max(c[i],l);
            ans+=i-l;
        }
        cout<<ans<<endl;
    }
  • 相关阅读:
    基本几何变换——以鼠标为中心进行缩放
    jquery radio/checkbox change 事件不能触发的问题
    CCF YOCSEF“面向科学的大数据管理与分析”报告会即将举行
    mac iTunes启动失败,声称iTunes文件夹被锁定
    opencv2.3 + visual studio 2010 编译配置方法
    Qt QGLWidget 不能够实时刷新的问题
    在mac os x中建立事件钩子
    二叉树前序、中序、后序、层次遍历
    CRC从原理到实现
    异步FIFO的FPGA实现
  • 原文地址:https://www.cnblogs.com/qscqesze/p/5330279.html
Copyright © 2011-2022 走看看