P5490 【模板】扫描线

https://www.luogu.org/problem/P5490 

总结:要左开右闭,不然节点中间空出来的就不会计算了;所以计算时用y[pos+1]; 

#include <iostream>
#include <cstring>
#include <queue>
#include <vector>
#include <algorithm>
#include <cstdio>
#include <set>
#include <map>
#include <cmath>
//#include<bits/stdc++.h>
using namespace std;

#define sfi(i) scanf("%d",&i)
#define sfl(i) scanf("%lld",&i)
#define sfs(i) scanf("%s",(i))
#define pri(i) printf("%d\n",i)
#define sff(i) scanf("%lf",&i)
#define ll long long
#define ull unsigned long long
#define mem(x,y) memset(x,y,sizeof(x))
#define INF 0x3f3f3f3f
#define eps 1e-10
#define PI acos(-1.0)
#define lowbit(x) ((x)&(-x))
#define fl() printf("flag\n")
#define MOD(x) ((x%mod)+mod)%mod
#define endl '\n'
#define pb push_back
#define lson (rt<<1)
#define rson (rt<<1|1)
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)

const int N=2e5+9;
const int M=4e5+9;

int n;
int xx1,xx2,yy1,yy2;
int cnt;
int y[N<<1];
struct node
{
    int l,r,cover;
    ll len;
}tree[N<<3];

struct L
{
    int x,y1,y2;
    int f;
    bool operator < (const L &b) const
    {
        if(x!=b.x) return x<b.x;
        else return f>b.f;
    }
}line[N<<3];
void pushup(int rt)
{
    if(tree[rt].cover) tree[rt].len=y[tree[rt].r+1]-y[tree[rt].l];//!!!!
    else if(tree[rt].l==tree[rt].r) tree[rt].len=0;
    else tree[rt].len=tree[lson].len+tree[rson].len;
}
void build(int l,int r,int rt)
{
    tree[rt].l=l;
    tree[rt].r=r;
    tree[rt].len=0;
    tree[rt].cover=0;
    if(l==r) return ;
    int mid=(l+r)>>1;
    build(l,mid,lson);
    build(mid+1,r,rson);
}
void update(int ql,int qr,int rt,int v)
{
    int l=tree[rt].l;
    int r=tree[rt].r;
    if(ql<=tree[rt].l&&qr>=tree[rt].r)
    {
        tree[rt].cover+=v;
        pushup(rt);
        return ;
    }
    int mid=(l+r)>>1;
    if(ql<=mid) update(ql,qr,lson,v);
    if(qr>mid) update(ql,qr,rson,v);
    pushup(rt);
}
int main()
{
    //FAST_IO;
    freopen("input.txt","r",stdin);

    sfi(n);
    for(int i=1;i<=n;i++)
    {
        scanf("%d%d%d%d",&xx1,&yy1,&xx2,&yy2);
        line[++cnt].x=xx1;
        line[cnt].y1=yy1;
        line[cnt].y2=yy2;
        line[cnt].f=1;

        y[cnt]=yy1;

        line[++cnt].x=xx2;
        line[cnt].y1=yy1;
        line[cnt].y2=yy2;
        line[cnt].f=-1;

        y[cnt]=yy2;

    }
    sort(y+1,y+1+cnt);
    sort(line+1,line+1+cnt);
    build(1,cnt,1);
    int m=unique(y+1,y+1+cnt)-y-1;
    ll ans=0;
    for(int i=1;i<=cnt;i++)
    {
        int pos1=lower_bound(y+1,y+1+m,line[i].y1)-y;
        int pos2=lower_bound(y+1,y+1+m,line[i].y2)-y;
        //cout<<pos1<<" "<<pos2<<endl;
        update(pos1,pos2-1,1,line[i].f);//!!!!
        ans+=tree[1].len*(line[i+1].x-line[i].x);
    }
    printf("%lld\n",ans);

    return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值