【HDU】1828-Picture(线段树扫描线)

矩阵交并周长的模板题

这题不需要离散化,需要注意的时候负坐标转化成正坐标

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define lson (pos<<1)
#define rson (pos<<1|1)
typedef long long LL;
const int maxn = 30005;
const int ADD  = 10001;
struct Seg{
    int l,r,c,h;
    Seg(int l = 0,int r = 0,int h = 0,int c = 0):l(l),r(r),h(h),c(c){};
    friend bool operator < (Seg p,Seg q){
        return p.h < q.h;
    }
}seg[maxn];
struct Node{
    int l,r,cover,sum1,sum2;
    bool left,right;
    int mid(){
        return (l + r) >> 1;
    }
    int len(){
        return  r - l + 1;
    }
}node[maxn << 2];
int cnt;
void build(int l,int r,int pos){
    node[pos].l = l; node[pos].r = r;
    node[pos].sum1 = node[pos].sum2 = 0;
    node[pos].left = node[pos].right = false;
    node[pos].cover = 0;
    if(l == r) return;
    int mid = (l + r) >> 1;
    build(l,mid,lson);
    build(mid + 1,r,rson);
}
void pushup(int pos){
    if(node[pos].cover){
        node[pos].sum1 = 2;
        node[pos].sum2 = node[pos].len();
        node[pos].left = node[pos].right = true;
    }
    else if(node[pos].len() == 1){
        node[pos].sum1 = node[pos].sum2 = node[pos].left = node[pos].right = 0;
    }
    else{
        node[pos].sum1  = node[lson].sum1 + node[rson].sum1;
        if(node[lson].right && node[rson].left) node[pos].sum1 -= 2;
        node[pos].sum2  = node[lson].sum2 + node[rson].sum2;
        node[pos].left  = node[lson].left;
        node[pos].right = node[rson].right;
    }
}
void update(int l,int r,int pos,int d){
    if(l <= node[pos].l && node[pos].r <= r){
        node[pos].cover += d;
        pushup(pos);
        return;
    }
    int mid = node[pos].mid();
    if(l <= mid)
        update(l,r,lson,d);
    if(r  > mid)
        update(l,r,rson,d);
    pushup(pos);
    return;
}
int main(){
    int n;
    while(scanf("%d",&n) != EOF){
        build(1,30000,1);
        cnt = 0;
        for(int i = 0; i < n; i++){
            int a,b,c,d;
            scanf("%d%d%d%d",&a,&b,&c,&d);
            a += ADD; c += ADD;
            seg[cnt++] = Seg(a,c,d,1);
            seg[cnt++] = Seg(a,c,b,-1);
        }
        sort(seg,seg + cnt);
        LL ans = 0,last = 0;
        for(int i = 0; i < cnt; i++){
            int l = seg[i].l,r = seg[i].r,c = seg[i].c;
            update(l,r - 1,1,c);
            if(i < cnt - 1)
                ans += node[1].sum1 * (seg[i + 1].h - seg[i].h);
            ans += abs(node[1].sum2 - last);
            last = node[1].sum2;
        }
        printf("%I64d\n",ans);
    }
    return 0;
}
时间: 2024-08-09 10:28:50

【HDU】1828-Picture(线段树扫描线)的相关文章

HDU 1828 Picture 线段树+扫描线

题意:给你一些矩形的左上角点的坐标和右下角点的坐标,求周长并 最显而易见的思路就是对于x轴和y轴做两次扫描线,对于负数的坐标进行离散化.每次增加的值是线段变化量的绝对值.具体写法和求面积并的差不多. #include <cstdio> #include <algorithm> #include <cstring> #include <vector> using namespace std; #define lson rt << 1 , l , m

hdu 1828 Picture(线段树&amp;扫描线&amp;周长并)

Picture Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2578    Accepted Submission(s): 1363 Problem Description A number of rectangular posters, photographs and other pictures of the same shap

POJ 1177/HDU 1828 picture 线段树+离散化+扫描线 轮廓周长计算

求n个图矩形放下来,有的重合有些重合一部分有些没重合,求最后总的不规则图型的轮廓长度. 我的做法是对x进行一遍扫描线,再对y做一遍同样的扫描线,相加即可.因为最后的轮廓必定是由不重合的线段长度组成的,这样理论上是对的 要注意处理高度相同的线段,把底边优先处理(在代码里就是f标记为1的线段),因为若是一个矩形的底边和另一个矩形的上边重合,则这个轮廓肯定不能算 不过POJ和HDU的数据好像都比较弱,我没进行上面的细节处理也AC了,不过一个很简单的数据就会不对,所以还是要处理一下才是真正正确的代码 我

hdu 1828 Picture(线段树轮廓线)

Picture Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3075    Accepted Submission(s): 1616 Problem Description A number of rectangular posters, photographs and other pictures of the same shape

hdu 1542 Atlantis(线段树&amp;扫描线&amp;面积并)

Atlantis Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6386    Accepted Submission(s): 2814 Problem Description There are several ancient Greek texts that contain descriptions of the fabled i

HDU 1542 Atlantis(线段树扫描线)

http://acm.hdu.edu.cn/showproblem.php?pid=1542 Atlantis Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6788    Accepted Submission(s): 2970 Problem Description There are several ancient Greek

hdu 1542 Atlantis (线段树+扫描线)

Atlantis Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 18559    Accepted Submission(s): 7523 Problem Description There are several ancient Greek texts that contain descriptions of the fabled i

HDU 1542 Atlantis (线段树 + 扫描线 + 离散化)

Atlantis Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 8998    Accepted Submission(s): 3856 Problem Description There are several ancient Greek texts that contain descriptions of the fabled i

hdu 3642(线段树+扫描线)

三维扫描线,枚举z寻找相交区间的立方体,然后直接扫描线求xy平面的相交三次及以上面积,乘以z区间求和就可以了 #include <iostream> #include <cstring> #include <algorithm> #include <cstdio> using namespace std; const int maxn=1e6+100; const int maxm=2000+500; int col[maxn<<2]; int

HDU 3265 Posters (线段树+扫描线)(面积并)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3265 给你n个中间被挖空了一个矩形的中空矩形,让你求他们的面积并. 其实一个中空矩形可以分成4个小的矩形,然后就是面积并,特别注意的是x1 == x3 || x2 == x4的时候,要特判一下,否则会RE. 1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 #include <algori