Codeforces Round #174 (Div. 1)E. Cow Tennis Tournament【线段树+组合计数】

E. Cow Tennis Tournament

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Farmer John is hosting a tennis tournament with his n cows. Each cow has a skill level si, and no two cows having the same skill level. Every cow plays every other cow exactly once in the tournament, and each cow beats every cow with skill level lower than its own.

However, Farmer John thinks the tournament will be demoralizing for the weakest cows who lose most or all of their matches, so he wants to flip some of the results. In particular, at k different instances, he will take two integers ai, bi (ai < bi) and flip all the results between cows with skill level between ai and bi inclusive. That is, for any pair x, y  he will change the result of the match on the final scoreboard (so if x won the match, the scoreboard will now display that y won the match, and vice versa). It is possible that Farmer John will change the result of a match multiple times. It is not guaranteed that ai and bi are equal to some cow's skill level.

Farmer John wants to determine how balanced he made the tournament results look. In particular, he wants to count the number of triples of cows (p, q, r) for which the final leaderboard shows that cow p beats cow q, cow q beats cow r, and cow r beats cow p. Help him determine this number.

Note that two triples are considered different if they do not contain the same set of cows (i.e. if there is a cow in one triple that is not in the other).

Input

On the first line are two space-separated integers, n and k (3 ≤ n ≤ 105; 0 ≤ k ≤ 105). On the next line are n space-separated distinct integers, s1, s2, ..., sn (1 ≤ si ≤ 109), denoting the skill levels of the cows. On the next k lines are two space separated integers, ai and bi(1 ≤ ai < bi ≤ 109) representing the changes Farmer John made to the scoreboard in the order he makes it.

Output

A single integer, containing the number of triples of cows (p, q, r) for which the final leaderboard shows that cow p beats cow q, cow qbeats cow r, and cow r beats cow p.

Please do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64dspecifier.

Examples

input

Copy

3 2
1 2 3
1 2
2 3

output

Copy

1

input

Copy

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

output

Copy

3

Note

In the first sample, cow 3 > cow 1, cow 3 > cow 2, and cow 2 > cow 1. However, the results between cows 1 and 2 and cows 2 and 3 are flipped, so now FJ's results show that cow 1 > cow 2, cow 2 > cow 3, and cow 3 > cow 1, so cows 1, 2, and 3 form a balanced triple.

 

 

分析:我们把大小关系转化为单向边的关系,若一个数大于另一个数,则边的方向是这个数指向另一个数。若三个点可以形成一个环的话,每个点的出度与入度一定为1,若不是,那么一定有一个出度为2和一个入度为2的点。

那么我们用总方案数减去不合法的就是答案了。

总方案数C_n^3,不合法的即是上述的有一个出度为2和一个入度为2的点的三元组,那么我们就去枚举这个出度为2的点。

对于每个点,找到所有比他小的设为x,C_x^2即是这个点的贡献。

ans=C_n^3-\sum _{i=1}^nC_{x_i}^2

再考虑如何维护每个点对其他点的大小关系。

由题意可以知道,如果某个区间没有包括这个点,那么这个区间对这个点和其他的点的大小关系是没有影响的,那么我们只用计算包含了这个点的所有区间,对于本来就小于它的点,若被区间覆盖计数次数是偶数,那么依然小于它,对于本来大于它的点,计数次数是奇数,那么就小于它。所以只需要用线段树维护一个区间01反转就可以了。加入区间时按照左端点排序,删除区间时按照右端点排序。

 

#include "bits/stdc++.h"

using namespace std;
int a[100004];

struct mat {
    int l, r;

    friend bool operator<(mat a, mat b) {
        return a.r < b.r;
    }
} l[100004];

bool cmp(mat a, mat b) {
    return a.l < b.l;
}

struct node {
    int l, r, laz, w;
} t[400004];

inline void pushdown(int rt) {
    if (t[rt].laz == 0)return;
    t[rt].laz = 0;
    t[rt << 1].laz ^= 1;
    t[rt << 1 | 1].laz ^= 1;
    t[rt << 1].w = t[rt << 1].r - t[rt << 1].l + 1 - t[rt << 1].w;
    t[rt << 1 | 1].w = t[rt << 1 | 1].r - t[rt << 1 | 1].l + 1 - t[rt << 1 | 1].w;
}

inline void build(int rt, int l, int r) {
    t[rt].l = l, t[rt].r = r, t[rt].w = t[rt].laz = 0;
    if (l != r) {
        int mid = (l + r) >> 1;
        build(rt << 1, l, mid);
        build(rt << 1 | 1, mid + 1, r);
    }
}

inline void update(int rt, int l, int r) {
    if (l > r)return;
    else if (t[rt].l >= l && t[rt].r <= r) {
        t[rt].laz ^= 1;
        t[rt].w = t[rt].r - t[rt].l + 1 - t[rt].w;
    } else {
        pushdown(rt);
        int mid = (t[rt].l + t[rt].r) >> 1;
        if (l <= mid)update(rt << 1, l, r);
        if (r > mid)update(rt << 1 | 1, l, r);
        t[rt].w = t[rt << 1].w + t[rt << 1 | 1].w;
    }
}

inline int que(int rt, int l, int r) {
    if (l > r)return 0;
    if (t[rt].l >= l && t[rt].r <= r) return t[rt].w;
    pushdown(rt);
    int res = 0;
    int mid = (t[rt].l + t[rt].r) >> 1;
    if (l <= mid)res += que(rt << 1, l, r);
    if (r > mid)res += que(rt << 1 | 1, l, r);
    return res;
}

int main() {
    long long n, k;
    cin >> n >> k;
    for (int i = 1; i <= n; ++i) {
        scanf("%d", &a[i]);
    }
    sort(a + 1, a + 1 + n);
    for (int i = 0; i < k; ++i) {
        scanf("%d%d", &l[i].l, &l[i].r);
        l[i].l = lower_bound(a + 1, a + 1 + n, l[i].l) - a;
        l[i].r = upper_bound(a + 1, a + 1 + n, l[i].r) - a - 1;
        if (l[i].l > l[i].r) {
            i--;
            k--;
        }
    }
    sort(l, l + k, cmp);
    long long ans = 0;
    build(1, 1, n);
    multiset<mat> s;
    int cnt = 0;
    for (int i = 1; i <= n; ++i) {
        while (cnt < k && l[cnt].l <= i) {
            update(1, l[cnt].l, l[cnt].r);
            s.insert(l[cnt++]);
        }
        while (!s.empty()) {
            mat temp = *s.begin();
            if (temp.r >= i)break;
            update(1, temp.l, temp.r);
            s.erase(s.begin());
        }
        long long res = i - 1 - que(1, 1, i - 1) + que(1, i + 1, n);
        ans += (res - 1) * res / 2;
    }
    printf("%lld\n", n * (n - 1) / 2 * (n - 2) / 3 - ans);
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值