Foe Pairs (Codeforces 652C)
Tags: Others, two pointers

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.

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

Sample Inputs:
4 2
1 3 2 4
3 2
2 4

9 5
9 7 2 3 1 4 6 5 8
1 6
4 5
2 7
7 2
2 7

Sample Outputs:
5

20

Solution (Python):
from sys import stdin

def main():
    (n, m) = map(int, input().split())
    (pos, left) = ([0] * n, [0] * n)
    for (i, a) in enumerate(map(int, input().split())):
        pos[a - 1] = i
    for s in stdin.read().splitlines():
        (l, r) = map(int, s.split())
        (l, r) = (pos[l - 1], pos[r - 1])
        if l > r:
            (l, r) = (r, l)
        if l >= left[r]:
            left[r] = l + 1
    ans = l = 0
    for i in range(n):
        if left[i] > l:
            l = left[i]
        ans += i - l + 1
    print(ans)
main()

Solution (Python):
def main():
    from sys import stdin
    (n, m) = map(int, input().split())
    n += 1
    (aa, pos, duo) = ([0] * n, [0] * n, [0] * n)
    for (i, a) in enumerate(map(int, input().split()), 1):
        aa[i] = a
        pos[a] = i
    for s in stdin.read().splitlines():
        (x, y) = map(int, s.split())
        (px, py) = (pos[x], pos[y])
        if px > py:
            if duo[x] < py:
                duo[x] = py
        elif duo[y] < px:
            duo[y] = px
    res = mx = 0
    for (i, a) in enumerate(aa):
        if mx < duo[a]:
            mx = duo[a]
        res += i - mx
    print(res)
if __name__ == '__main__':
    main()

Solution (Python):
from sys import stdin

def main():
    (n, m) = map(int, input().split())
    (pos, left) = ([0] * n, [0] * n)
    for (i, a) in enumerate(map(int, input().split())):
        pos[a - 1] = i
    for s in stdin.read().splitlines():
        (l, r) = map(int, s.split())
        (l, r) = (pos[l - 1], pos[r - 1])
        if l > r:
            (l, r) = (r, l)
        if l >= left[r]:
            left[r] = l + 1
    ans = l = 0
    for i in range(n):
        if left[i] > l:
            l = left[i]
        ans += i - l + 1
    print(ans)
main()