Educational codeforces Round 10C Foe Pairs

Source: Internet
Author: User

You are given a permutation p of length n. Also you is 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 does not contain any foe pairs. So you shouldn ' t count intervals (x, y) this contain at least one foe pair in it (the positions and order of the values FR Om the foe pair is 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 both foe pairs (3, 2) and (4, 2). But the interval (1, 2) are correct because it doesn ' t contain any foe pair.

Input
The first line contains integers n and m (1≤n, m≤3 105)-the length of the permutation p and the number of foe PA IRs.

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

Each of the next m lines contains the 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) This does not contain any foe pairs.

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

Example
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
9 |
2 7
Output
20
Note
The first example the intervals from the answer is (1, 1), (1, 2), (2, 2), (3, 3) and (4, 4).

To find the number of intervals that meet the criteria, the required interval cannot have the listed point pair, the given point pair is the value, and the interval is subscript.
Raise the point first, then find the longest interval length. The first thought is to enumerate the length and then raise the point, too time-consuming.
The point pairs are converted to subscript and then the right coordinates from small to large, and then from left to right to raise the point, the point to the left of the coordinates in the first left of the direct exclusion, because it is not possible to be included in the interval, so the right coordinate of the second point pair after the exclusion is the limit length of the interval, because it A longer interval must not meet the criteria.

#include <iostream> #include <stdio.h> #include <map> #include <set> #include <queue> # Include <algorithm> #include <vector> #include <math.h> #include <iterator> #include <
String.h> using namespace std;
typedef long Long LL;
int mo[4][2]={0,1,1,0,0,-1,-1,0};
const int maxn=0x3f3f3f3f;
const int sz=600005;
int n,m;
int A[sz],pos[sz],vis[sz]; struct node{int len,l,r;}
EDGE[SZ];
    BOOL CMP (node X,node y) {if (X.R==Y.R) {return x.l<y.l;
    }else{return x.r<y.r;
}} ll ans;
    void SS () {int j=1;
        for (int i=1;i<=n;i++) {while (edge[j].l<i&&j<=m) {j + +;
        } if (j<=m) ans+=edge[j].r-i;
        else ans+=n+1-i;
    cout<<i<< ' <<edge[j].r<<endl;
    }} int main () {int x,y,len,pre,cot;
    Freopen ("C:\\users\\administrator\\desktop\\r.txt", "R", stdin); while (scanf ("%d%d",&n,&m)!=eof) {for (int i=1;i<=n;i++) {scanf ("%d", &a[i]);
        Pos[a[i]]=i;
        } memset (Vis,0,sizeof (VIS));
        ans=0;
            for (int i=1;i<=m;i++) {scanf ("%d%d", &x,&y);
            X=pos[x];y=pos[y];
            if (x>y) swap (x, y);
            Edge[i].l=x;
        Edge[i].r=y;
        } sort (edge+1,edge+1+m,cmp);
        SS ();
    printf ("%i64d\n", ans);
} return 0; }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.