CodeForces - 283E Cow Tennis Tournament
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
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
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
Input3 2Output
1 2 3
1 2
2 3
1
Input
5 3Output
5 9 4 1 7
1 7
2 8
3 9
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