Discription

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 ywon 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 q beats 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 %I64d specifier.

Examples

Input
3 2
1 2 3
1 2
2 3
Output
1
Input
5 3
5 9 4 1 7
1 7
2 8
3 9
Output
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.

jzh大佬给学弟学妹们讲课的课件里,唯一一个不是弱智题的就是这个了2333,然鹅一找原题,mdzz数据范围后面加了俩0,有毒。。。

如果n<=1000的话,我们可以很容易的用差分去维护区间覆盖的问题,然后暴力计算每两个牛之间的比赛结果就好了。。。

所以n<=1e5怎么做呢??

我们只要先求出每个人最后赢的场数,就可以直接算出不合法的三元组数量,再用C(n,3)减去这个就是答案了。

那么如何快速计算每个人赢的场数呢?

考虑扫描线,把修改存在vector里,先倒着扫一遍,查询s[j] < s[i] 且 i赢j的个数;再倒着扫一遍,。。。。把修改看成区间异或,查询看成区间1的个数,然后这就是基本线段树操作了23333

#include<bits/stdc++.h>
#define ll long long
using namespace std;
#define pb push_back
#define lc (o<<1)
#define mid (l+r>>1)
#define rc ((o<<1)|1)
const int maxn=100005;
vector<int> L[maxn],R[maxn];
int a[maxn],n,num[maxn],k,X,Y,len[maxn*4];
int win[maxn],le,ri,sum[maxn*4],tag[maxn*4],w;
ll ans=0; inline void maintain(int o){ sum[o]=sum[lc]+sum[rc];} inline void work(int o){ tag[o]^=1,sum[o]=len[o]-sum[o];} inline void pushdown(int o){
if(tag[o]){
tag[o]=0;
work(lc),work(rc);
}
} void build(int o,int l,int r){
len[o]=r-l+1;
if(l==r){ sum[o]=1; return;}
build(lc,l,mid);
build(rc,mid+1,r);
maintain(o);
} void update(int o,int l,int r){
if(l>=le&&r<=ri){ work(o); return;}
pushdown(o);
if(le<=mid) update(lc,l,mid);
if(ri>mid) update(rc,mid+1,r);
maintain(o);
} void query(int o,int l,int r){
if(l>=le&&r<=ri){ w+=sum[o]; return;}
pushdown(o);
if(le<=mid) query(lc,l,mid);
if(ri>mid) query(rc,mid+1,r);
} inline void solve(){
build(1,1,n); for(int i=n;i;i--){
ri=i; for(int j=L[i].size()-1;j>=0;j--) le=L[i][j],update(1,1,n); w=0,le=1,ri=i-1;
if(le<=ri) query(1,1,n);
win[i]+=w; // cout<<i<<' '<<w<<endl;
} memset(sum,0,sizeof(sum));
memset(tag,0,sizeof(tag)); for(int i=1;i<=n;i++){
le=i;
for(int j=R[i].size()-1;j>=0;j--) ri=R[i][j],update(1,1,n); w=0,le=i+1,ri=n;
if(le<=ri) query(1,1,n);
win[i]+=w; // cout<<i<<' '<<win[i]<<endl;
} for(int i=1;i<=n;i++) ans-=win[i]*(ll)(win[i]-1)>>1;
} int main(){
scanf("%d%d",&n,&k);
for(int i=1;i<=n;i++) scanf("%d",a+i),num[i]=a[i]; sort(num+1,num+n+1);
// unique(num+1,num+n+1);
for(int i=1;i<=n;i++) a[i]=lower_bound(num+1,num+n+1,a[i])-num; while(k--){
scanf("%d%d",&X,&Y);
X=lower_bound(num+1,num+n+1,X)-num;
Y=upper_bound(num+1,num+n+1,Y)-num-1; if(!X||!Y) continue; L[Y].pb(X),R[X].pb(Y);
} ans=n*(ll)(n-1)*(ll)(n-2)/6ll,solve(); cout<<ans<<endl; return 0;
}

  

CodeForces - 283E Cow Tennis Tournament的更多相关文章

  1. 283E&EZOJ #89 Cow Tennis Tournament

    传送门 分析 我们考虑用所有的情况减去不合法的情况 不难想出所有情况为$C_n^3$ 于是我们考虑不合法的情况 我们知道对于一个不合法的三元组$(a,b,c)$一定是修改后$a<b,b>c ...

  2. Educational Codeforces Round 8 A. Tennis Tournament 暴力

    A. Tennis Tournament 题目连接: http://www.codeforces.com/contest/628/problem/A Description A tennis tour ...

  3. Codeforces CF#628 Education 8 A. Tennis Tournament

    A. Tennis Tournament time limit per test 1 second memory limit per test 256 megabytes input standard ...

  4. CF 628A --- Tennis Tournament --- 水题

    CF 628A 题目大意:给定n,b,p,其中n为进行比赛的人数,b为每场进行比赛的每一位运动员需要的水的数量, p为整个赛程提供给每位运动员的毛巾数量, 每次在剩余的n人数中,挑选2^k=m(m & ...

  5. Codeforces 678E. Another Sith Tournament(概率DP,状压)

    Codeforces 678E. Another Sith Tournament 题意: n(n<=18)个人打擂台赛,给定任意两人对决的胜负概率,比赛规则:可指定一人作为最开始的擂主,每次可指 ...

  6. Codeforces Educational Codeforces Round 8 A. Tennis Tournament

    大致题意: 网球比赛,n个參赛者,每场比赛每位选手b瓶水+裁判1瓶水,所有比赛每一个參赛者p条毛巾 每一轮比赛有2^k个人參加比赛(k为2^k<=n中k的最大值),下一轮晋级人数是本轮每场比赛的 ...

  7. Codeforces 735C:Tennis Championship(数学+贪心)

    http://codeforces.com/problemset/problem/735/C 题意:有n个人打锦标赛,淘汰赛制度,即一个人和另一个人打,输的一方出局.问这n个人里面冠军最多能赢多少场, ...

  8. CodeForces - 1209D Cow and Snacks 并查集

    CodeForces - 1209D 题意 现在n种点心,每种点心只有一份,有k位客人,每位客人有两种想要吃的点心,你可以安排他们进场的顺序,每位客人会吃掉所有他想要吃的,并且还没被吃掉的点心.如果客 ...

  9. codeforces 678E Another Sith Tournament 概率dp

    奉上官方题解 然后直接写的记忆化搜索 #include <cstdio> #include <iostream> #include <ctime> #include ...

随机推荐

  1. 【BZOJ 3772】精神污染 主席树+欧拉序

    这道题的内存…………………真·精神污染……….. 这道题的思路很明了,我们就是要找每一个路径包含了多少其他路径那么就是找,有多少路径的左右端点都在这条路径上,对于每一条路径,我们随便选定一个端点作为第 ...

  2. JavaScript几种数组去掉重复值的方法

    数组去重复是一个常见的需求,我们暂时考虑同类型的数组去重复.主要是理清思路和考虑下性能.以下方法,网上基本都有,这里只是简单地总结一下. 思路: 遍历数组,一一比较,比较到相同的就删除后面的 遍历数组 ...

  3. [poj 2104]主席树+静态区间第k大

    题目链接:http://poj.org/problem?id=2104 主席树入门题目,主席树其实就是可持久化权值线段树,rt[i]维护了前i个数中第i大(小)的数出现次数的信息,通过查询两棵树的差即 ...

  4. SICAU-OJ: 第k小

    第k小 题意: 给出一个长度不超过5000的字符串,然后让你找出第K小的字串(1<=K<=5).重复的串大小相等. 题解: 这里我们知道某些串的前缀是肯定小于等于其本身的. 那么长度为5的 ...

  5. 关闭listener监听日志

    有几次碰到过由于监听日志文件大小达到几G,使得在连接时非常慢,像hang住一样,windows下的监听日志达到4G限制,后续连接如果无法写监听日志,就会产生TNS-12537报错,可以通过关闭写监听日 ...

  6. python并发进程

    1 引言 2 创建进程 2.1 通过定义函数的方式创建进程 2.2 通过定义类的方式创建进程 3 Process中常用属性和方法 3.1 守护进程:daemon 3.2 进程终结于存活检查:termi ...

  7. [BZOJ2190&BZOJ2705]欧拉函数应用两例

    欧拉函数phi[n]是表示1~n中与n互质的数个数. 可以用公式phi[n]=n*(1-1/p1)*(1-1/p2)*(1-1/p3)...*(1-1/pk)来表示.(p为n的质因子) 求phi[p] ...

  8. HDU3336(KMP + dp)

    Count the string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  9. shell命令行混合进制计算器smartbc

    需要简单的计算的时候,不想用GUI的计算器,能在shell下直接计算就最好了 查了下,有个东西叫 bc,  具体的使用就不赘述了,可以运行bc,然后进去计算,也可以echo传递过去,大概是像这样 ec ...

  10. python学习笔记 协程

    在学习异步IO模型前,先来了解协程 协程又叫做微线程,Coroutine 子程序或者成为函数,在所有语言中都是层级调用,比如a调用b,b调用c.c执行完毕返回,b执行完毕返回,最后a执行完毕返回 所以 ...