zoj 3544 路径压缩

来源:互联网 发布:腾讯淘宝精选 编辑:程序博客网 时间:2024/06/07 12:35
 
Draw a Mess

Time Limit: 5 Seconds      Memory Limit: 262144 KB

It's graduated season, every students should leave something on the wall, so....they draw a lot of geometry shape with different color.

When teacher come to see what happened, without getting angry, he was surprised by the talented achievement made by students. He found the wall full of color have a post-modern style so he want to have an in-depth research on it.

To simplify the problem, we divide the wall into n*m (1 ≤ n ≤ 200, 1 ≤ m ≤ 50000) pixels, and we have got the order of coming students who drawing on the wall. We found that all students draw four kinds of geometry shapes in total that is Diamond, Circle, Rectangle and Triangle. When a student draw a shape in pixel (i, j) with color c (1 ≤ c ≤ 9), no matter it is covered before, it will be covered by colorc.

There are q (1 ≤ q ≤ 50000) students who have make a drawing one by one. And afterq operation we want to know the amount of pixels covered by each color.

Input

There are multiple test cases.

In the first line of each test case contains three integers n, m,q. The next q lines each line contains a string at first indicating the geometry shape:

  • Circle: given xc, yc, r, c, and you should cover the pixels(x,y) which satisfied inequality (x - xc)2 + (y - yc)2 ≤ r2 with colorc;
  • Diamond: given xc, yc, r, c, and you should cover the pixels(x,y) which satisfied inequality abs(x - xc) + abs(y - yc) ≤ r with colorc;
  • Rectangle: given xc, yc, l, w, c, and you should cover the pixels(x,y) which satisfied xc ≤ x ≤ xc+l-1, yc ≤ y ≤ yc+w-1 with colorc;
  • Triangle: given xc, yc, w, c, W is the bottom length and is odd, the pixel(xc,yc) is the middle of the bottom. We define this triangle is isosceles and the height of this triangle is (w+1)/2, you should cover the correspond pixels with colorc;

Note: all shape should not draw out of the n*m wall! You can get more details from the sample and hint. (0 ≤xc, xn-1, 0 ≤ yc, ym-1)

Output

For each test case you should output nine integers indicating the amount of pixels covered by each color.

Sample Input

8 8 4Diamond 3 3 1 1Triangle 4 4 3 2Rectangle 1 1 2 2 3Circle 6 6 2 4

Sample Output

4 4 4 11 0 0 0 0 0

Hint

The final distribution of different colors:
00000000
03300000
03310000
00111000
00022240
00002444
00004444
00000444

线段树做一直超时

从后往前涂色,每涂一次色,就把对应格子删除,记next[i] = i; 当i这个格子删除的时候 next[i] = i+1 ,再利用路径压缩,和并查集的思想是一样的,这样平摊的复杂度分析为 O(n*m)

# include <math.h># include <stdio.h># include <string.h># include <stdlib.h># include <algorithm># include <iostream># include <string># include <queue># include <stack># include <map># include <set># include <vector># include <cstring># include <list># include <ctime># define For(i,a)   for((i)=0;i<(a);(i)++)# define MAX(x,y)   ((x)>(y)? (x):(y))# define MIN(x,y)   ((x)<(y)? (x):(y))# define MEM(a)     (memset((a),0,sizeof(a)))# define MEME(a)    (memset((a),-1,sizeof(a)))# define MEMX(a)    (memset((a),0x7f,sizeof(a)))using namespace std;typedef long long           ll      ;typedef unsigned long long  ull     ;typedef unsigned int        uint    ;typedef unsigned char       uchar   ;#define N 205#define M 100500char str[M][25];int xx[M],yy[M],p[M],t[M],c[M];int next[N][M];int find(int i,int t){    if(next[i][t]!=t)        next[i][t]=find(i,next[i][t]);    return next[i][t];}int dele(int i,int l,int r){    int ret=0;    l=find(i,l);    while(l<=r)    {        ret++;        next[i][l]=l+1;        l=find(i,l);    }    return ret;}inline ll isqrt(long long x) //开方取整{     long long l = 0, r = 100860, m;     while (l < r)     {         m = (l + r) / 2;         if (m * m <= x)            l = m + 1;         else            r = m;     }     return r - 1;}int main(){    int n,m,q;    int i,j,x,y,r,w,l,h,color;    int tmp1,tmp2,tmp3;    int ans[10];    while(scanf("%d%d%d",&n,&m,&q)!=EOF)    {        for(i=0;i<n;i++)            for(j=0;j<=m;j++)                next[i][j]=j;        memset(ans,0,sizeof(ans));        for(i=0;i<q;i++)        {            scanf("%s%d%d%d%d",str[i],&xx[i],&yy[i],&tmp1,&tmp2);            if(str[i][0]!='R')                p[i]=tmp1,c[i]=tmp2;            else            {                scanf("%d",&tmp3);                p[i]=tmp1;t[i]=tmp2;c[i]=tmp3;            }        }        for(i=q-1;i>=0;i--)        {            x=xx[i],y=yy[i];            if(str[i][0]=='D')            {                r=p[i],color=c[i];                for(j=MAX(0,x-r);j<=MIN(n-1,x+r);j++)                    ans[color]+=dele(j,MAX(0,y-r+abs(x-j)),MIN(m-1,y+r-abs(x-j)));            }            else if(str[i][0]=='T')            {                w=p[i],color=c[i];                h=(w+1)/2;                for(j=MAX(0,x);j<MIN(n,x+h);j++)                    ans[color]+=dele(j,MAX(0,y-h+j-x+1),MIN(m-1,y+h-j+x-1));            }            else if(str[i][0]=='R')            {                l=p[i],w=t[i],color=c[i];                for(j=MAX(0,x);j<=MIN(n-1,x+l-1);j++)                    ans[color]+=dele(j,y,MIN(m-1,y+w-1));            }            else if(str[i][0]=='C')            {                r=p[i];color=c[i];                for(j=MAX(0,x-r);j<=MIN(n-1,x+r);j++)                {                    int tmp=isqrt(1LL*r*r-(j-x)*(j-x));                    ans[color]+=dele(j,MAX(0,y-tmp),MIN(m-1,y+tmp));                }            }        }        for(i=1;i<=9;i++)            printf("%d%c",ans[i],(i==9)?'\n':' ');    }    return 0;}


 

原创粉丝点击