Adding New Machine ZOJ

https://vjudge.net/problem/ZOJ-3540

错误记录:

扫描线没有考虑到同一行的要删除在前,加入在后;由于用了特殊的方式所以想当然以为不需要考虑这个问题

  1 #include<cstdio>
  2 #include<algorithm>
  3 #include<cstring>
  4 #include<vector>
  5 #include<set>
  6 using namespace std;
  7 #define fi first
  8 #define se second
  9 #define mp make_pair
 10 #define pb push_back
 11 typedef long long ll;
 12 typedef unsigned long long ull;
 13 typedef pair<int,int> pi;
 14 struct Q
 15 {
 16     ll x1,y1,x2,y2;
 17 }q[100100];
 18 ll w,h,n,m;
 19 struct Q2
 20 {
 21     ll x1,x2,y,type;
 22 }qt[200100];
 23 ll t1[200100];
 24 bool operator<(const Q2 &a,const Q2 &b)
 25 {
 26     return a.y<b.y||(a.y==b.y&&a.type<b.type);
 27 }
 28 ll qn,ta,ans;
 29 set<pi> s;
 30 set<pi>::iterator it;
 31 int main()
 32 {
 33     ll i,r,tx,ty;
 34     while(scanf("%lld%lld%lld%lld",&w,&h,&n,&m)==4)
 35     {
 36         ans=0;
 37 
 38         for(i=1;i<=n;i++)    scanf("%lld%lld%lld%lld",&q[i].x1,&q[i].y1,&q[i].x2,&q[i].y2);
 39 
 40         qn=0;
 41         s.clear();s.insert(mp(-1000,0));s.insert(mp(w+1,w+1000));
 42         for(i=1;i<=n;i++)
 43         {
 44             ++qn;qt[qn].x1=q[i].x1;qt[qn].x2=q[i].x2;qt[qn].y=q[i].y1;qt[qn].type=1;
 45             ++qn;qt[qn].x1=q[i].x1;qt[qn].x2=q[i].x2;qt[qn].y=q[i].y2+1;qt[qn].type=0;
 46         }
 47         sort(qt+1,qt+qn+1);
 48         for(i=1;i<=qn;i++)    t1[i]=qt[i].y;
 49         t1[0]=qn;
 50         t1[++t1[0]]=1;
 51         t1[++t1[0]]=h+1;
 52         sort(t1+1,t1+t1[0]+1);t1[0]=unique(t1+1,t1+t1[0]+1)-t1-1;
 53         r=0;ta=max(w-m+1,0ll);
 54         for(i=1;i<t1[0];i++)
 55         {
 56             while(r+1<=qn&&qt[r+1].y<=t1[i])
 57             {
 58                 r++;
 59                 if(qt[r].type==1)
 60                 {
 61                     it=s.lower_bound(mp(qt[r].x1,qt[r].x2));
 62                     tx=it->first;ty=(--it)->second;
 63                     ta-=max(tx-ty-m,0ll);
 64                     ta+=max(qt[r].x1-ty-m,0ll);
 65                     ta+=max(tx-qt[r].x2-m,0ll);
 66                     s.insert(mp(qt[r].x1,qt[r].x2));
 67                 }
 68                 else
 69                 {
 70                     it=s.lower_bound(mp(qt[r].x1,qt[r].x2));
 71                     tx=(++it)->first;--it;ty=(--it)->second;
 72                     ta-=max(qt[r].x1-ty-m,0ll);
 73                     ta-=max(tx-qt[r].x2-m,0ll);
 74                     ta+=max(tx-ty-m,0ll);
 75                     s.erase(mp(qt[r].x1,qt[r].x2));
 76                 }
 77             }
 78             ans+=ta*(t1[i+1]-t1[i]);
 79         }
 80 
 81         qn=0;
 82         s.clear();s.insert(mp(-1000,0));s.insert(mp(h+1,h+1000));
 83         for(i=1;i<=n;i++)
 84         {
 85             ++qn;qt[qn].x1=q[i].y1;qt[qn].x2=q[i].y2;qt[qn].y=q[i].x1;qt[qn].type=1;
 86             ++qn;qt[qn].x1=q[i].y1;qt[qn].x2=q[i].y2;qt[qn].y=q[i].x2+1;qt[qn].type=0;
 87         }
 88         sort(qt+1,qt+qn+1);
 89         for(i=1;i<=qn;i++)    t1[i]=qt[i].y;
 90         t1[0]=qn;
 91         t1[++t1[0]]=1;
 92         t1[++t1[0]]=w+1;
 93         sort(t1+1,t1+t1[0]+1);t1[0]=unique(t1+1,t1+t1[0]+1)-t1-1;
 94         r=0;ta=max(h-m+1,0ll);
 95         for(i=1;i<t1[0];i++)
 96         {
 97             while(r+1<=qn&&qt[r+1].y<=t1[i])
 98             {
 99                 r++;
100                 if(qt[r].type==1)
101                 {
102                     it=s.lower_bound(mp(qt[r].x1,qt[r].x2));
103                     tx=it->first;ty=(--it)->second;
104                     ta-=max(tx-ty-m,0ll);
105                     ta+=max(qt[r].x1-ty-m,0ll);
106                     ta+=max(tx-qt[r].x2-m,0ll);
107                     s.insert(mp(qt[r].x1,qt[r].x2));
108                 }
109                 else
110                 {
111                     it=s.lower_bound(mp(qt[r].x1,qt[r].x2));
112                     tx=(++it)->first;--it;ty=(--it)->second;
113                     ta-=max(qt[r].x1-ty-m,0ll);
114                     ta-=max(tx-qt[r].x2-m,0ll);
115                     ta+=max(tx-ty-m,0ll);
116                     s.erase(mp(qt[r].x1,qt[r].x2));
117                 }
118             }
119             ans+=ta*(t1[i+1]-t1[i]);
120         }
121 
122         if(m==1)    ans/=2;
123         printf("%lld
",ans);
124     }
125     return 0;
126 }
原文地址:https://www.cnblogs.com/hehe54321/p/9274211.html