• 洛谷 P2061 [USACO07OPEN]城市的地平线City Horizon


    简化版的矩形面积并,不用线段树,不用离散化,代码意外的简单

    扫描线,这里的基本思路就是把要求的图形竖着切几刀分成许多矩形,求面积并。(切法就是每出现一条与y轴平行的线段都切一刀)

    对于每一个切出来的矩形在处理其右边的线段时计算面积的贡献,

     1 #include<cstdio>
     2 #include<algorithm>
     3 #include<set>
     4 using namespace std;
     5 typedef long long LL;
     6 struct Q
     7 {
     8     int a,b,h;
     9 }q[40100];
    10 struct QQ
    11 {
    12     int pos,fl,h;
    13     bool operator<(const QQ &b)
    14     {
    15         return pos<b.pos;
    16     }
    17 }qq[80100];
    18 int n,len;LL ans;
    19 multiset<int> s;
    20 int main()
    21 {
    22     int i;LL t;
    23     scanf("%d",&n);
    24     for(i=1;i<=n;i++)    scanf("%d%d%d",&q[i].a,&q[i].b,&q[i].h);
    25     for(i=1;i<=n;i++)    qq[++len]=(QQ){q[i].a,1,q[i].h},qq[++len]=(QQ){q[i].b,-1,q[i].h};
    26     sort(qq+1,qq+len+1);
    27     for(i=1;i<=len;i++)
    28     {
    29         if(!s.empty())
    30         {
    31             t=*s.rbegin();ans+=t*(qq[i].pos-qq[i-1].pos);
    32         }
    33         if(qq[i].fl==1)    s.insert(qq[i].h);
    34         else    s.erase(s.find(qq[i].h));
    35     }
    36     printf("%lld",ans);
    37     return 0;
    38 }
  • 相关阅读:
    json_encode([0])
    try catch throw
    如何 lookback
    协程||进程
    客户错了?
    循环 php 变量会影响到传入的参数
    csv变成xls, csv乱码
    虚拟机不能git push
    数组下标大小写
    php 静态方法
  • 原文地址:https://www.cnblogs.com/hehe54321/p/8762448.html
Copyright © 2020-2023  润新知