codefroces 297E Mystic Carvings
problem:
一個圓上依次有1~2*n的數字。每個數字都有且只有另一個數字與他相連。選出三條線,使得每條線的兩端之間隔的最少點(只包括被選擇的6個點)的個數相等。
輸入輸出格式
輸入格式:
The first line contains integer n(3<=n<=10^5) — the number of lines.
Each of the following n n n lines contains two integers ai?,bi? (1<=ai,bi<=2n), which means that there is a line carved on the ice connecting the ai –th and bi? –th endpoint.
It's guaranteed that each endpoints touches exactly one line.
輸出格式:
Print the number of ways to build the caves.
Please, do not write the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.
輸入輸出樣例
輸入樣例#1:
4
5 4
1 2
6 7
8 3
輸出樣例#1:
2
輸入樣例#2:
8
1 7
2 4
3 9
5 11
6 8
10 16
13 15
14 12
輸出樣例#2:
6
說明
The second sample corresponds to the figure in the problem statement.
六個點三條邊有以下五種可能:
明顯只有第1個和第4個可以。但是這2個情況不好求
可以求出總方案再減去不合法方案
?
此題有兩種寫fa♂:
第一種是樹狀數組
假設當前直線i a->b(a<b)
在直線左邊的直線數為x,右邊的直線數為y,與i相♂蕉的直線數為z
顯然有x+y+z+1=n
第3種情況就是x*y
第2,5種是(x+y)*z
但是因為每個點都有邊連出,且圓上無蕉♂點
所以z=b-a-1-2*x
第二種是主席樹,了解一下
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<cmath> 6 using namespace std; 7 typedef long long lol; 8 lol c[200001],n,m; 9 int a[200001],b[200001],p[200001]; 10 lol ans1,ans2,ans; 11 void add(int x,int d) 12 { 13 while (x<=m) 14 { 15 c[x]+=d; 16 x+=(x&(-x)); 17 } 18 } 19 int query(int x) 20 { 21 int s=0; 22 while (x) 23 { 24 s+=c[x]; 25 x-=(x&(-x)); 26 } 27 return s; 28 } 29 int main() 30 {int i; 31 cin>>n; 32 m=2*n; 33 for (i=1;i<=n;i++) 34 { 35 scanf("%d%d",&a[i],&b[i]); 36 p[a[i]]=b[i]; 37 p[b[i]]=a[i]; 38 } 39 for (i=1;i<=m;i++) 40 { 41 if (i<p[i]) continue; 42 int x=query(i)-query(p[i]-1); 43 int z=i-p[i]-1-x*2; 44 add(p[i],1); 45 int y=n-1-x-z; 46 ans1+=x*y; 47 ans2+=(x+y)*z; 48 } 49 ans=n*(n-1)*(n-2)/6; 50 cout<<ans-ans1-ans2/2; 51 }?
轉載于:https://www.cnblogs.com/Y-E-T-I/p/8619419.html
總結
以上是生活随笔為你收集整理的codefroces 297E Mystic Carvings的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: JS中原型与原型链
- 下一篇: ptmalloc内存分配和回收详解(文字