You are developing a software for painting rectangles on the screen. The software supports drawing several rectangles and filling some of them with a color different from the color of the background. You are to implement an important function. The function answer such queries as what is the colored area if a subset of rectangles on the screen are filled.

扫描线裸题

 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<algorithm>
 4 #include<math.h>
 5 using namespace std;
 6 const int maxn=50;
 7 
 8 int st[maxn<<2],cov[maxn<<2],y[maxn];
 9 int Q[maxn];
10 
11 struct rect{
12     int x1,y1,x2,y2;
13 }R[maxn];
14 
15 struct seg{
16     int x,y1,y2,c;
17     bool operator < (const seg a)const{
18         return x<a.x;
19     }
20 }s[maxn];
21 
22 inline void build(){
23     memset(st,0,sizeof(st));
24     memset(cov,0,sizeof(cov));
25 }
26 
27 void pushup(int o,int l,int r){
28     if(cov[o])st[o]=y[r]-y[l];
29     else if(l+1==r)st[o]=0;
30     else st[o]=st[o<<1]+st[o<<1|1];
31 }
32 
33 void update(int o,int l,int r,seg a){
34     if(a.y1<=y[l]&&a.y2>=y[r]){
35         cov[o]+=a.c;
36         pushup(o,l,r);
37         return;
38     }
39     if(l+1==r)return;
40     int m=l+((r-l)>>1);
41     if(a.y1<y[m])update(o<<1,l,m,a);
42     if(a.y2>y[m])update(o<<1|1,m,r,a);
43     pushup(o,l,r);
44 }
45 
46 int main(){
47     int n,m,c=0;
48     while(scanf("%d%d",&n,&m)!=EOF&&n+m){
49         printf("Case %d:\n",++c);
50         for(int i=1;i<=n;++i)scanf("%d%d%d%d",&R[i].x1,&R[i].y1,&R[i].x2,&R[i].y2);
51         build();
52         for(int q=1;q<=m;++q){
53             int k;
54             scanf("%d",&k);
55             int cnt=0;
56             for(int i=1;i<=k;++i){
57                 int num;
58                 scanf("%d",&num);
59                 ++cnt;
60                 s[cnt].x=R[num].x1;s[cnt].y1=R[num].y1;s[cnt].y2=R[num].y2;
61                 s[cnt].c=1;
62                 y[cnt]=R[num].y1;
63                 ++cnt;
64                 s[cnt].x=R[num].x2;s[cnt].y1=R[num].y1;s[cnt].y2=R[num].y2;
65                 s[cnt].c=-1;
66                 y[cnt]=R[num].y2;
67             }
68             sort(y+1,y+cnt+1);
69             int t=unique(y+1,y+cnt+1)-(y+1);
70             sort(s+1,s+cnt+1);
71             int sum=0;
72             for(int j=1;j<cnt;++j){
73                 update(1,1,t,s[j]);
74                 sum+=st[1]*(s[j+1].x-s[j].x);
75             }
76             update(1,1,t,s[cnt]);
77             printf("Query %d: %d\n",q,sum);
78         }
79         printf("\n");
80     }
81     return 0;
82 }
View Code