• hdu2461 Rectangles 线段树--扫描线


    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:
    ",++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
    ",q,sum);
    78         }
    79         printf("
    ");
    80     }
    81     return 0;
    82 }
    View Code
  • 相关阅读:
    Tarjan求图的连通性总结
    hdu3849 Tarjan求无向图的割边+map
    两种代码风格解决强连通分量解决加多少条边使整个图连通&多少个点可以到达所有点
    hdu 离线处理+并查集
    hdu 1325判断是不是树
    poj3041 最小点覆盖即最大匹配(匈牙利算法)(模板)
    poj 2186 tarjan求强连通分量(模板题)
    poj2135 最小费用最大流模板
    SPFA队列模板
    链表专项刷题
  • 原文地址:https://www.cnblogs.com/cenariusxz/p/6592405.html
Copyright © 2020-2023  润新知