Gym 100269 L Lonely Mountain
分段函数+积分
/*
{
######################
# Author #
# Gary #
# 2021 #
######################
*/
#include<bits/stdc++.h>
#define rb(a,b,c) for(int a=b;a<=c;++a)
#define rl(a,b,c) for(int a=b;a>=c;--a)
#define LL long long
#define IT iterator
#define PB push_back
#define II(a,b) make_pair(a,b)
#define FIR first
#define SEC second
#define FREO freopen("check.out","w",stdout)
#define rep(a,b) for(int a=0;a<b;++a)
#define SRAND mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
#define random(a) rng()%a
#define ALL(a) a.begin(),a.end()
#define POB pop_back
#define ff fflush(stdout)
#define fastio ios::sync_with_stdio(false)
#define check_min(a,b) a=min(a,b)
#define check_max(a,b) a=max(a,b)
using namespace std;
//inline int read(){
// int x=0;
// char ch=getchar();
// while(ch<'0'||ch>'9'){
// ch=getchar();
// }
// while(ch>='0'&&ch<='9'){
// x=(x<<1)+(x<<3)+(ch^48);
// ch=getchar();
// }
// return x;
//}
const int INF=0x3f3f3f3f;
typedef pair<int,int> mp;
typedef long double lb;
/*}
*/
lb get(lb l){
l=l*l*l;
l*=1.0;
l/=3.0;
return l;
}
lb integral2(lb a,lb l,lb r){
return (get(r)-get(l))*a;
}
lb integral1(lb a,lb l,lb r){
lb l_,r_;
l_=l*a;
r_=r*a;
return (r_+l_)*(r-l)/2.0;
}
lb integral0(lb a,lb l,lb r){
return a*(r-l);
}
struct line{
lb k,b;
inline bool operator < (line oth)const{
return 0;
}
inline bool operator > (line oth)const{
return 0;
}
line (lb k_,lb b_){
k=k_,b=b_;
}
line operator + (line oth){
return line(k+oth.k,b+oth.b);
}
line operator - (line oth){
return line(k-oth.k,b-oth.b);
}
line (pair<lb,lb> A,pair<lb,lb> B,bool rev=0){
swap(A.FIR,A.SEC);
swap(B.FIR,B.SEC);
k=(A.SEC-B.SEC)/(A.FIR-B.FIR);
b=A.SEC-k*A.FIR;
// if(rev) k=-k;
}
};
lb get(line A,line B,lb l,lb r){
// printf("A:%.10Lf %.10Lf\n",A.k,A.b);
// printf("B:%.10Lf %.10Lf\n",B.k,B.b);
// printf("l:r %.10Lf %.10Lf\n",l,r);
lb a,b,c;
a=A.k*B.k;
b=A.k*B.b+B.k*A.b;
c=A.b*B.b;
// printf("a,b,c[ret] : %.10Lf %.10Lf %.10Lf %.10Lf \n",a,b,c,integral0(c,l,r)+integral1(b,l,r)+integral2(a,l,r));
return integral0(c,l,r)+integral1(b,l,r)+integral2(a,l,r);
}
const int MAXN=1e5+23;
pair<lb,lb> a[MAXN],b[MAXN];
int n,m;
int main(){
scanf("%d",&n);
lb maxa,maxb;
maxa=maxb=0;
rb(i,1,n) scanf("%Lf%Lf",&a[i].FIR,&a[i].second),check_max(maxa,a[i].second);
scanf("%d",&m);
rb(i,1,m) scanf("%Lf%Lf",&b[i].FIR,&b[i].second),check_max(maxb,b[i].second);
if(maxa!=maxb){
printf("Invalid plan\n");
return 0;
}
priority_queue<pair<lb,pair<int,line> > ,vector<pair<lb,pair<int,line> > > ,greater<pair<lb,pair<int,line> > > > heap;
lb rest=0;
rb(i,1,n){
if(i!=1&&a[i-1].second>a[i].second){
// cout<<"$"<<i<<endl;
// cout<<line(a[i],a[i-1]).k<<endl;
heap.push(II(a[i].second,II(1,line(a[i],a[i-1],1))));
heap.push(II(a[i-1].SEC,II(-1,line(a[i],a[i-1],1))));
}
if(i!=n&&a[i+1].second>a[i].second){
heap.push(II(a[i].second,II(-1,line(a[i],a[i+1],1))));
heap.push(II(a[i+1].SEC,II(1,line(a[i],a[i+1],1))));
}
}
rb(i,1,m){
if(i!=1&&b[i-1].second>b[i].second){
heap.push(II(b[i].second,II(2,line(b[i],b[i-1],1))));
heap.push(II(b[i-1].SEC,II(-2,line(b[i],b[i-1],1))));
}
if(i!=m&&b[i+1].second>b[i].second){
heap.push(II(b[i].second,II(-2,line(b[i],b[i+1],1))));
heap.push(II(b[i+1].SEC,II(2,line(b[i],b[i+1],1))));
}
}
line basea(0,0),baseb(0,0);
while(!heap.empty()){
lb tim=heap.top().FIR;
while(!heap.empty()&&heap.top().FIR==tim){
pair<lb,pair<int,line> > now=heap.top();
heap.pop();
if(now.second.FIR==1){
basea=basea+now.second.SEC;
}
if(now.second.FIR==-1){
basea=basea-now.second.SEC;
}
if(now.second.FIR==2){
baseb=baseb+now.second.SEC;
}
if(now.second.FIR==-2){
baseb=baseb-now.second.SEC;
}
}
if(heap.empty()) break;
lb r=heap.top().FIR;
rest+=get(basea,baseb,tim,r);
}
printf("%.18Lf\n",rest);
return 0;
}