求下偏导就好了。。。。直接O(1)维护。
#include<iostream> #include<cstring> #include<cstdio> #include<algorithm> #include<cmath> #define eps 1e-7 #define maxn 120050 using namespace std; double a=0,b=0,c=0,d=0,e=0,f=0; double x11,y11,x22,y22; double tab[10]; int n,type,k,cnt=0,tot=0; struct line { double a,b,c; line (double a,double b,double c):a(a),b(b),c(c) {} line () {} }p[maxn]; int main() { scanf("%d",&n); for (int i=1;i<=n;i++) { scanf("%d",&type); if (type==0) { scanf("%lf%lf%lf%lf",&x11,&y11,&x22,&y22); if (x11==x22) p[++cnt]=line(1,0,-x11); else if (y11==y22) p[++cnt]=line(0,1,-y11); else p[++cnt]=line(y22-y11,x11-x22,-x11*y22+x11*y11+x22*y11-x11*y11); double a1=p[cnt].a,b1=p[cnt].b,c1=p[cnt].c,dd=a1*a1+b1*b1; a+=a1*a1/dd;b+=a1*b1/dd;c+=a1*c1/dd; d+=a1*b1/dd;e+=b1*b1/dd;f+=b1*c1/dd; tab[1]+=a1*a1/dd;tab[2]+=b1*b1/dd;tab[3]+=c1*c1/dd; tab[4]+=2.0*a1*b1/dd;tab[5]+=2.0*a1*c1/dd;tab[6]+=2.0*b1*c1/dd; tot++; } else if (type==1) { scanf("%d",&k);tot--; double a1=p[k].a,b1=p[k].b,c1=p[k].c,dd=a1*a1+b1*b1; a-=a1*a1/dd;b-=a1*b1/dd;c-=a1*c1/dd; d-=a1*b1/dd;e-=b1*b1/dd;f-=b1*c1/dd; tab[1]-=a1*a1/dd;tab[2]-=b1*b1/dd;tab[3]-=c1*c1/dd; tab[4]-=2.0*a1*b1/dd;tab[5]-=2.0*a1*c1/dd;tab[6]-=2.0*b1*c1/dd; } else { if (tot==1) {printf("0.00\n");continue;} double x,y; if (a*e==b*d) { x=0; if (fabs(b)>eps) y=-c/b; else if (fabs(e)>eps) y=-(f/e); else y=0; double ans=tab[1]*x*x+tab[2]*y*y+tab[3]+tab[4]*x*y+tab[5]*x+tab[6]*y; y=0; if (fabs(a)>eps) x=-(c/a); else if (fabs(d)>eps) x=-(f/d); else x=0; ans=min(ans,tab[1]*x*x+tab[2]*y*y+tab[3]+tab[4]*x*y+tab[5]*x+tab[6]*y); printf("%.2f\n",ans);continue; } else {y=(a*f-c*d)/(b*d-a*e);x=(b*f-c*e)/(a*e-b*d);} double ans=tab[1]*x*x+tab[2]*y*y+tab[3]+tab[4]*x*y+tab[5]*x+tab[6]*y; if (fabs(ans)<1e-3) printf("0.00\n");else printf("%.2f\n",ans); } } return 0; }