夏夜、

心若平似镜、何题不AC。

HDU 1828 Picture 线段树 扫描线

写完后才发现数据是int不是double...

//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<iostream>
#include<sstream>
#include<cmath>
#include<climits>
#include<string>
#include<map>
#include<queue>
#include<vector>
#include<stack>
#include<set>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
#define pb(a) push_back(a)
#define INF 0x1f1f1f1f
#define lson idx<<1,l,mid
#define rson idx<<1|1,mid+1,r
#define PI  3.1415926535898
template<class T> T min(const T& a,const T& b,const T& c) {
    return min(min(a,b),min(a,c));
}
template<class T> T max(const T& a,const T& b,const T& c) {
    return max(max(a,b),max(a,c));
}
void debug() {
#ifdef ONLINE_JUDGE
#else

    freopen("d:\\in.txt","r",stdin);
   // freopen("d:\\out1.txt","w",stdout);
#endif
}
int getch() {
    int ch;
    while((ch=getchar())!=EOF) {
        if(ch!=' '&&ch!='\n')return ch;
    }
    return EOF;
}
const int maxn=5000;
struct Seg
{
    double a,b,x;
    int c;
    Seg(){}
    Seg(double a,double b,double c,int d):a(a),b(b),x(c),c(d){}
    bool operator < (const Seg& another) const
    {
        return x<another.x;
    }
};
Seg seg[maxn<<1];
double Y[maxn<<1];
int num[maxn<<3];
int flag[maxn<<3];
int point[maxn<<3][2];
double sum[maxn<<3];
int build(int idx,int l, int r)
{
    memset(num,0,sizeof(num));
    memset(flag,0,sizeof(flag));
    memset(point,0,sizeof(point));
    memset(sum,0,sizeof(sum));
    return 0;
}
int PushUp(int idx,int l,int r)
{
    if(flag[idx])
    {
        num[idx]=2;
        point[idx][0]=point[idx][1]=1;
        sum[idx]=Y[r+1]-Y[l];
    }else if(l!=r)
    {
        num[idx]=num[idx<<1]+num[idx<<1|1];
        if(point[idx<<1][1]&&point[idx<<1|1][0])
            num[idx]-=2;
        point[idx][0]=point[idx<<1][0];
        point[idx][1]=point[idx<<1|1][1];
        sum[idx]=sum[idx<<1]+sum[idx<<1|1];
    }

    else
    {
        num[idx]=0;
        point[idx][0]=point[idx][1]=0;
        sum[idx]=0;
    }

    return 0;
}
int update(int idx,int l,int r,int tl,int tr,int v)
{
    if(tl<=l&&tr>=r)
    {
        flag[idx]+=v;
        PushUp(idx,l,r);
        return 0;
    }
    int mid=(r+l)>>1;
    if(tl<=mid)update(lson,tl,tr,v);
    if(tr>mid)update(rson,tl,tr,v);
    PushUp(idx,l,r);
    return 0;
}
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        int m=0;
        for(int i=0;i<n;i++)
        {
            double a,b,c,d;
            scanf("%lf%lf%lf%lf",&a,&b,&c,&d);
            seg[m]=Seg(b,d,a,1);
            Y[m++]=b;
            seg[m]=Seg(b,d,c,-1);
            Y[m++]=d;
        }
        sort(seg,seg+m);
        sort(Y,Y+m);
        int k=1;
        for(int i=1;i<m;i++)
            if(Y[i]!=Y[i-1])Y[k++]=Y[i];
        double res=0;
        build(1,0,k);
        for(int i=0;i<m;i++)
        {
            if(i!=0)
                res+=(seg[i].x-seg[i-1].x)*num[1];
            int l=lower_bound(Y,Y+k,seg[i].a)-Y;
            int r=lower_bound(Y,Y+k,seg[i].b)-Y;
            double last=sum[1];
            update(1,0,k,l,r-1,seg[i].c);
            res+=fabs(sum[1]-last);

        }
        printf("%d\n",int(res+0.00001));
    }
    return 0;
}
View Code

 

posted on 2013-09-12 11:30  BMan、  阅读(186)  评论(0编辑  收藏  举报

导航