bzoj1249: SGU277 HERO 动态凸包

动态维护凸包面积。

  1 //Achen
  2 #include<bits/stdc++.h>
  3 #define For(i,a,b) for(int i=(a);i<=(b);i++)
  4 #define Rep(i,a,b) for(int i=(a);i>=(b);i--)
  5 #define Formylove return 0
  6 const int N=200007;
  7 typedef long long LL;
  8 typedef double db;
  9 using namespace std;
 10 int n;
 11 LL ans;
 12 
 13 template<typename T> void read(T &x) {
 14     char ch=getchar(); T f=1; x=0;
 15     while(ch!='-'&&(ch<'0'||ch>'9')) ch=getchar();
 16     if(ch=='-') f=-1,ch=getchar();
 17     for(;ch>='0'&&ch<='9';ch=getchar()) x=x*10+ch-'0'; x*=f;
 18 }
 19 
 20 struct pt {
 21     LL x,y;
 22     pt(){}
 23     pt(LL x,LL y):x(x),y(y){}
 24     friend bool operator <(const pt&A,const pt&B) {
 25         return A.x<B.x||(A.x==B.x&&A.y<B.y);
 26     }
 27 }p[10];
 28 pt operator -(const pt&A,const pt&B) { return pt(A.x-B.x,A.y-B.y); }
 29 pt operator +(const pt&A,const pt&B) { return pt(A.x+B.x,A.y+B.y); }
 30 LL dot(pt A,pt B) { return A.x*B.x+A.y*B.y; }
 31 LL cross(pt A,pt B) { return A.x*B.y-A.y*B.x; }
 32 
 33 set<pt>s1,s2;
 34 #define IT set<pt>::iterator 
 35 void delr(set<pt>&s,pt P,IT l,IT r,int f) {
 36     while(cross(P-(*l),(*r)-(*l))<=0) {
 37         if(f) ans+=cross((*r)-(*l),P-(*l));
 38         s.erase(r); r=l; 
 39         if(l!=s.begin()) l--;
 40         else break;
 41     }
 42 }
 43 
 44 void dell(set<pt>&s,pt P,IT l,IT r,int f) {
 45     while(cross((*r)-P,(*l)-P)<=0) {
 46         if(f) ans+=cross((*l)-P,(*r)-P);
 47         s.erase(*l); l=r; r++;
 48         if(r==s.end()) break;
 49     }
 50 }
 51 
 52 void ins(set<pt>&s,pt P,int f) {
 53     IT l,r,it;
 54     if(s.size()<2) {
 55         s.insert(P); return ;
 56     }
 57     it=s.lower_bound(P);
 58     if(it==s.end()) {
 59         pt st=*s.begin(); --it;
 60         if(f) ans-=cross((*it)-st,P-st);
 61         r=it; l=r; --l;
 62         if(cross(P-(*l),(*r)-(*l))<0) delr(s,P,l,r,f);
 63     }
 64     else {
 65         if(it==s.begin()) {
 66             pt ed=*s.rbegin();
 67             if(f) ans-=cross(P-ed,(*it)-ed);
 68             l=it; r=l; ++r;
 69             if(cross((*r)-P,(*l)-P)<0) dell(s,P,l,r,f);
 70         }
 71         else {
 72             r=it; l=r; --l;
 73             if((*r).x==P.x&&(*r).y==P.y) return ;
 74             if(cross(P-(*l),(*r)-(*l))>=0) return ;
 75             if(f) ans+=cross((*l)-P,(*r)-P);
 76             r=l; 
 77             if(l!=s.begin()) {
 78                 --l;
 79                 delr(s,P,l,r,f);
 80             }
 81             l=it; r=l; ++r;
 82             if(r!=s.end()) dell(s,P,l,r,f);
 83         }
 84     }
 85     s.insert(P);
 86 }
 87 
 88 int main() {
 89     //freopen("1.in","r",stdin);
 90     //freopen("1.out","w",stdout);
 91     For(i,1,3) read(p[i].x),read(p[i].y);
 92     ans=abs(cross(p[2]-p[1],p[3]-p[1]));
 93     For(i,1,3) {
 94         ins(s1,p[i],0);
 95         ins(s2,pt(-p[i].x,-p[i].y),0);
 96     }
 97     read(n);
 98     For(i,1,n) {
 99         pt P;
100         read(P.x); read(P.y);
101         ins(s1,P,1);
102         ins(s2,pt(-P.x,-P.y),1);
103         printf("%lld\n",ans);
104     }
105     Formylove;
106 }
View Code

 

posted @ 2019-03-11 22:18  啊宸  阅读(296)  评论(0编辑  收藏  举报