C - 覆盖的面积 HDU - 1255

https://cn.vjudge.net/contest/303187#problem/C 

总结: 覆盖一次,sum2是至少覆盖两次;因为时区间修改,并不是单点修改所以当前覆盖两次的是当前区间的子区间又覆盖过一次;    else if(tree[rt].cover==1)tree[rt].len2=tree[lson].len1+tree[rson].len1;

#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;
double xx1,xx2,yy1,yy2;
int cnt;
double y[N<<1];
struct node
{
    int l,r,cover;
    double len1,len2;
}tree[N<<3];

struct L
{
    double 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].len1=y[tree[rt].r+1]-y[tree[rt].l];
    else if(tree[rt].l==tree[rt].r) tree[rt].len1=0;
    else tree[rt].len1=tree[lson].len1+tree[rson].len1;

    if(tree[rt].cover>1) tree[rt].len2=y[tree[rt].r+1]-y[tree[rt].l];
    else if(tree[rt].l==tree[rt].r) tree[rt].len2=0;
    else if(tree[rt].cover==1)tree[rt].len2=tree[lson].len1+tree[rson].len1;
    else tree[rt].len2=tree[lson].len2+tree[rson].len2;
}
void build(int l,int r,int rt)
{
    tree[rt].l=l;
    tree[rt].r=r;
    tree[rt].len1=0;
    tree[rt].len2=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);

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

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

            y[cnt]=yy1;

        }
        sort(y+1,y+1+cnt);
        sort(line+1,line+1+cnt);
        int m=unique(y+1,y+1+cnt)-y-1;
        build(1,cnt,1);
        double 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].len2*(line[i+1].x-line[i].x);
        }
        printf("%.2lf\n",ans+eps);
    }


    return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值