[题解/模板]扫描线

luogu_P1856矩形周长

#include<bits/stdc++.h>
#define ls (x<<1)
#define rs (x<<1|1)
//#define mid (l+r>>1)
using namespace std;
const int maxn=5009;
const int maxm=10009;
int n,ans;
struct square{
    int xa,xb,ya,yb;
}r[maxn];
int hsh[maxn<<1],cnt;
struct line{
    int l,r,h,f;
    bool operator <(const line&a)const{
        return h<a.h||h==a.h &&f>a.f;
    }
}e[maxn<<1];
struct node{
    int sum,tag;
}t[maxm<<2];
void upd(int x,int l,int r){
    if(t[x].tag)t[x].sum=hsh[r+1]-hsh[l];
    else if(l==r)t[x].sum=0;
    else t[x].sum=t[ls].sum+t[rs].sum;
}
void change(int x,int l,int r,int L,int R,int f){
    if(L<=l && r<=R){
        t[x].tag+=f;

    }
    else{
        int mid=(l+r)>>1;
        if(L<=mid)change(ls,l,mid,L,R,f);
        if(R>mid)change(rs,mid+1,r,L,R,f);
    }
    upd(x,l,r);
}
//int find(int x,int l,int r){
//    while(l<=r){
//        int mid=l+r>>1;
//        if(hsh[mid]<x)l=mid+1;
//        else if(hsh[mid]==x)return mid;
//        else r=mid-1;
//    }
//}
int solve(){
    int res=0;
    sort(hsh+1,hsh+1+cnt);
    sort(e+1,e+1+cnt);
    for(int i=1;i<=cnt;i++){
        int l=lower_bound(hsh+1,hsh+1+cnt,e[i].l)-hsh,
        r=lower_bound(hsh+1,hsh+1+cnt,e[i].r)-hsh-1;//这里要减一线段树维护的长度才正确
//        int l=find(e[i].l,1,cnt),r=find(e[i].r,1,cnt)-1;
        if(l<=r){
            int lst=t[1].sum;
            change(1,1,cnt,l,r,e[i].f);
            res+=abs(t[1].sum-lst);
        }
    }
    return res;
}
int main(){
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        scanf("%d%d%d%d",&r[i].xa,&r[i].ya,&r[i].xb,&r[i].yb);
        hsh[++cnt]=r[i].xa;e[cnt].l=r[i].xa;
        e[cnt].r=r[i].xb;e[cnt].h=r[i].ya;e[cnt].f=1;
        hsh[++cnt]=r[i].xb;e[cnt].l=r[i].xa;
        e[cnt].r=r[i].xb,e[cnt].h=r[i].yb,e[cnt].f=-1;
    }
    ans+=solve();
    cnt=0;
    for(int i=1;i<=n;i++){
        hsh[++cnt]=r[i].ya,e[cnt].l=r[i].ya;
        e[cnt].r=r[i].yb,e[cnt].h=r[i].xa,e[cnt].f=1;
        hsh[++cnt]=r[i].yb,e[cnt].l=r[i].ya;
        e[cnt].r=r[i].yb,e[cnt].h=r[i].xb,e[cnt].f=-1;
    }
    ans+=solve();
    printf("%d",ans);
}

POJ_1151_Atlantis

//#include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define ls (x<<1)
#define rs (x<<1|1)
#define ll long long
using namespace std;
const int maxn=509;
int n;
double ans;
struct node{
    double xa,xb,ya,yb;
}r[maxn];
double hsh[maxn<<1];int cnt;
struct line{
    double l,r,h;
    int f;
    bool operator <(const line &a)const{
        return h<a.h || h==a.h && f>a.f;
    }
}e[maxn<<1];
struct tree{
    double sum;
    int tag;
}t[maxn<<2];
void upd(int x,int l,int r){
    if(t[x].tag)t[x].sum=hsh[r+1]-hsh[l];
    else if(l==r)t[x].sum=0;
    else t[x].sum=t[ls].sum+t[rs].sum;
}
void change(int x,int l,int r,int L,int R,int f){
    if(L<=l && r<=R){
        t[x].tag+=f;
    }
    else{
        int mid=l+r>>1;
        if(L<=mid)change(ls,l,mid,L,R,f);
        if(R>mid)change(rs,mid+1,r,L,R,f);
    }
    upd(x,l,r);
}
double solve(){
    double res=0;
    sort(hsh+1,hsh+1+cnt);
    sort(e+1,e+1+cnt);
    int tot=unique(hsh+1,hsh+1+cnt)-hsh-1;
    for(int i=1;i<cnt;i++){
        int l=lower_bound(hsh+1,hsh+1+tot,e[i].l)-hsh,
        r=lower_bound(hsh+1,hsh+1+tot,e[i].r)-hsh-1;
        if(l<=r){
            change(1,1,tot,l,r,e[i].f);
        }res+=fabs(t[1].sum)*(e[i+1].h-e[i].h);
    }
    return res;
}
int main(){int tt=0;
    while(scanf("%d",&n) && n){tt++;
        cnt=0;
        memset(hsh,0,sizeof(hsh));
        memset(t,0,sizeof(t));
        for(int i=1;i<=n;i++){
            scanf("%lf%lf%lf%lf",&r[i].xa,&r[i].ya,&r[i].xb,&r[i].yb);
            hsh[++cnt]=r[i].xa;
            e[cnt].l=r[i].xa;
            e[cnt].r=r[i].xb;
            e[cnt].h=r[i].ya;
            e[cnt].f=1;
            hsh[++cnt]=r[i].xb;
            e[cnt].l=r[i].xa;
            e[cnt].r=r[i].xb;
            e[cnt].h=r[i].yb;
            e[cnt].f=-1;
        }
        printf("Test case #%d\n",tt);
        printf("Total explored area: %.2f\n\n",solve());
    }
}

原文地址:https://www.cnblogs.com/superminivan/p/11468482.html

时间: 2024-10-22 04:26:51

[题解/模板]扫描线的相关文章

[题解/模板]luogu_P3942_(树上覆盖问题

抄题解 把点按深度排序,用near数组记录到每个点最近的关键点的距离,每次取出一个点更新一下near数组,如果不能被覆盖就在它的k级祖先建立关键点,并更新所有k级祖先的k级祖先的near数组 #include<bits/stdc++.h> using namespace std; const int maxn=100009; int n,k,t,ans; struct edge{ int v,nxt; }e[maxn<<1]; int head[maxn],cnt; inline

uva 11983 Weird Advertisement 扫描线

Weird Advertisement Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/problem/visitOriginUrl.action?id=18802 Description 2DPlaneLand is a land just like a huge 2D plane. The range of X axis is 0 to 109 and the range ofY axis

POJ1151 Atlantis 【扫描线】

Atlantis Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 16882   Accepted: 6435 Description There are several ancient Greek texts that contain descriptions of the fabled island Atlantis. Some of these texts even include maps of parts of

POJ1151 Atlantis 【扫描线】+【线段树】+【离散化】

Atlantis Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 16882   Accepted: 6435 Description There are several ancient Greek texts that contain descriptions of the fabled island Atlantis. Some of these texts even include maps of parts of

「题解」kuangbin 最小生成树

POJ-1251 Jungle Roads (水题,%c) POJ-1287 Networking (水) POJ-2031 Building a Space Station (%f浮点数尴尬精度,两球间距离) POJ-2421 Constructing Roads (一些边已建好,简单处理一下) ZOJ-1586 QS Network (处理一下边权) HDU-1233 还是畅通工程 (水) HDU-1875 畅通工程再续 (浮点数,条件连边) HDU-1301 Jungle Roads (重

codeforces 几道题目

BZOJ挂了....明天就要出发去GDKOI了....不能弃疗. 于是在cf水了几道题, 写写详(jian)细(dan)题解, 攒攒RP, 希望GDKOI能好好发挥.......  620E. New Year Tree 题目大意: N个结点的树, 结点1为根, 要支持2种操作(M个操作): 1.将以v为根的子树所有节点的颜色为c 2.询问以v为根的子树中不同颜色个数 N,M<=4*10^5, 1<=c<=60 题解: 处理出dfs序, 线段树维护. 1,2操作都对应线段树的一段区间(子

Ampzz 2011 Cross Spider 计算几何

原题链接:http://codeforces.com/gym/100523/attachments/download/2798/20142015-ct-s02e07-codeforces-trainings-season-2-episode-7-en.pdf 题意 给你一堆三维点,问你他们是否共面 题解 模板题,套版就好,需要注意的是共线 代码 #include<iostream> #include<cmath> #include<cstdio> #define MAX

2015 年 JXNU_ACS 算法组寒假第二次周赛

2015 年 JXNU_ACS 算法组寒假第二次周赛 比赛链接:http://acm.hdu.edu.cn/diy/contest_show.php?cid=26246 Start Time : 2015-02-01 13:00:00 End Time : 2015-02-01 17:30:00 终榜:http://acm.hdu.edu.cn/diy/contest_ranklist.php?cid=26246&page=1 这次比赛考查的知识都很基础,甚至还有几道就是C语言基础(比如1006

POJ1458 &amp;&amp; HDOJ1159 Common Subsequence【LCS】

题目链接(POJ) :http://poj.org/problem?id=1458 题目链接(HDOJ):http://acm.hdu.edu.cn/showproblem.php?pid=1159 Common Subsequence Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 40156   Accepted: 16162 Description A subsequence of a given sequence