HNU12884_Area Coverage(扫描线/线段树+离散化)

解题报告

题目传送门

题意:

又是求面积并

思路:

又是求面积并,还被坑了,题目明明描述的是int坐标,用了double才过。。。

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
struct Seg {
    double lx,rx,h;
    int v;
    friend bool operator <(Seg a,Seg b) {
        return a.h<b.h;
    }
} seg[2100];
double _hash[4100],sum[201000];
int lz[201000];
void push_up(int rt,int l,int r) {
    if(lz[rt])sum[rt]=_hash[r+1]-_hash[l];
    else if(l==r)sum[rt]=0;
    else sum[rt]=sum[rt<<1]+sum[rt<<1|1];
}
void update(int rt,int l,int r,int ql,int qr,int v) {
    if(ql>r||qr<l)return ;
    if(ql<=l&&r<=qr) {
        lz[rt]+=v;
        push_up(rt,l,r);
        return ;
    }
    int mid=(l+r)>>1;
    update(rt<<1,l,mid,ql,qr,v);
    update(rt<<1|1,mid+1,r,ql,qr,v);
    push_up(rt,l,r);
}
int main() {
    int t,n,i,j;
    double x1,x2,y1,y2;
    scanf("%d",&t);
    while(t--) {
        memset(sum,0,sizeof(sum));
        memset(_hash,0,sizeof(_hash));
        memset(lz,0,sizeof(lz));
        scanf("%d",&n);
        for(i=0; i<n; i++) {
            scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
            _hash[i]=x1,_hash[i+n]=x2;
            seg[i].lx=x1,seg[i].rx=x2,seg[i].v=1,seg[i].h=y1;
            seg[i+n].lx=x1,seg[i+n].rx=x2,seg[i+n].v=-1,seg[i+n].h=y2;
        }
        sort(_hash,_hash+n*2);
        sort(seg,seg+n*2);
        int m=unique(_hash,_hash+n*2)-_hash;
        double ans=0;
        for(i=0; i<n*2-1; i++) {
            int ql=lower_bound(_hash,_hash+m,seg[i].lx)-_hash;
            int qr=lower_bound(_hash,_hash+m,seg[i].rx)-_hash-1;
            update(1,0,m-1,ql,qr,seg[i].v);
            ans+=sum[1]*(seg[i+1].h-seg[i].h);
        }
        printf("%.0lf\n",ans);
    }
    return 0;
}
Area Coverage
Time Limit: 10000ms, Special Time Limit:2500ms, Memory Limit:65536KB
Total submit users: 22, Accepted users: 16
Problem 12884 : No special judgement
Problem description

In this day and age, a lot of the spying on other countries is done with the use of satellites and drones equipped with cameras. All these photographs of various sizes and from various sources can be combined to give a picture of the country as a whole.

Given the photographs (that is to say, the rectangular area covered by each, since the contents of the photographs themselves are of course top-secret!), can you work out what the total area is of all that is photographed? Note that certain areas can appear
on multiple photographs and should be counted only once.

Input

On the first line one positive number: the number of test cases, at most 100. After that per test case:

&#15; one line with an integer n (1<=n<=1000): the number of photographs.

&#15; n lines with four space-separated integers x1, y1, x2 and y2 (0<=x1; y1; x2; y2<=1 000 000, x1 < x2 and y1 < y2): the coordinates of the southwest and northeast corner, respectively, of each photograph. The photographs are all rectangular in shape with their
other corners at (x1; y2) and (x2; y1).

The coordinates correspond to a flat two-dimensional space (i.e. we assume the Earth to be flat).

Output

Per test case:

&#15; one line with an integer: the total area of all that appears on the photographs.

Sample Input
2
3
0 6 20 16
14 0 24 10
50 50 60 60
2
0 0 20 10
10 4 14 8
Sample Output
376
200
Problem Source
BAPC preliminary 2013

HNU12884_Area Coverage(扫描线/线段树+离散化)

时间: 2024-08-27 12:22:42

HNU12884_Area Coverage(扫描线/线段树+离散化)的相关文章

ZOJ1659_Mobile Phone Coverage(扫描线/线段树+离散)

解题报告 题目传送门 题意: 求矩形面积并 思路: 扫描线+线段树.要离散化,坐标是浮点型的. 对于线段树(区间)与点坐标对应起来可以这样 区间[1,4]对应的线段树. #include <algorithm> #include <iostream> #include <cstring> #include <cstdio> using namespace std; struct Seg { int v; double lx,rx,h; friend bool

HDU 1255 覆盖的面积 (扫描线 线段树 离散化)

题目链接 题意:中文题意. 分析:纯手敲,与上一道题目很相似,但是刚开始我以为只是把cnt>=0改成cnt>=2就行了,. 但是后来发现当当前加入的线段的范围之前 还有线段的时候就不行了,因为虽然现在都不等于 2,但是之前的那个线段加上现在的已经覆盖2次了. 1 #include <iostream> 2 #include <cstdio> 3 #include <vector> 4 #include <cstring> 5 #include &

hdu1828 Picture(线段树+离散化+扫描线)两种方法

C - Picture Time Limit:2000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Submit Status Description A number of rectangular posters, photographs and other pictures of the same shape are pasted on a wall. Their sides are all vertical or

poj 1151 Atlantis (离散化 + 扫描线 + 线段树)

题目链接题意:给定n个矩形,求面积并,分别给矩形左上角的坐标和右上角的坐标. 分析: 1 #include <iostream> 2 #include <cstdio> 3 #include <vector> 4 #include <cstring> 5 #include <cstdlib> 6 #include <algorithm> 7 #define LL __int64 8 #define lson l, mid, 2*rt

POJ 2482 Stars in Your Window 线段树+离散化+扫描线

题面据说很美- 每个星星可以根据在窗口的左下角和右上角两个位置建立两条扫描线,之后就是简单的区间增减和求最大值操作了. 注意要处理在边界上的星星是不算的情况,其实只要把左右边界分别增减一个eps即可. #include <cstdio> #include <cstring> #include <iostream> #include <map> #include <set> #include <vector> #include <

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

题意:给出一些矩形的最上角坐标和右下角坐标,求这些矩形的面积并. NotOnlySuccess 线段树专辑中扫描线模板题,弱智的我对着大大的代码看了一下午才搞懂. 具体见思路见注释=.= #include <cstdio> #include <cstring> #include <algorithm> #include <vector> #define lson rt<<1,l,mid #define rson rt<<1|1,mid

poj3277--City Horizon(线段树+离散化+扫描线)

City Horizon Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 16206   Accepted: 4414 Description Farmer John has taken his cows on a trip to the city! As the sun sets, the cows gaze at the city horizon and observe the beautiful silhouette

【POJ 2482】 Stars in Your Window(线段树+离散化+扫描线)

[POJ 2482] Stars in Your Window(线段树+离散化+扫描线) Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11294   Accepted: 3091 Description Fleeting time does not blur my memory of you. Can it really be 4 years since I first saw you? I still remembe

HDU 3642 线段树+离散化+扫描线

题意:给你N个长方体的左下角和右上角坐标,问你空间中有多少体积是被大于两个不同的立方体覆盖的.x,y~10^6 z~500 考虑到给的z比较小,所以可以直接枚举z,然后跑二维的扫描线就好. 关于处理被不同的线段覆盖三次的问题,可以维护四个信息,cnt,once,twice,more,然后相互推出结果就好. #include <cstdio> #include <cstring> #include <vector> #include <algorithm> #