283E&EZOJ #89 Cow Tennis Tournament
分析
我们考虑用所有的情况减去不合法的情况
不难想出所有情况为$C_n^3$
于是我们考虑不合法的情况
我们知道对于一个不合法的三元组$(a,b,c)$一定是修改后$a<b,b>c$
于是我们可以离散化后用线段树维护每个点被覆盖了几次
所以每次对于一个点$i$,比它大的点的个数即为在它前面修改次数为偶数的数量加在它后面修改次数为奇数的数量
而产生的不合法情况即为$C_{sum_i}^2$
我们再统计前后两种情况的时候将修改排序然后分别从后往前和从前往后各跑一次即可
每次只要区间不再覆盖点$i$则统计答案
代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<queue>
#include<ctime>
#include<vector>
#include<set>
#include<map>
#include<stack>
using namespace std;
struct node {
long long le,ri;
};
node q[];
long long d[],col[],n,m;
long long a[],sum[];
vector<long long>id;
inline bool cmp1(const node x,const node y){
return x.le==y.le?x.ri<y.ri:x.le<y.le;
}
inline bool cmp2(const node x,const node y){
return x.ri==y.ri?x.le>y.le:x.ri>y.ri;
}
inline void update(long long le,long long ri,long long wh,long long x,long long y){
if(x>y)return;
if(le>=x&&ri<=y){
col[wh]^=;
d[wh]=(ri-le+)-d[wh];
return;
}
long long mid=(le+ri)>>;
if(col[wh]){
col[wh<<]^=;
col[wh<<|]^=;
d[wh<<]=(mid-le+)-d[wh<<];
d[wh<<|]=(ri-mid)-d[wh<<|];
col[wh]=;
}
if(mid>=x)update(le,mid,wh<<,x,y);
if(mid<y)update(mid+,ri,wh<<|,x,y);
d[wh]=d[wh<<]+d[wh<<|];
}
inline long long Q(long long le,long long ri,long long wh,long long x,long long y){
if(x>y)return ;
if(le>=x&&ri<=y)return d[wh];
long long mid=(le+ri)>>,ans=;
if(col[wh]){
col[wh<<]^=;
col[wh<<|]^=;
d[wh<<]=(mid-le+)-d[wh<<];
d[wh<<|]=(ri-mid)-d[wh<<|];
col[wh]=;
}
if(mid>=x)ans+=Q(le,mid,wh<<,x,y);
if(mid<y)ans+=Q(mid+,ri,wh<<|,x,y);
d[wh]=d[wh<<]+d[wh<<|];
return ans;
}
int main(){
long long i,j,k;
scanf("%lld%lld",&n,&m);
for(i=;i<=n;i++)scanf("%lld",&a[i]),id.push_back(a[i]);;
sort(id.begin(),id.end());
id.erase(unique(id.begin(),id.end()),id.end());
for(i=;i<=m;i++)
scanf("%lld%lld",&q[i].le,&q[i].ri);
sort(q+,q+m+,cmp1);
long long Ans=n*(n-)*(n-)/;
j=;
for(i=;i<=m;i++){
for(j;j<=lower_bound(id.begin(),id.end(),q[i].le)-id.begin();j++)
sum[j]+=(n-j)-Q(,n,,j+,n);
update(,n,,lower_bound(id.begin(),id.end(),q[i].le)-id.begin()+,
upper_bound(id.begin(),id.end(),q[i].ri)-id.begin());
}
for(j;j<=n;j++)sum[j]+=(n-j)-Q(,n,,j+,n);
memset(d,,sizeof(d));
memset(col,,sizeof(col));
sort(q+,q+m+,cmp2);
j=n;
for(i=;i<=m;i++){
for(j;j>=upper_bound(id.begin(),id.end(),q[i].ri)-id.begin()+;j--)
sum[j]+=Q(,n,,,j-);
update(,n,,lower_bound(id.begin(),id.end(),q[i].le)-id.begin()+,
upper_bound(id.begin(),id.end(),q[i].ri)-id.begin());
}
for(j;j>;j--)sum[j]+=Q(,n,,,j-);
for(i=;i<=n;i++)Ans-=sum[i]*(sum[i]-)/;
cout<<Ans;
return ;
}
283E&EZOJ #89 Cow Tennis Tournament的更多相关文章
- CodeForces - 283E Cow Tennis Tournament
Discription Farmer John is hosting a tennis tournament with his n cows. Each cow has a skill level s ...
- 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 ...
- CF 628A --- Tennis Tournament --- 水题
CF 628A 题目大意:给定n,b,p,其中n为进行比赛的人数,b为每场进行比赛的每一位运动员需要的水的数量, p为整个赛程提供给每位运动员的毛巾数量, 每次在剩余的n人数中,挑选2^k=m(m & ...
- Educational Codeforces Round 8 A. Tennis Tournament 暴力
A. Tennis Tournament 题目连接: http://www.codeforces.com/contest/628/problem/A Description A tennis tour ...
- Codeforces Educational Codeforces Round 8 A. Tennis Tournament
大致题意: 网球比赛,n个參赛者,每场比赛每位选手b瓶水+裁判1瓶水,所有比赛每一个參赛者p条毛巾 每一轮比赛有2^k个人參加比赛(k为2^k<=n中k的最大值),下一轮晋级人数是本轮每场比赛的 ...
- Codeforces Round #174 (Div. 1 + Div. 2)
A. Cows and Primitive Roots 暴力. B. Cows and Poker Game 模拟. C. Cows and Sequence 线段树维护. D. Cow Progra ...
- Codeforces Round #382 (Div. 2)C. Tennis Championship 动态规划
C. Tennis Championship 题目链接 http://codeforces.com/contest/735/problem/C 题面 Famous Brazil city Rio de ...
- Codeforces Round #382 (Div. 2) C. Tennis Championship 斐波那契
C. Tennis Championship time limit per test 2 seconds memory limit per test 256 megabytes input stand ...
- Tennis Championship
Tennis Championship time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
随机推荐
- hdu--1878--欧拉回路(并查集判断连通,欧拉回路模板题)
题目链接 /* 模板题-------判断欧拉回路 欧拉路径,无向图 1判断是否为连通图, 2判断奇点的个数为0 */ #include <iostream> #include <c ...
- CF 739E Gosha is Hunting
有 $n$ 个 Pokemon,你有 $A$ 个一类精灵球,$B$ 个二类精灵球 分别给出每个 Pokemon 被这两类精灵球捕捉的概率 求抓到 Pokemon 的最优期望个数 $n\leq 2000 ...
- [基本操作] Mobius 反演, Dirichlet 卷积和杜教筛
Dirichlet 卷积是两个定义域在正整数上的函数的如下运算,符号为 $*$ $(f * g)(n) = \sum_{d|n}f(d)g(\frac{n}{d})$ 如果不强调 $n$ 可简写为 $ ...
- HihoCoder1337 动态第k大(treap)
描述 小Ho:小Hi,之前你不是讲过Splay和Treap么,那么还有没有更简单的平衡树呢? 小Hi:但是Splay和Treap不是已经很简单了么? 小Ho:是这样没错啦,但是Splay和Treap和 ...
- mvc那些事
mvc的特点: 1.无控件,有HtmlHelper类,此类提供了各种生成html控件的方法.如果不能满足需要,就自定义扩展吧,比如说分页显示.HtmlHelper类提供了Partial(加载局部视图) ...
- LeetCode Beautiful Arrangement II
原题链接在这里:https://leetcode.com/problems/beautiful-arrangement-ii/description/ 题目: Given two integers n ...
- shell while的用法
1. #!/bin/shint=1while (( "$int < 10" ))doecho "$int"let "int++"don ...
- 深入理解Spring IOC
转载自 http://www.cnblogs.com/xdp-gacl/p/4249939.html 学习过Spring框架的人一定都会听过Spring的IoC(控制反转) .DI(依赖注入)这两个概 ...
- J2EE分布式服务基础之RPC
一.RPC介绍 什么是RPC 远程过程调用(RPC)是一个协议,程序可以使用这个协议请求网络中另一台计算机上某程序的服务而不需知道网络细节. RPC模型 C/S模式 基于传输层协议 (例如 TCP/I ...
- gulp之文件合并以及整合html中的script和link
gulp的文件合并,也就是将多个js或css文件合并为一个的插件是:gulp-concat gulp将html中的多个<script>或<link>合并为一个的插件是:gulp ...