矩形周长并

来炫耀一下我的1A

#include<bits/stdc++.h>
#define x1 _x1
#define y1 _y1
#define x2 _x2
#define y2 _y2
#define lson (node<<1)
#define rson (node<<1|1)
using namespace std;
const int N=100001;
struct data{
    int x1,x2,y,v;
}a[N<<1];
int n;
map<int,int> mp;
struct seg{
    int cnt,lins,lbd,rbd,m;
}t[N<<3];
int xx[N<<1];

void read(int &x){
    char ch=getchar();x=0;int w=1;
    for(;ch<'0'||ch>'9';ch=getchar()) if (ch=='-') w=-1;
    for(;ch>='0'&&ch<='9';ch=getchar()) x=(x<<3)+(x<<1)+ch-'0';
    x*=w;
}

int cmp(const data &q,const data &w){
    if (q.y==w.y) return q.v>w.v;
    return q.y<w.y;
}

void update(int node,int L,int r){
    seg &q=t[node];
    if (q.cnt) return;
    q.lins=t[lson].lins+t[rson].lins;
    if (t[lson].rbd&&t[rson].lbd) q.lins--;
    q.lbd=t[lson].lbd;q.rbd=t[rson].rbd;
    q.m=t[lson].m+t[rson].m;
}

void cover(int node,int L,int r,int s){
    seg &q=t[node];
    q.cnt+=s;
    if (q.cnt){
        q.lins=q.lbd=q.rbd=1;
        q.m=xx[r]-xx[L-1];
    } else{
        if (L==r){
            q.lins=q.lbd=q.rbd=q.m=0;
            return;
        }
        update(node,L,r);
    }
}

void insert(int node,int L,int r,int x,int y,int v){
    seg &q=t[node];
    if (L>=x&&r<=y){
        cover(node,L,r,v);
        return;
    }
    int mid=L+r>>1;
    if (x<=mid) insert(lson,L,mid,x,y,v);
    if (y>mid) insert(rson,mid+1,r,x,y,v);
    update(node,L,r);
}

int main(){
    read(n);
    int x1,y1,x2,y2;
    for(int i=1;i<=n;i++){
        read(x1);read(y1);
        read(x2);read(y2);
        mp[x1]=1;mp[x2]=1;
        a[i*2-1]=(data){x1,x2,y1,1};
        a[i*2]=(data){x1,x2,y2,-1};
    }
    int cnt=-1;
    for(map<int,int>::iterator it=mp.begin();it!=mp.end();it++)
        it->second=++cnt,xx[cnt]=it->first;
    sort(a+1,a+1+(n<<1),cmp);
    int pre=0,ans=0;
    for(int i=1;i<=n<<1;i++){
        x1=mp[a[i].x1];x2=mp[a[i].x2];
        ans+=2*t[1].lins*(a[i].y-a[i-1].y);
        insert(1,1,cnt,x1+1,x2,a[i].v);
        ans+=abs(t[1].m-pre);
        pre=t[1].m;
    }
    cout<<ans<<endl;
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值