hdu 3265 矩形剪块面积并

http://acm.hust.edu.cn/vjudge/problem/10769

给n张海报,在每张海报上剪掉一个矩形,求面积并

把剪块的海报分成四个矩形,就是普通的求面积并问题了

#include <iostream>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <stack>
#include <queue>
#include <cctype>
#include <vector>
#include <iterator>
#include <set>
#include <map>
#include <sstream>
using namespace std;

#define mem(a,b) memset(a,b,sizeof(a))
#define pf printf
#define sf scanf
#define spf sprintf
#define pb push_back
#define debug printf("!\n")
#define MAXN 50000+5
#define MAX(a,b) a>b?a:b
#define blank pf("\n")
#define LL long long
#define ALL(x) x.begin(),x.end()
#define INS(x) inserter(x,x.begin())
#define pqueue priority_queue
#define INF 0x3f3f3f3f

#define ls (rt<<1)
#define rs (rt<<1|1)

int n,m;

int hh[MAXN],col[MAXN<<3],len[MAXN<<3];

struct node
{
    int l,r,x,c;
    node(){}
    node(int a,int b,int c,int d):l(a),r(b),x(c),c(d){}
    bool operator < (const node &b) const
    {
        return x<b.x;
    }
}a[MAXN<<3];

void PushUp(int rt,int l,int r)
{
    if(col[rt])
    {
        len[rt] = hh[r+1] - hh[l];
    }
    else if(l==r) len[rt] = 0;
    else
    {
        len[rt] = len[ls]+len[rs];
    }
}

void update(int val,int L,int R,int l,int r,int rt)
{
    if(L<=l && r<=R)
    {
        col[rt] += val;
        PushUp(rt,l,r);
        return;
    }
    int mid = (l+r)>>1;
    if(L <= mid) update(val,L,R,l,mid,ls);
    if(R > mid) update(val,L,R,mid+1,r,rs);
    PushUp(rt,l,r);
}

int main()
{
    int i,j,k,t,kase=1;
    while(~sf("%d",&n) && n)
    {
        int v=0;
        LL sum = 0;
        for(i=0;i<n;i++)
        {
            int x1,y1,x2,y2;
            int X1,Y1,X2,Y2;
            sf("%d%d%d%d%d%d%d%d",&x1,&y1,&x2,&y2,&X1,&Y1,&X2,&Y2);
            if(x1!=X1)
            {
                hh[v] = y1;
                a[v++] = node(y1,y2,x1,1);
                hh[v] = y2;
                a[v++] = node(y1,y2,X1,-1);
            }
            if(x2!=X2)
            {
                hh[v] = y1;
                a[v++] = node(y1,y2,X2,1);
                hh[v] = y2;
                a[v++] = node(y1,y2,x2,-1);
            }
            if(y1!=Y1)
            {
                hh[v] = y1;
                a[v++] = node(y1,Y1,X1,1);
                hh[v] = Y1;
                a[v++] = node(y1,Y1,X2,-1);
            }
            if(y2!=Y2)
            {
                hh[v] = Y2;
                a[v++] = node(Y2,y2,X1,1);
                hh[v] = y2;
                a[v++] = node(Y2,y2,X2,-1);
            }
        }
        sort(hh,hh+v);
        sort(a,a+v);
        int d = 1;
        for(i=1;i<v;i++)
        {
            if(hh[i]!=hh[i-1]) hh[d++] = hh[i];
        }
        mem(len,0);
        mem(col,0);
        for(i=0;i<v-1;i++)
        {
            int l = lower_bound(hh,hh+d,a[i].l)-hh;
            int r = lower_bound(hh,hh+d,a[i].r)-hh-1;
            update(a[i].c,l,r,0,d-1,1);
            sum+=len[1]*((long long)a[i+1].x-a[i].x);
            //pf("%lf %lf\n",sum,len[1]);
        }
        pf("%I64d\n",sum);
    }

    return 0;
}
时间: 2024-08-24 22:27:47

hdu 3265 矩形剪块面积并的相关文章

HDU 3265 Posters(线段树)

HDU 3265 Posters 题目链接 题意:给定一些矩形海报,中间有孔,求贴海报的之后的海报覆盖面积并 思路:海报一张可以切割成4个矩形,然后就是普通的矩形面积并了,利用线段树维护即可 代码: #include <cstdio> #include <cstring> #include <algorithm> using namespace std; typedef long long ll; const int N = 50005; struct Node { i

hdu 3265 Posters(扫描线)

题目链接:hdu 3265 Posters 题目大意:就是给定N个矩形,矩形比较特殊,均被减掉了一部分,问说图形最后的覆盖面积. 解题思路:一开始做的时候以为直接做扫描线就好了,一个做加的一个做减的,后来写完样例都跑不出来,还是对扫描线理解的不够深刻,因为扫描线没有pushdown的操作,因为它肯定对于每段区间有加有减,那么如果碰到一开始就是减的,就没法做了. 正解是将一个图形差分成至多4个小的矩形表示,然后直接扫描线. #include <cstdio> #include <cstri

Torch 两个矩形框重叠面积的计算 (IoU between tow bounding box)

Torch 两个矩形框重叠面积的计算 (IoU between tow bounding box) 1 function DecideOberlap(BBox_x1, BBox_y1, BBox_x2, BBox_y2, BBox_gt_x1, BBox_gt_y1, BBox_gt_x2, BBox_gt_y2) 2 3 x1 = BBox_x1; 4 y1 = BBox_y1; 5 width1 = BBox_x2 - BBox_x1; 6 height1 = BBox_y2 - BBox_

hdu 2892 多边形与园面积相交

area Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 623    Accepted Submission(s): 233 Problem Description 小白最近被空军特招为飞行员,参与一项实战演习.演习的内容是轰炸某个岛屿... 作为一名优秀的飞行员,任务是必须要完成的,当然,凭借小白出色的操作,顺利地将炸弹投到了岛上某

UVA 11983 Weird Advertisement(线段树求矩形并的面积)

UVA 11983 题目大意是说给你N个矩形,让你求被覆盖k次以上的点的总个数(x,y<1e9) 首先这个题有一个转化,吧每个矩形的x2,y2+1这样就转化为了求N个矩形被覆盖k次以上的区域的面积 由于坐标很大,首先考虑的就是将坐标离散化,然后建立线段树tree[][K],表示x的某个区间被覆盖了K次(大于K次算K次)的实际长度,在计算时把矩形转化为一系列横线段(就是说将一个矩形拆开为两条线段,下面的标记为1,上面的标记为-1(这里的标记很有技巧)),然后将这些线段按照y值的从小到达排序(y值相

HDU 1798 两圆相交面积

Tell me the area Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 1755    Accepted Submission(s): 535 Problem Description There are two circles in the plane (shown in the below picture), there is

Leetcode 84 求直方图中矩形的最大面积

题目描述 Leetcode 84 给定 n 个正整数的列表,表示矩形的高度,表示直方图.每一个给出的矩形宽度是 1,找到在直方图里最大的矩形面积. 如图中给出的直方图,宽度是 1,给出的高度是 [2,1,5,6,2,3]. 可以在直方图中找出最大的隐藏面积,答案是 10. Input: [2,1,5,6,2,3] Output: 10 题目分析 解法一: 最后矩形的最大面积,肯定是以某个矩形为最矮高度,向左向右可扩展的最大面积. 举例子来说,假设以 6 为当前直方图中的最矮高度,分别向左和向右扩

HDU 3265 Posters(线段树扫描线&#183;矩形框面积并)

题意  把一些矩形海报挖去一部分小矩形贴在指定位置  问最后海报覆盖的面积 一个矩形框可以分割成4个独立的小矩形  然后就能用扫描线求面积并了 #include <cstdio> #include <algorithm> using namespace std; const int N = 100005, M = N << 2; typedef long long ll; struct SLine { int x, y1, y2, flag; SLine() {}; S

HDU 3265 扫描线(矩形面积并变形)

Posters Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5230    Accepted Submission(s): 1220 Problem Description Ted has a new house with a huge window. In this big summer, Ted decides to decora