扫描线算法 求矩形面积交并和周长并

矩形面积并 HDU - 1542

#include <set>
#include <map>
#include <list>
#include <cmath>
#include <queue>
#include <vector>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define lowbit(x) x&(-x)
#define debug cout<<"****************"<<endl
#define go int T;cin>>T;for(int kase=0;kase<T;kase++)
#define FRER() freopen("/Users/seven7777777/Desktop/Code/TESTXCODE/TESTXCODE/i.txt","r",stdin)
#define FREW() freopen("/Users/seven7777777/Desktop/Code/TESTXCODE/TESTXCODE/out.txt","w",stdout)
#define eps 1e-8
#define mod 1000000007
#define yes cout << "T" << endl
#define no cout << "F" <<endl
using namespace std;
typedef pair<int,int> pii;
typedef long long ll;
const int maxn = 10000+7;
double y[maxn];
struct Line {
    double l,r,x;
    int flag;
    bool operator < (const Line& rhs) const {
        return x < rhs.x;
    }
}lines[maxn];
struct Node {
    double l,r;
    int cover;
    double len;
}tree[maxn];
void build(int rt,int l,int r){
    tree[rt].l = y[l];
    tree[rt].r = y[r];
    tree[rt].cover = 0;
    tree[rt].len = 0;
    if(l==r-1) return;
    int mid = (l+r) >> 1;
    build(rt<<1, l, mid);
    build(rt<<1|1,mid,r);
}
void pushup(int rt,int l,int r){
    if(tree[rt].cover > 0){
        tree[rt].len = tree[rt].r - tree[rt].l;
    }else if(l==r-1){
        tree[rt].len = 0;
    }else{
        tree[rt].len = tree[rt<<1].len + tree[rt<<1|1].len;
    }
}
void update(int rt,int l,int r,Line e){
    if(e.l>=y[r]||e.r<=y[l]) return;
    if(l==r-1) tree[rt].cover += e.flag;
    else{
        int mid = (l+r) >> 1;
        update(rt<<1, l, mid, e);
        update(rt<<1|1, mid, r, e);
    }
    pushup(rt,l,r);
}
int main()
{
    //FRER();
    //FREW();
    int n,k=0;
    while(~scanf("%d",&n)&&n){
        int cnt = 0;
        for(int i=1;i<=n;i++){
            double x1,y1,x2,y2;
            scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
            lines[cnt].x = x1;
            lines[cnt].flag = 1;
            lines[cnt].l = y1;
            lines[cnt].r = y2;
            y[cnt++] = y1;
            lines[cnt].x = x2;
            lines[cnt].flag = -1;
            lines[cnt].l = y1;
            lines[cnt].r = y2;
            y[cnt++] = y2;
        }
        sort(lines,lines+cnt);
        sort(y, y+cnt);
        build(1,0,cnt-1);
        double ans = 0;
        for(int i=0;i<cnt;i++){
            update(1, 0, cnt-1, lines[i]);
            ans += tree[1].len * (lines[i+1].x-lines[i].x);
        }
        printf("Test case #%d\nTotal explored area: %.2f\n\n",++k,ans);
    }
}

矩形面积交 HDU - 1255

#include <set>
#include <map>
#include <list>
#include <cmath>
#include <queue>
#include <vector>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define lowbit(x) x&(-x)
#define debug cout<<"****************"<<endl
#define go int T;cin>>T;for(int kase=0;kase<T;kase++)
#define FRER() freopen("/Users/seven7777777/Desktop/Code/TESTXCODE/TESTXCODE/i.txt","r",stdin)
#define FREW() freopen("/Users/seven7777777/Desktop/Code/TESTXCODE/TESTXCODE/out.txt","w",stdout)
#define eps 1e-8
#define mod 1000000007
#define yes cout << "T" << endl
#define no cout << "F" <<endl
using namespace std;
typedef pair<int,int> pii;
typedef long long ll;
const int maxn = 10000+7;
double y[maxn];
struct Line {
    double l,r,x;
    int flag;
    bool operator < (const Line& rhs) const {
        return x < rhs.x;
    }
}lines[maxn];
struct Node {
    double l,r;
    int cover;
    double len;
}tree[maxn];
void build(int rt,int l,int r){
    tree[rt].l = y[l];
    tree[rt].r = y[r];
    tree[rt].cover = -1;
    tree[rt].len = 0;
    if(l==r-1) return;
    int mid = (l+r) >> 1;
    build(rt<<1, l, mid);
    build(rt<<1|1,mid,r);
}
void pushup(int rt,int l,int r){
    if(tree[rt].cover > 0){
        tree[rt].len = tree[rt].r - tree[rt].l;
    }else if(l==r-1){
        tree[rt].len = 0;
    }else{
        tree[rt].len = tree[rt<<1].len + tree[rt<<1|1].len;
    }
}
void update(int rt,int l,int r,Line e){
    if(e.l>=y[r]||e.r<=y[l]) return;
    if(l==r-1) tree[rt].cover += e.flag;
    else{
        int mid = (l+r) >> 1;
        update(rt<<1, l, mid, e);
        update(rt<<1|1, mid, r, e);
    }
    pushup(rt,l,r);
}
int main()
{
    //FRER();
    //FREW();
    int n;
    go{
        scanf("%d",&n);
        int cnt = 0;
        for(int i=1;i<=n;i++){
            double x1,y1,x2,y2;
            scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
            lines[cnt].x = x1;
            lines[cnt].flag = 1;
            lines[cnt].l = y1;
            lines[cnt].r = y2;
            y[cnt++] = y1;
            lines[cnt].x = x2;
            lines[cnt].flag = -1;
            lines[cnt].l = y1;
            lines[cnt].r = y2;
            y[cnt++] = y2;
        }
        sort(lines,lines+cnt);
        sort(y, y+cnt);
        build(1,0,cnt-1);
        double ans = 0;
        for(int i=0;i<cnt;i++){
            update(1, 0, cnt-1, lines[i]);
            ans += tree[1].len * (lines[i+1].x-lines[i].x);
        }
        printf("%.2f\n",ans);
    }
}

两者的不同在于cover值的初值,一个为0,一个为-1

矩形周长并 POJ - 1177

#include <set>
#include <map>
#include <list>
#include <cmath>
#include <queue>
#include <vector>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define lowbit(x) x&(-x)
#define debug cout<<"****************"<<endl
#define go int T;cin>>T;for(int kase=0;kase<T;kase++)
#define FRER() freopen("/Users/seven7777777/Desktop/Code/TESTXCODE/TESTXCODE/i.txt","r",stdin)
#define FREW() freopen("/Users/seven7777777/Desktop/Code/TESTXCODE/TESTXCODE/out.txt","w",stdout)
#define eps 1e-8
#define mod 1000000007
#define yes cout << "T" << endl
#define no cout << "F" <<endl
using namespace std;
typedef pair<int,int> pii;
typedef long long ll;
const int maxn = 100000+7;
double xy[2][maxn];
struct Line {
    double l,r,idx;
    int flag;
    bool operator < (const Line& rhs) const {
        return idx < rhs.idx;
    }
}lines[2][maxn];
struct Node {
    double l,r;
    int cover;
    double len;
}tree[2][maxn];
void build(int rt,int l,int r,int idx){
    tree[idx][rt].len = 0;
    tree[idx][rt].cover = 0;
    tree[idx][rt].l = xy[idx][l];
    tree[idx][rt].r = xy[idx][r];
    if(l==r-1) return;
    int mid = (l+r) >> 1;
    build(rt<<1,l,mid,idx);
    build(rt<<1|1,mid,r,idx);
}
void pushup(int rt,int l,int r,int idx){
    if(tree[idx][rt].cover > 0) tree[idx][rt].len = tree[idx][rt].r - tree[idx][rt].l;
    else if(l==r-1) tree[idx][rt].len = 0;
    else {
        tree[idx][rt].len = tree[idx][rt<<1].len + tree[idx][rt<<1|1].len;
    }
}
void update(int rt,int l,int r,Line e,int idx){
    if(e.l>=xy[idx][r]||e.r<=xy[idx][l]) return;
    if(l==r-1) tree[idx][rt].cover += e.flag;
    else{
        int mid = (l+r) >> 1;
        update(rt<<1, l, mid, e, idx);
        update(rt<<1|1, mid, r, e, idx);
    }
    pushup(rt,l,r,idx);
}
int main()
{
    //FRER();
    //FREW();
    int n;
    scanf("%d",&n);
    int cnt = 0;
    for(int i=0;i<n;i++){
        double x1,y1,x2,y2;
        scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
        xy[0][cnt] = x1;xy[1][cnt] = y1;
        lines[0][cnt].l = x1 ; lines[0][cnt].r = x2;
        lines[1][cnt].l = y1 ; lines[1][cnt].r = y2;
        lines[0][cnt].flag = lines[1][cnt].flag = 1;
        lines[0][cnt].idx = y1 ; lines[1][cnt].idx = x1;
        cnt++;
        
        xy[0][cnt] = x2;xy[1][cnt] = y2;
        lines[0][cnt].l = x1 ; lines[0][cnt].r = x2;
        lines[1][cnt].l = y1 ; lines[1][cnt].r = y2;
        lines[0][cnt].flag = lines[1][cnt].flag = -1;
        lines[0][cnt].idx = y2 ; lines[1][cnt].idx = x2;
        cnt++;
    }
    
    sort(xy[0], xy[0]+cnt);
    sort(xy[1], xy[1]+cnt);
    sort(lines[0], lines[0]+cnt);
    sort(lines[1], lines[1]+cnt);
    
    build(1,0,cnt-1,0);
    build(1,0,cnt-1,1);
    
    double ans = 0;
    for(int i=0;i<2;i++){
        double t = 0 , last = 0;
        for(int j=0;j<cnt;j++){
            update(1, 0, cnt-1, lines[i][j], i);
            t += fabs(last-tree[i][1].len);
            last = tree[i][1].len;
        }
        ans += t;
    }
    cout << ans << endl;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值