ZOJ 3544 / HDU 4056 Draw a Mess( 并查集好题 )

方法参见:http://blog.acmol.com/?p=751

从最后一个线段开始倒着处理(因为之后的线段不会被它之前的线段覆盖),把这条线段所覆盖的所有线段编号合并到一个集合里,并以最左边线段编号为父结点。然后,以后的线段每次都是从右端向左端进行以下处理:

1、判断该线段在并查集中的根结点是否被覆盖过(用一个数组标记),如果没有被覆盖,则将该线段所在集合与海报左端点所在集合进行合并(以左端点所在集合为根)。

2、然后开始处理刚处理过的线段父结点左边的那一个线段,处理方法与第1步时一样。

3、直到要处理的线段在左端点的左边时停止循环。

处理时,如果有一个线段未被覆盖,就证明该点的染色没有被之后的染色覆盖掉。

我头一次知道还有这种搞法,涨姿势了。

PS.之前我觉得Diamond和Circle是弄出来应该是一样的形状,就把它俩当一样处理了,事实证明这是不对的……

 

#include <cstdio>

#include <cstring>

#include <cstdlib>

#include <cmath>

#include <algorithm>



using namespace std;



const int MAXN = 52222;



struct node

{

    char op[20];

    int xc, yc;

    int l, w, c;

};



int N, M, Q;

node D[MAXN];

int p[MAXN];

bool vis[MAXN];



int find( int x )

{

    return p[x] == x ? x : p[x] = find(p[x]);

}



int main()

{

    while ( scanf( "%d%d%d", &N, &M, &Q ) == 3 )

    {

        for ( int i = 0; i < Q; ++i )

        {

            scanf( "%s%d%d", D[i].op, &D[i].xc, &D[i].yc );

            if ( D[i].op[0] == 'R' )

                scanf("%d%d%d", &D[i].l, &D[i].w, &D[i].c );

            else

                scanf( "%d%d", &D[i].w, &D[i].c );

        }



        int ans[10] = { 0 };

        for ( int i = 0; i < N; ++i )

        {

            for ( int j = 0; j < M; ++j )

            {

                vis[j] = false;

                p[j] = j;

            }

            for ( int j = Q - 1; j >= 0; --j )

            {

                int l, r;

                int color = D[j].c;

                if ( D[j].op[0] == 'R' )

                {

                    if ( i < D[j].xc || i >= D[j].xc + D[j].l ) continue;

                    l = D[j].yc;

                    r = D[j].yc + D[j].w - 1;

                }

                else if ( D[j].op[0] == 'D' )

                {

                    if ( i < D[j].xc - D[j].w || i > D[j].xc + D[j].w ) continue;

                    l = D[j].yc - ( D[j].w - abs( i - D[j].xc ) );

                    r = D[j].yc + ( D[j].w - abs( i - D[j].xc ) );

                }

                else if ( D[j].op[0] == 'C' )

                {

                    if ( ( i - D[j].xc )*( i - D[j].xc ) > D[j].w*D[j].w ) continue;

                    int ww = (int)( sqrt( ( double)(D[j].w*D[j].w - ( i - D[j].xc )*( i - D[j].xc ) ) ) + 1e-8 );

                    l = D[j].yc - ww;

                    r = D[j].yc + ww;

                }

                else

                {

                    if ( i < D[j].xc || i > D[j].xc + (D[j].w+1)/2 - 1 ) continue;

                    int ww = D[j].w - 2*( i - D[j].xc );

                    l = D[j].yc - ww / 2;

                    r = D[j].yc + ww / 2;

                }



                l = max( 0, l );

                r = min( M - 1, r );



                int xx = find(l);

                int yy;

                for ( int k = r; k >= l; k = yy - 1 )

                {

                    yy = find(k);

                    if ( !vis[yy] ) ++ans[color];

                    vis[yy] = true;

                    if ( xx != yy ) p[yy] = xx;

                }

            }

        }



        for ( int i = 1; i <= 9; ++i )

        {

            if ( i != 1 ) putchar(' ');

            printf( "%d", ans[i] );

        }

        puts("");

    }

    return 0;

}

 

之前线段树也能做,只不过效率和代码长度都比较惊悚。

ZOJ上因为内存给的比较宽松,以前线段树可以AC,但是现在好像把数据加强了,线段树一定TLE……

HDU一定MLE……

附:线段树代码,TLE

#include <cstdio>

#include <cstdlib>

#include <cstring>



#define lson l, m, rt << 1

#define rson m + 1, r, rt << 1 | 1

#define lc rt << 1

#define rc rt << 1 | 1



const int MAXN = 52222;



int N, M, Q;

short tree[201][ MAXN << 2 ];

char op[20];

int ans[10];



inline int max( int a, int b )

{

    return a > b ? a : b;

}



inline int min( int a, int b )

{

    return a < b ? a : b;

}



inline int abs( int a )

{

    return a >= 0 ? a : -a;

}



inline void update( short *color, int L, int R, short val, int l, int r, int rt )

{

    if ( L <= l && r <= R )

    {

        color[rt] = val;

        return;

    }

    //if ( l >= r ) return;



    if ( color[rt] )

    {

        color[lc] = color[rc] = color[rt];

        color[rt] = 0;

    }



    int m = ( l + r ) >> 1;

    if ( L <= m ) update( color, L, R, val, lson );

    if ( R > m )  update( color, L, R, val, rson );



    //if ( color[lc] == color[rc] ) color[rt] = color[lc];

    return;

}



inline void query( short *color, int l, int r, int rt )

{

    if ( color[rt] != 0 )

    {

        //printf("[%d %d]: %d\n", l, r, color[rt] );

        ans[ color[rt] ] += r - l + 1;

        return;

    }

    if ( l >= r ) return;

    int m = ( l + r ) >> 1;

    query( color, lson );

    query( color, rson );

    return;

}



int main()

{

    while ( scanf( "%d%d%d", &N, &M, &Q ) == 3 )

    {

        for ( int i = 0; i < N; ++i )

            memset( tree[i], 0, sizeof(short)*(( N << 2 ) + 4) );



        while ( Q-- )

        {

            scanf( "%s", op );

            if ( op[0] == 'D' || op[0] == 'C' )

            {

                int xc, yc, r, c;

                scanf("%d%d%d%d", &xc, &yc, &r, &c);

                int stX = max( 0, xc - r );

                int edX = min( N - 1, xc + r );

                //printf( "stX=%d edX=%d\n", stX, edX );

                if ( r == 0 )

                {

                    update( tree[xc], yc, yc, c, 0, M - 1, 1 );

                    continue;

                }



                for ( int i = stX; i <= edX; ++i )

                {

                    int stY = max( 0, yc - ( r - abs( i - xc ) ) );

                    int edY = min( M - 1, yc + ( r - abs(i - xc) ) );

                    //printf("**%d %d\n", stY, edY );

                    update( tree[i], stY, edY, c, 0, M - 1, 1 );

                }

            }

            else if ( op[0] == 'T' )

            {

                int xc, yc, w, c;

                scanf( "%d%d%d%d", &xc, &yc, &w, &c );

                int stx = xc, sty = yc;

                int limitX = min( N - 1, xc + (w+1)/2 - 1 );

                //printf( "T: %d %d\n", xc, limitX );

                for ( int i = stx; i <= limitX && w >= 0; ++i )

                {

                    update( tree[i], max( sty - w/2, 0 ), min( sty + w/2, M - 1 ), c, 0, M - 1, 1 );

                    w -= 2;

                    if ( w < 0 ) break;

                }

            }

            else if ( op[0] == 'R' )

            {

                int xc, yc, l, w, c;

                scanf( "%d%d%d%d%d", &xc, &yc, &l, &w, &c );

                if ( l == 0 || w == 0 ) continue;

                int limitX = min( xc + l - 1, N - 1 );

                int limitY = min( yc + w - 1, M - 1 );

                //printf("R: %d %d %d %d\n", xc, limitX, yc, limitY );

                for ( int i = xc; i <= limitX; ++i )

                    update( tree[i], yc, limitY, c, 0, M - 1, 1 );

            }

        }



        memset( ans, 0, sizeof(ans) );

        for ( int j = 0; j < N; ++j )

            query( tree[j], 0, M - 1, 1 );



        bool first = false;

        for ( int i = 1; i <= 9; ++i )

        {

            if ( first ) putchar(' ');

            printf( "%d", ans[i] );

            first = true;

        }

        puts("");

    }

    return 0;

}

 

你可能感兴趣的:(HDU)