zoukankan      html  css  js  c++  java
  • hdu5091(线段树+扫描线)

    题目连接:HDU - 5091

    比较少写这类问题,要多练习。。,

    参考:http://www.cnblogs.com/tsw123/p/4470115.html

      1 #include<cstdio>
      2 #include<cstring>
      3 #include<algorithm>
      4 #define lson l,m,rt<<1
      5 #define rson m+1,r,rt<<1|1
      6 const int maxn=40010;
      7 using namespace std;
      8 
      9 struct LINE
     10 {
     11     int x,y1,y2,v;
     12     LINE(){}
     13     LINE(int x,int y1,int y2,int v):
     14         x(x),y1(y1),y2(y2),v(v){}
     15     bool operator<(const LINE&a)const
     16     {
     17         return x<a.x||x==a.x&&v>a.v;
     18     }
     19 }line[maxn];
     20 
     21 int Y[maxn];//离散
     22 int ct;
     23 int n,w,h;
     24 
     25 int sum[maxn<<2],add[maxn<<2];
     26 
     27 void pushup(int rt)
     28 {
     29     sum[rt]=max(sum[rt<<1],sum[rt<<1|1]);
     30 }
     31 void build(int l,int r,int rt)
     32 {
     33     sum[rt]=add[rt]=0;
     34     if(l==r) return ;//
     35     int m=(l+r)>>1;
     36     build(lson);
     37     build(rson);
     38     pushup(rt);
     39 }
     40 void pushdown(int rt)
     41 {
     42     if(add[rt])
     43     {
     44         add[rt<<1]+=add[rt];
     45         add[rt<<1|1]+=add[rt];
     46         sum[rt<<1]+=add[rt];
     47         sum[rt<<1|1]+=add[rt];
     48         add[rt]=0;
     49     }
     50 }
     51 void update(int L,int R,int v,int l,int r,int rt)
     52 {
     53     if(L<=l&&r<=R)
     54     {
     55         add[rt]+=v;
     56         sum[rt]+=v;
     57         return ;//
     58     }
     59     pushdown(rt);
     60     int m=(l+r)>>1;
     61     if(L<=m) update(L,R,v,lson);
     62     if(R>m) update(L,R,v,rson);
     63     pushup(rt);
     64 }
     65 int query(int L,int R,int l,int r,int rt)
     66 {
     67     if(L<=l&&r<=R)
     68     {
     69         return sum[rt];
     70     }
     71     pushdown(rt);
     72     int m=(l+r)>>1;
     73     int ans=0;
     74     if(L<=m) ans+=query(L,R,lson);
     75     if(R>m) ans+=query(L,R,rson);
     76     return ans;
     77 }
     78 int main()
     79 {
     80     while(scanf("%d",&n)&&n!=-1)
     81     {
     82         ct=0;
     83         scanf("%d%d",&w,&h);
     84         for(int i=0;i<n;i++)
     85         {
     86             int x,y;
     87             scanf("%d%d",&x,&y);
     88             x+=20001;//
     89             y+=20001;//
     90             Y[ct]=y;
     91             line[ct++]=LINE(x,y,y+h,1);
     92             Y[ct]=y+h;
     93             line[ct++]=LINE(x+w,y,y+h,-1);
     94         }
     95         sort(Y,Y+ct);
     96         sort(line,line+ct);
     97         int num=unique(Y,Y+ct)-Y;//
     98         build(0,num-1,1);
     99         int ans=0;
    100         for(int i=0;i<ct;i++)
    101         {
    102             int l=lower_bound(Y,Y+num,line[i].y1)-Y;//
    103             int r=lower_bound(Y,Y+num,line[i].y2)-Y;//
    104             update(l,r,line[i].v,0,num-1,1);
    105             ans=max(ans,query(0,num-1,0,num-1,1));//此处可以省去query,直接写ans=max(ans,sum[1]);也行
    106         }
    107         printf("%d
    ",ans);
    108     }
    109 }
  • 相关阅读:
    FFmpeg在JAVA中的使用以及Process.waitFor()引发的阻塞问题
    分析自己遇到的Excel导出报NullpointException问题
    C# 获取文件路径
    List的方法和属性 方法或属性 作用
    asp.net发布到IIS中出现错误:处理程序“PageHandlerFactory-Integrated”在其模块列表中有一个错误模块“ManagedPipelineHandler”
    .NET 垃圾回收与内存泄漏
    .NET多线程总结和实例介绍
    C#中释放数据库连接资源
    PHP
    sqlconnection dispose()与close()的区别
  • 原文地址:https://www.cnblogs.com/yijiull/p/6792640.html
Copyright © 2011-2022 走看看