zoukankan      html  css  js  c++  java
  • HDU 1828 POJ 1177 Picture

    矩形周长并

    POJ上C++,G++都能过,HDU上C++过了,G++WA ,不知道为什么

    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<map>
    #include<algorithm>
    using namespace std;
    
    const int maxn=10000;
    struct Seg
    {
        int x;
        int Y1,Y2;//离散化之后的坐标
        int flag;
    }s[maxn];
    int X1[maxn],X2[maxn],Y1[maxn],Y2[maxn];
    map<int ,int>m;
    int M[maxn];int k;
    int n,tot;
    int sum,ans;
    
    struct SegTree
    {
        int len;
        int cover;
    } segTree[maxn*4];
    
    bool cmp(const Seg&a,const Seg&b)
    {
        return a.x<b.x;
    }
    
    void lsh1()
    {
        k=0;
        m.clear();
        for(int i=1; i<=n; i++)
        {
            if(m[Y1[i]]==0) M[k++]=Y1[i],m[Y1[i]]=1;
            if(m[Y2[i]]==0) M[k++]=Y2[i],m[Y2[i]]=1;
        }
        sort(M,M+k);
        m.clear();
        for(int i=0; i<k; i++) m[M[i]]=i;
    }
    
    void lsh2()
    {
        k=0;
        m.clear();
        for(int i=1; i<=n; i++)
        {
            if(m[X1[i]]==0) M[k++]=X1[i],m[X1[i]]=1;
            if(m[X2[i]]==0) M[k++]=X2[i],m[X2[i]]=1;
        }
        sort(M,M+k);
        m.clear();
        for(int i=0; i<k; i++) m[M[i]]=i;
    }
    
    void build(int l,int r,int rt)
    {
        segTree[rt].cover=0;
        segTree[rt].len=0;
        if(l==r) return;
        int m=(l+r)/2;
        build(l,m,2*rt);
        build(m+1,r,2*rt+1);
    }
    
    void pushUp(int rt,int l,int r)
    {
        if(segTree[rt].cover) segTree[rt].len=M[r]-M[l-1];
        else segTree[rt].len=segTree[2*rt].len+segTree[2*rt+1].len;
    }
    
    void update(int info,int L,int R,int l,int r,int rt)
    {
        if(L<=l&&r<=R)
        {
            segTree[rt].cover=segTree[rt].cover+info;
            pushUp(rt,l,r);
            return;
        }
    
        int m=(l+r)/2;
        if(L<=m) update(info,L,R,l,m,2*rt);
        if(R>m)  update(info,L,R,m+1,r,2*rt+1);
        pushUp(rt,l,r);
    }
    
    int main()
    {
        int Case=1;
        while(~scanf("%d",&n))
        {
            if(n==0)
            {
                printf("0
    ");
                continue;
            }
            for(int i=1; i<=n; i++)
                scanf("%d%d%d%d",&X1[i],&Y1[i],&X2[i],&Y2[i]);
    
            lsh1();
    
            tot=0;
            for(int i=1; i<=n; i++)
            {
                s[tot].x=X1[i],s[tot].Y1=m[Y1[i]],s[tot].Y2=m[Y2[i]],s[tot].flag=1,tot++;
                s[tot].x=X2[i],s[tot].Y1=m[Y1[i]],s[tot].Y2=m[Y2[i]],s[tot].flag=-1,tot++;
            }
            sort(s,s+tot,cmp);
    
            ans=0;
    
            build(1,k,1);
           
            for(int i=0; i<tot; i++)
            {
                int sum1=segTree[1].len;
                update(s[i].flag,s[i].Y1+1,s[i].Y2,1,k,1);
                int sum2=segTree[1].len;
                ans=ans+abs(sum2-sum1);
            }
    
            lsh2();
    
            tot=0;
            for(int i=1; i<=n; i++)
            {
                s[tot].x=Y1[i],s[tot].Y1=m[X1[i]],s[tot].Y2=m[X2[i]],s[tot].flag=1,tot++;
                s[tot].x=Y2[i],s[tot].Y1=m[X1[i]],s[tot].Y2=m[X2[i]],s[tot].flag=-1,tot++;
            }
            sort(s,s+tot,cmp);
    
            build(1,k,1);
           
            for(int i=0; i<tot; i++)
            {
                int sum1=segTree[1].len;
                update(s[i].flag,s[i].Y1+1,s[i].Y2,1,k,1);
                int sum2=segTree[1].len;
                ans=ans+abs(sum2-sum1);
            }
    
            printf("%d
    ",ans);
        }
        return 0;
    }
  • 相关阅读:
    PointToPointNetDevice doesn't support TapBridgeHelper
    NS3系列—10———NS3 NodeContainer
    NS3系列—9———NS3 IP首部校验和
    NS3系列—8———NS3编译运行
    【习题 7-6 UVA
    【Good Bye 2017 C】 New Year and Curling
    【Good Bye 2017 B】 New Year and Buggy Bot
    【Good Bye 2017 A】New Year and Counting Cards
    【Educational Codeforces Round 35 D】Inversion Counting
    【Educational Codeforces Round 35 C】Two Cakes
  • 原文地址:https://www.cnblogs.com/zufezzt/p/5055262.html
Copyright © 2011-2022 走看看