ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1028. Stars

Help this WR!
Posted by daizi sheng(from USTC) 10 Jun 2002 16:38
#include<stdio.h>
int sum[32001];
int pre[32001];
int lnk[32001];
int end;
int tmp_sum[32001];
int tmp_pre[32001];
int tmp_end;
int n,m;
int count[15000];
int get(int i){
    while(i>=0){
        if(i==pre[i])return sum[i];
        i=pre[i];}
    return 0;}

int main(void){
    int i,j,x,y,curx,cury,left,k,prej,preend;
    scanf("%d",&n);
        m=n;
    /*init*/
    pre[0]=-1;
    for(i=1;i<=500;i++)
        pre[i]=0;
    for(i=500;i<32000;i+=500)
        for(j=i+1;j<=i+500;j++)
            pre[j]=i;
    end=-1;
    for(i=0;i<n;i++)
        count[i]=0;
    /*end of init*/
    scanf("%d %d",&x,&y);
        n--;
    while(1){
        curx=-1;
        left=0;
        cury=y;
        tmp_sum[x]=get(x)+left+1;
        count[tmp_sum[x]-1]++;
        left++;
        tmp_pre[x]=curx;
        curx=x;
        while(1){
            if(!n)goto end;
            scanf("%d %d",&x,&y);
                        n--;
                        if(y!=cury)break;
            tmp_sum[x]=get(x)+left+1;
                        count[tmp_sum[x]-1]++;
            left++;
            tmp_pre[x]=curx;
            curx=x;
            }
        tmp_end=curx;
        i=tmp_end;
        j=end;
                prej=end;
                preend=end;
              while(i>=0){
            while(j>i){
                                sum[j]=tmp_sum[i]-get(i)+get(j);
                                prej=j;
                j=lnk[j];}
            if(j==i){
                sum[i]=tmp_sum[i];
                pre[i]=i;
                                prej=j;
                j=lnk[j];}
            else{
                for(k=i+1;pre[k]==pre[i]
&&k<32001;k++)
                    pre[k]=i;
                pre[i]=i;
                                sum[i]=tmp_sum[i];
                                if(prej==preend){
                                        end=i;
                                        lnk[i]=prej;
                                        }
                                else{
                                        lnk[prej]=i;
                                        lnk[i]=j;}
                                prej=i;}
            i=tmp_pre[i];
            }
        }
    end:
    for(i=0;i<m;i++)
        printf("%d\n",count[i]);
    return 0;}