#include "message.h" #include "dzialka.h" #include <cstdio> //#include <cassert> //#define DEBUG 1 /* * Zadanie: dzi * Autor rozwiązania: Jan Horodecki */ static int stosW[75010]; static int stosR[75010]; static int stosSize=0; int empty(){ return stosSize==0?1:0; } int topW(){ //#ifdef DEBUG //assert(!empty()); //#endif return stosW[stosSize-1]; } int topR(){ //#ifdef DEBUG //assert(!empty()); //#endif return stosR[stosSize-1]; } void put(int W, int R){ stosW[stosSize]=W; stosR[stosSize]=R; stosSize++; } void pop(){ //#ifdef DEBUG //assert(!empty()); //#endif stosSize--; } int max(int a ,int b){ return a>b?a:b; } int main(){ int H=GetFieldHeight(),W=GetFieldWidth(),N=NumberOfNodes(),Id=MyNodeId(); //IsUsableCell(int r, int c); int yp=(H*Id)/N, yk=(H*(Id+1))/N, xp=(W*Id)/N, xk=(W*(Id+1))/N; int left,curr,buf,target,targetX; int *pion=new int[H]; int *nast_pion=new int[H]; int *pom; int tmpc=0; while(W*tmpc/N==W*(tmpc+1)/N)tmpc++; //#ifdef DEBUG //assert(pion!=NULL); //if(Id==0)for(int i=0;i<N;i++){ // printf("id=%d xp=%d xk=%d\n",i,W*i/N,W*(i+1)/N); //} //#endif for(int i=0;i<H;i++)pion[i]=0; for(int i=yp;i<yk;i++){ //printf("1myid=%d i=%d yp=%d yk=%d\n",Id,i,yp,yk); left=0,target=1; while(W*target/N==W*(target+1)/N ||W*target/N==0)target++; targetX=W*target/N; //printf("myid=%d tar=%d tarX=%d\n",Id,target,targetX); for(int j=0;j<W;j++){ buf=IsUsableCell(i,j); if(buf!=0)curr=left+1; else curr=0; if(j==targetX && j!=0){ if(target!=Id){ PutInt(target,curr); //printf("putInt myid=%d i=%d target=%d curr=%d\n",Id,i,target,curr); }else{ pion[i]=curr; //printf("local save myid=%d i=%d j=%d target=%d curr=%d\n",Id,i,j,target,curr); } target++; while(W*target/N==W*(target+1)/N)target++; //printf("nexttarget myid=%d i=%d j=%d target=%d\n",Id,i,j,target); if(target>=N){ break; } targetX=W*target/N; } left=curr; } } for(int i=tmpc+1;i<N;i++){ if(i!=Id)Send(i); } if(Id>tmpc){ for(int i=0;i<N;i++){ if(i!=Id){ Receive(i); if(W*Id/N != W*(Id+1)/N){ int recyp=H*i/N,recyk=H*(i+1)/N; for(int j=recyp;j<recyk;j++){ pion[j]=GetInt(i); } } } } }else{ if(Id==tmpc) for(int i=0;i<H;i++){ pion[i]=IsUsableCell(i,0); } } //fprintf(stderr,"\nId -> %d\n",Id); long long Wyn=0; int actw=0,currW,currR,nastW; for(int i=xp;i<xk;i++){ actw=0; for(int j=0;j<H;j++){ if(pion[j]>actw){ //fprintf(stderr,"id=%d new W=%d,R=%d\n",Id,pion[j],j); put(pion[j],j); actw=pion[j]; } if(actw>pion[j]){ while(!empty() && topW()>pion[j]){ currW=topW(); currR=topR(); pop(); nastW=(empty()?pion[j]:max(topW(),pion[j])); //wysokość - od 1 do j-currR ; szer od nastW+1 do currW; //fprintf(stderr,"id=%d lg=(%d,%d) pd=(%d,%d) nastW=%d wyn+=%lld\n",Id,i-currW+1,currR,i,j-1,nastW,((long long)(1+j-currR))*((long long)(j-currR))/2LL*((long long)(currW-nastW))); Wyn+= ((long long)(1+j-currR))*((long long)(j-currR))/2LL*((long long)(currW-nastW)); } if((empty() || topW()<pion[j]) && pion[j]!=0){ //fprintf(stderr,"id=%d new W=%d,R=%d\n",Id,pion[j],j); put(pion[j],currR); } actw=pion[j]; } //if(i==xp)fprintf(stderr,"%d [%d]=> %d\n",Id,j,pion[j]); } while(!empty()){ currW=topW(); currR=topR(); pop(); nastW=(empty()?0:topW()); //wysokość - od 1 do j-currR ; szer od nastW+1 do currW; //fprintf(stderr,"id=%d lg=(%d,%d) pd=(%d,%d) nastW=%d wyn+=%lld\n",Id,i-currW+1,currR,i,H-1,nastW,((long long)(1+H-currR))*((long long)(H-currR))/2LL*((long long)(currW-nastW))); Wyn+= ((long long)(1+H-currR))*((long long)(H-currR))/2LL*((long long)(currW-nastW)); } if(i!=W-1){ for(int j=0;j<H;j++){ buf=IsUsableCell(j,i+1); if(buf!=0)nast_pion[j]=pion[j]+1; else nast_pion[j]=0; } pom=pion; pion=nast_pion; nast_pion=pom; } } if(Id>0){ PutLL(0,Wyn); Send(0); //if(Wyn!=0)fprintf(stderr,"%d > %d\n",Id,Wyn); }else{ unsigned long long total_result=Wyn; for(int i=1;i<N;i++){ Receive(i); Wyn=GetLL(i); total_result+=Wyn; } printf("%llu",total_result); } return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 | #include "message.h" #include "dzialka.h" #include <cstdio> //#include <cassert> //#define DEBUG 1 /* * Zadanie: dzi * Autor rozwiązania: Jan Horodecki */ static int stosW[75010]; static int stosR[75010]; static int stosSize=0; int empty(){ return stosSize==0?1:0; } int topW(){ //#ifdef DEBUG //assert(!empty()); //#endif return stosW[stosSize-1]; } int topR(){ //#ifdef DEBUG //assert(!empty()); //#endif return stosR[stosSize-1]; } void put(int W, int R){ stosW[stosSize]=W; stosR[stosSize]=R; stosSize++; } void pop(){ //#ifdef DEBUG //assert(!empty()); //#endif stosSize--; } int max(int a ,int b){ return a>b?a:b; } int main(){ int H=GetFieldHeight(),W=GetFieldWidth(),N=NumberOfNodes(),Id=MyNodeId(); //IsUsableCell(int r, int c); int yp=(H*Id)/N, yk=(H*(Id+1))/N, xp=(W*Id)/N, xk=(W*(Id+1))/N; int left,curr,buf,target,targetX; int *pion=new int[H]; int *nast_pion=new int[H]; int *pom; int tmpc=0; while(W*tmpc/N==W*(tmpc+1)/N)tmpc++; //#ifdef DEBUG //assert(pion!=NULL); //if(Id==0)for(int i=0;i<N;i++){ // printf("id=%d xp=%d xk=%d\n",i,W*i/N,W*(i+1)/N); //} //#endif for(int i=0;i<H;i++)pion[i]=0; for(int i=yp;i<yk;i++){ //printf("1myid=%d i=%d yp=%d yk=%d\n",Id,i,yp,yk); left=0,target=1; while(W*target/N==W*(target+1)/N ||W*target/N==0)target++; targetX=W*target/N; //printf("myid=%d tar=%d tarX=%d\n",Id,target,targetX); for(int j=0;j<W;j++){ buf=IsUsableCell(i,j); if(buf!=0)curr=left+1; else curr=0; if(j==targetX && j!=0){ if(target!=Id){ PutInt(target,curr); //printf("putInt myid=%d i=%d target=%d curr=%d\n",Id,i,target,curr); }else{ pion[i]=curr; //printf("local save myid=%d i=%d j=%d target=%d curr=%d\n",Id,i,j,target,curr); } target++; while(W*target/N==W*(target+1)/N)target++; //printf("nexttarget myid=%d i=%d j=%d target=%d\n",Id,i,j,target); if(target>=N){ break; } targetX=W*target/N; } left=curr; } } for(int i=tmpc+1;i<N;i++){ if(i!=Id)Send(i); } if(Id>tmpc){ for(int i=0;i<N;i++){ if(i!=Id){ Receive(i); if(W*Id/N != W*(Id+1)/N){ int recyp=H*i/N,recyk=H*(i+1)/N; for(int j=recyp;j<recyk;j++){ pion[j]=GetInt(i); } } } } }else{ if(Id==tmpc) for(int i=0;i<H;i++){ pion[i]=IsUsableCell(i,0); } } //fprintf(stderr,"\nId -> %d\n",Id); long long Wyn=0; int actw=0,currW,currR,nastW; for(int i=xp;i<xk;i++){ actw=0; for(int j=0;j<H;j++){ if(pion[j]>actw){ //fprintf(stderr,"id=%d new W=%d,R=%d\n",Id,pion[j],j); put(pion[j],j); actw=pion[j]; } if(actw>pion[j]){ while(!empty() && topW()>pion[j]){ currW=topW(); currR=topR(); pop(); nastW=(empty()?pion[j]:max(topW(),pion[j])); //wysokość - od 1 do j-currR ; szer od nastW+1 do currW; //fprintf(stderr,"id=%d lg=(%d,%d) pd=(%d,%d) nastW=%d wyn+=%lld\n",Id,i-currW+1,currR,i,j-1,nastW,((long long)(1+j-currR))*((long long)(j-currR))/2LL*((long long)(currW-nastW))); Wyn+= ((long long)(1+j-currR))*((long long)(j-currR))/2LL*((long long)(currW-nastW)); } if((empty() || topW()<pion[j]) && pion[j]!=0){ //fprintf(stderr,"id=%d new W=%d,R=%d\n",Id,pion[j],j); put(pion[j],currR); } actw=pion[j]; } //if(i==xp)fprintf(stderr,"%d [%d]=> %d\n",Id,j,pion[j]); } while(!empty()){ currW=topW(); currR=topR(); pop(); nastW=(empty()?0:topW()); //wysokość - od 1 do j-currR ; szer od nastW+1 do currW; //fprintf(stderr,"id=%d lg=(%d,%d) pd=(%d,%d) nastW=%d wyn+=%lld\n",Id,i-currW+1,currR,i,H-1,nastW,((long long)(1+H-currR))*((long long)(H-currR))/2LL*((long long)(currW-nastW))); Wyn+= ((long long)(1+H-currR))*((long long)(H-currR))/2LL*((long long)(currW-nastW)); } if(i!=W-1){ for(int j=0;j<H;j++){ buf=IsUsableCell(j,i+1); if(buf!=0)nast_pion[j]=pion[j]+1; else nast_pion[j]=0; } pom=pion; pion=nast_pion; nast_pion=pom; } } if(Id>0){ PutLL(0,Wyn); Send(0); //if(Wyn!=0)fprintf(stderr,"%d > %d\n",Id,Wyn); }else{ unsigned long long total_result=Wyn; for(int i=1;i<N;i++){ Receive(i); Wyn=GetLL(i); total_result+=Wyn; } printf("%llu",total_result); } return 0; } |