被这题气死,做了半天才做出来
求折线U在L上面的面积
有两个点要注意:1.求的面积必须是封闭的,即开口的开头不算,开口的结尾不算
2.最后x可以水平延伸到无限远
#include<bits/stdc++.h> using namespace std; #define N 200006 int n,m,tot,x[N],xl[N],yl[N],xu[N],yu[N]; int main(){ cin>>n>>m; x[++tot]=0; xl[0]=xu[0]=0; int px=0,py=-1; for(int i=1;i<=2*n+1;i++){ int v;cin>>v; if(i%2)yl[++py]=v; else xl[++px]=v,x[++tot]=v; } px=0,py=-1; for(int i=1;i<=2*m+1;i++){ int v;cin>>v; if(i%2)yu[++py]=v; else xu[++px]=v,x[++tot]=v; } sort(x+1,x+1+tot); n++;m++; xl[n]=xl[n-1]+100000; yl[n]=yl[n-1]; xu[m]=xu[m-1]+100000; yu[m]=yu[m-1]; n++;m++; x[++tot]=1000000; long long sum=0,time=0,ans=0; int flag=0,f=0; for(int i=1;i<=tot-1;i++){ double mid=(x[i]+x[i+1])/2.0; int posl=lower_bound(xl,xl+n,mid)-xl-1; int hl=yl[posl]; int posu=lower_bound(xu,xu+m,mid)-xu-1; int hu=yu[posu]; if(hu>hl){ if(flag==0)flag=1; sum+=1ll*(x[i+1]-x[i])*(hu-hl); }else { if(f || yu[0]<=yl[0]){ time+=flag; ans+=sum; } f=1; flag=0; sum=0; } } cout<<time<<" "<<ans<<' '; } /* 5 4 2 26 11 30 13 38 16 44 21 48 24 5 23 8 31 10 35 15 41 18 */