2018 ICPC Pacific Northwest Regional Contest Problem F Rectangles

 http://codeforces.com/gym/101982/attachments

思路:只有将区间最大长度减更改的长度即可;就是异或了;

注意:异或不是出入边标志;而是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,sum;
}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)
{
    tree[rt].len=tree[lson].len+tree[rson].len;
    tree[rt].sum=tree[lson].sum+tree[rson].sum;
}
void pushdown(int rt)
{
    int l=tree[rt].l;
    int r=tree[rt].r;
    int mid=(l+r)>>1;
    if(tree[rt].cover!=0)
    {
        tree[lson].len=tree[lson].sum-tree[lson].len;
        tree[rson].len=tree[rson].sum-tree[rson].len;
        tree[lson].cover^=1;
        tree[rson].cover^=1;
        tree[rt].cover=0;
    }
}
void build(int l,int r,int rt)
{
    tree[rt].l=l;
    tree[rt].r=r;
    tree[rt].len=0;
    tree[rt].sum=0;
    tree[rt].cover=0;
    if(l==r)
    {
        tree[rt].sum=y[l+1]-y[l];
        return ;
    }
    int mid=(l+r)>>1;
    build(l,mid,lson);
    build(mid+1,r,rson);
    pushup(rt);
}
void update(int ql,int qr,int rt,int v=1)
{
    int l=tree[rt].l;
    int r=tree[rt].r;
    if(ql<=tree[rt].l&&qr>=tree[rt].r)
    {
        tree[rt].cover^=v;
        tree[rt].len=tree[rt].sum-tree[rt].len;
        return ;
    }
    pushdown(rt);
    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);
    cnt=0;
    for(int i=1;i<=n;i++)
    {
        scanf("%d%d%d%d",&xx1,&yy1,&xx2,&yy2);
        if(xx1>xx2) swap(xx1,xx2);
        if(yy1>yy2) swap(yy1,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);
    int m=unique(y+1,y+1+cnt)-y-1;
    build(1,m,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);
        ans+=tree[1].len*(ll)(line[i+1].x-line[i].x);
        //cout<<tree[1].len<<endl;
    }
    printf("%lld\n",ans);

    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值