Cow Tennis Tournament【Codeforces283E】【线段树+思维】

106 篇文章 0 订阅
46 篇文章 0 订阅

Codeforces Round #174 (Div. 1) E


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.


  最后,好像用“%lld”输入,以及long long也是没有问题的,我当时忘记改了,也是过了的。

  这道题做了好久了,都快记不清初始的题意了,三天了吧,才想到怎样去做的,并且Debug了好久,终于还是弄出来了。

  题意:我们有一种操作,就是在判断的时候,改变[l, r]的比较时候的大小使得其小的变成大的,大的变成原来的小的,譬如有「1、2、3」一次操作之后,1>2、2>3,但是再经历一次操作之后,会变回去「1、2、3」就成了原来的样子了,我们想知道的是会构成几个三元环,满足{ x<y, y<z, z<x }这样的形式。

  思路:一开始的时候,真的没有任何的思路,看了tips之后,想到了反过来求解的方式(就不说一开始愚蠢的想法了),我们知道,如果所有的点都能构成三元环的话,那么总数就是C(N, 3),但是很显然,有时候是不可能的,存在着那么些绝对会破坏这个链的存在。那我们就要去剪掉这部分,发现假如存在一个数,大于其他的两个数的话,就是意味着这个三个数是一定不能构成三元环的,我们就需要减去这样的存在。

  那么,这里需要怎么做?我们继续推下去,就推1、2、3、4、5这样的样例吧,假如我们有三种改变[1, 4]、[2, 4]、[2, 5]这样的三种改变,那么我们先看到“1”这个点,后面比它小的是不是只有{2, 3, 4}因为他们被反转了,那么比1小的组合就是C(3, 2) = 3,再往下看,看到“2”,不难发现,它的后面,3、4被反转了3次,依然是比2小的,5也被反转了一次,也是比2小的了;……我们按照这个想法去往下推,实际上可以发现这样的一个定理:

  我们现在看到某数的时候,前面的数如果被改变的次数(与该数同时被改变的次数)是偶数次,那么一定依然比它小的;后面的数(同时改变)改变奇数次的,会变得比它小,所以我们最后想要的点就是这样的多个C(sum, 2)的和,再去用总的C(N, 2)减去它即可。

#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#define lowbit(x) ( x&(-x) )
#define pi 3.141592653589793
#define e 2.718281828459045
#define INF 0x3f3f3f3f3f3f3f3f
#define HalF (l + r)>>1
#define lsn rt<<1
#define rsn rt<<1|1
#define Lson lsn, l, mid
#define Rson rsn, mid+1, r
#define QL Lson, ql, qr
#define QR Rson, ql, qr
#define myself rt, l, r
#define MP(a, b) make_pair(a, b)
#define MP3(a, b, c) MP(MP(a, b), c)
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int maxN = 3e5 + 7;
vector<int> add[maxN], del[maxN];
int N, K, a[maxN], tree[maxN<<2], lazy[maxN<<2];
inline void pushup(int rt) { tree[rt] = tree[lsn] + tree[rsn]; }
inline void pushdown(int rt, int l, int r)
{
    if(lazy[rt])
    {
        int mid = HalF;
        lazy[lsn] ^= 1; lazy[rsn] ^= 1;
        tree[lsn] = mid - l + 1 - tree[lsn]; tree[rsn] = r - mid - tree[rsn];
        lazy[rt] = 0;
    }
}
inline void update(int rt, int l, int r, int ql, int qr)
{
    if(ql <= l && qr >= r)
    {
        lazy[rt] ^= 1;
        tree[rt] = r - l + 1 - tree[rt];
        return;
    }
    int mid = HalF;
    pushdown(myself);
    if(qr <= mid) update(QL);
    else if(ql > mid) update(QR);
    else { update(QL); update(QR); }
    pushup(rt);
}
inline int query(int rt, int l, int r, int ql, int qr)
{
    if(ql <= l && qr >= r) return tree[rt];
    int mid = HalF;
    pushdown(myself);
    if(qr <= mid) return query(QL);
    else if(ql > mid) return query(QR);
    else return query(QL) + query(QR);
}
int main()
{
    scanf("%d%d", &N, &K);
    for(int i=1; i<=N; i++) scanf("%d", &a[i]);
    sort(a + 1, a + N + 1);
    for(int i=1, l, r, x, y; i<=K; i++)
    {
        scanf("%d%d", &l, &r);
        x = (int)(lower_bound(a + 1, a + N + 1, l) - a);
        y = (int)(upper_bound(a + 1, a + N + 1, r) - a - 1);
        if(x > y) continue;
        add[x].push_back(y);
        del[y].push_back(x);
    }
    ll ans = 1LL * N * (N - 1) * (N - 2)/6;
    for(int i=1, len, sum; i<=N; i++)
    {
        len = (int)add[i].size();
        for(int j=0; j<len; j++) update(1, 1, N, i, add[i][j]);
        sum = 0;
        if(i > 1) sum = i - query(1, 1, N, 1, i - 1) - 1;
        if(i < N) sum += query(1, 1, N, i + 1, N);
        ans -= 1LL * sum * (sum - 1) / 2;
        len = (int)del[i].size();
        for(int j=0; j<len; j++) update(1, 1, N, del[i][j], i);
    }
    printf("%lld\n", ans);
    return 0;
}

 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Wuliwuliii

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值