| 12
 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
 
 | #include<cstdio>#include<vector>
 #include<algorithm>
 using namespace std;
 const int N=100010,FSIZE=1<<20,fx[4][2]={{-1,0},{0,1},{1,0},{0,-1}};
 int n,len[N],sts;
 struct{
 int x;
 bool y;
 }st[N];
 struct point{int x,y;}ans[N];
 vector<int> a;
 bool s[N];
 struct sqr{
 int w,h,lx,ly,nx,ny,x,y;
 void operator+=(const sqr &b){
 if(!len[ny]) len[ny]=ly+b.lx-1;
 *this=sqr{w+b.w,
 max(h-y,b.h-b.x)+
 max(y,b.x),
 lx,b.ly,nx,b.ny,
 x+max(b.x-y,0),
 b.y+max(y-b.x,0)
 };
 }
 
 
 sqr operator^(bool b){
 
 if(!len[nx]) len[nx]=lx;
 if(!len[ny]) len[ny]=ly;
 
 return(sqr{h,
 w+2,
 b?h-x+1:x,
 b?y:h-y+1,
 nx-1,
 ny+1,
 b?1:w+2,
 b?w+2:1
 });
 }
 }d[N];
 char BuF[FSIZE],*InF=BuF;
 template<typename T>void read(T &x){
 for(;*InF<33;++InF);
 for(x=0;*InF>32;x=x*10+(*InF++^48));
 }
 sqr solve(int l,int r){
 if(l>r)
 return(sqr{0,1,0,0,0,0,0,0});
 for(d[0]=sqr();l<=r;++l){
 if(sts&&s[l]!=st[sts].y){
 if(!d[sts--].w){
 d[sts+1]={1,2,1,1,l-1,l+1,s[l]?1:2,s[l]?2:1};
 len[l]=1;
 }else
 d[sts+1]=d[sts+1]^s[l];
 if(!d[sts].w)
 d[sts]=d[sts+1];
 else
 d[sts]+=d[sts+1];
 d[sts+1]=sqr();
 }else{
 st[++sts]={l,s[l]};
 d[sts]=sqr();
 }
 }
 return(d[0]);
 }
 void calc(int m){
 ans[m]={0,0};
 for(int i=m,f=0;i<=n;++i){
 ans[i+1]={ans[i].x+fx[f][0]*len[i],ans[i].y+fx[f][1]*len[i]};
 if((s[i]?++f:--f)>3) f=0;
 else if(f<0) f=3;
 }
 }
 void work(){
 int L=0,R=0,m=1,p=0;
 for(int i=1;*InF>32;(s[i++]=*InF++=='P')?++R:++L,++n);
 if(L-R!=4){
 printf("NIE\n");
 return;
 }
 
 
 for(a.clear();s[m];s[++n]=s[m++]);
 
 
 for(int i=m,d=0;a.size()<4;++i)
 if(!d&&s[i]==s[m]) a.push_back(i);
 else s[i]==s[m]?++d:--d;
 
 
 len[a[0]]=(solve(a[0]+1,a[1]-1)^0).lx+N;
 len[a[1]]=(solve(a[1]+1,a[2]-1)^0).lx+N;
 len[a[2]]=(solve(a[2]+1,a[3]-1)^0).lx+N;
 len[a[3]]=(solve(a[3]+1,n     )^0).lx+N;
 
 
 calc(m);
 
 
 if(ans[n+1].x>0)
 len[a[0]]+=ans[n+1].x;
 else
 len[a[2]]-=ans[n+1].x;
 
 
 if(ans[n+1].y>0)
 len[a[1]]+=ans[n+1].y;
 else
 len[a[3]]-=ans[n+1].y;
 
 
 for(calc(m);m>1;ans[--m]=ans[n--]);
 for(int i=m+p;i<=n;++i) printf("%d %d\n",ans[i].x,ans[i].y);
 }
 int main(){
 fread(BuF,1,FSIZE,stdin);
 work();
 return(0);
 }
 
 
 |