Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdio>
- #include <algorithm>
- #define N 1111
- using namespace std;
- struct ceil{
- int v1,v2,h1,h2,x;
- } t[N*N];
- int n,m,k;
- inline void init(){
- freopen("input.txt","r",stdin);
- freopen("output.txt","w",stdout);
- scanf("%d%d",&n,&m);
- for (int i=2;i<=m+1;i++) t[i]=(ceil){-1,i+m+2,i-1,i+1,-1};
- t[1]=(ceil){-1,m+3,-1,2,-1};
- t[m+2]=(ceil){-1,2*m+4,m+1,-1,-1};
- for (int i=(n+1)*(m+2)+2;i<=(n+2)*(m+2)-1;i++) t[i]=(ceil){i-m-2,-1,i-1,i+1,-1};
- t[(n+1)*(m+2)+1]=(ceil){n*(m+2)+1,-1,-1,(n+1)*(m+2)+2,-1};
- t[(n+2)*(m+2)]=(ceil){(n+1)*(m+2),-1,(n+2)*(m+2)-1,-1,-1};
- for (int i=m+3;i<=n*(m+2)+1;i+=m+2) t[i]=(ceil){i-m-2,i+m+2,-1,i+1,-1};
- for (int i=2*m+4;i<=(n+1)*(m+2);i+=m+2) t[i]=(ceil){i-m-2,i+m+2,i-1,-1,-1};
- int x,num;
- for (int i=1;i<=n;i++)
- for (int j=1;j<=m;j++){
- scanf("%d",&x);
- num=i*(m+2)+j+1;
- t[num]=(ceil){num-m-2,num+m+2,num-1,num+1,x};
- }
- scanf("%d",&k);
- }
- inline void get(int x1,int y1,int x2,int y2,int &c1,int &c2,int &c3,int &c4,int &l1,int &l2,int &r3,int &r4,int &u1,int &u3,int &d2,int &d4){
- int p,q;
- p=y1+1;c1=t[p].v2;
- for (int i=1;i<x1;i++){
- q=t[c1].v1;
- if (q==p) q=t[c1].v2;
- p=c1;c1=q;
- }
- u1=p;
- p=x1*(m+2)+1;c1=t[p].h2;
- for (int i=1;i<y1;i++){
- q=t[c1].h1;
- if (q==p) q=t[c1].h2;
- p=c1;c1=q;
- }
- l1=p;
- p=(n+1)*(m+2)+y1+1;c2=t[p].v1;
- for (int i=n;i>x2;i--){
- q=t[c2].v1;
- if (q==p) q=t[c2].v2;
- p=c2;c2=q;
- }
- d2=p;
- p=x2*(m+2)+1;c2=t[p].h2;
- for (int i=1;i<y1;i++){
- q=t[c2].h1;
- if (q==p) q=t[c2].h2;
- p=c2;c2=q;
- }
- l2=p;
- p=y2+1;c3=t[p].v2;
- for (int i=1;i<x1;i++){
- q=t[c3].v1;
- if (q==p) q=t[c3].v2;
- p=c3;c3=q;
- }
- u3=p;
- p=(x1+1)*(m+2);c3=t[p].h1;
- for (int i=m;i>y2;i--){
- q=t[c3].h1;
- if (q==p) q=t[c3].h2;
- p=c3;c3=q;
- }
- r3=p;
- p=(n+1)*(m+2)+y2+1;c4=t[p].v1;
- for (int i=n;i>x2;i--){
- q=t[c4].v1;
- if (q==p) q=t[c4].v2;
- p=c4;c4=q;
- }
- d4=p;
- p=(x2+1)*(m+2);c4=t[p].h1;
- for (int i=m;i>y2;i--){
- q=t[c4].h1;
- if (q==p) q=t[c4].h2;
- p=c4;c4=q;
- }
- r4=p;
- }
- inline void vert(int x1,int y1,int x2,int y2){
- int c1,c2,c3,c4,l1,l2,r3,r4,u1,u3,d2,d4,a1,a2,b1,b2,p1,p2;
- get(x1,y1,x2,y2,c1,c2,c3,c4,l1,l2,r3,r4,u1,u3,d2,d4);
- a1=l1;a2=c1;b1=l2;b2=c2;p1=u1;p2=d2;
- for (int i=1;i<=(x2-x1+1)/2;i++){
- if (t[a2].h1==a1){
- if (t[b2].h1==b1) swap(t[a2].h1,t[b2].h1);
- else swap(t[a2].h1,t[b2].h2);
- }else{
- if (t[b2].h1==b1) swap(t[a2].h2,t[b2].h1);
- else swap(t[a2].h2,t[b2].h2);
- }
- if (t[a1].h1==a2){
- if (t[b1].h1==b2) swap(t[a1].h1,t[b1].h1);
- else swap(t[a1].h1,t[b1].h2);
- }else{
- if (t[b1].h1==b2) swap(t[a1].h2,t[b1].h1);
- else swap(t[a1].h2,t[b1].h2);
- }
- int q=t[a2].v1;
- if (q==p1) q=t[a2].v2;
- p1=a2;a2=q;
- q=t[b2].v1;
- if (q==p2) q=t[b2].v2;
- p2=b2;b2=q;
- q=t[a1].v1;
- if (t[q].h1!=a2 && t[q].h2!=a2) q=t[a1].v2;
- a1=q;
- q=t[b1].v1;
- if (t[q].h1!=b2 && t[q].h2!=b2) q=t[b1].v2;
- b1=q;
- }
- a1=r3;a2=c3;b1=r4;b2=c4;p1=u3;p2=d4;
- for (int i=1;i<=(x2-x1+1)/2;i++){
- if (t[a2].h1==a1){
- if (t[b2].h1==b1) swap(t[a2].h1,t[b2].h1);
- else swap(t[a2].h1,t[b2].h2);
- }else{
- if (t[b2].h1==b1) swap(t[a2].h2,t[b2].h1);
- else swap(t[a2].h2,t[b2].h2);
- }
- if (t[a1].h1==a2){
- if (t[b1].h1==b2) swap(t[a1].h1,t[b1].h1);
- else swap(t[a1].h1,t[b1].h2);
- }else{
- if (t[b1].h1==b2) swap(t[a1].h2,t[b1].h1);
- else swap(t[a1].h2,t[b1].h2);
- }
- int q=t[a2].v1;
- if (q==p1) q=t[a2].v2;
- p1=a2;a2=q;
- q=t[b2].v1;
- if (q==p2) q=t[b2].v2;
- p2=b2;b2=q;
- q=t[a1].v1;
- if (t[q].h1!=a2 && t[q].h2!=a2) q=t[a1].v2;
- a1=q;
- q=t[b1].v1;
- if (t[q].h1!=b2 && t[q].h2!=b2) q=t[b1].v2;
- b1=q;
- }
- a1=u1;a2=c1;
- b1=d2;b2=c2;
- p1=l2;p2=l1;
- for (int i=y1;i<=y2;i++){
- if (t[a2].v1==a1){
- if (t[b2].v1==b1) swap(t[a2].v1,t[b2].v1);
- else swap(t[a2].v1,t[b2].v2);
- }else{
- if (t[b2].v1==b1) swap(t[a2].v2,t[b2].v1);
- else swap(t[a2].v2,t[b2].v2);
- }
- if (t[a1].v1==a2){
- if (t[b1].v1==b2) swap(t[a1].v1,t[b1].v1);
- else swap(t[a1].v1,t[b1].v2);
- }else{
- if (t[b1].v1==b2) swap(t[a1].v2,t[b1].v1);
- else swap(t[a1].v2,t[b1].v2);
- }
- int q=t[a2].h1;
- if (q==p1) q=t[a2].h2;
- p1=a2;a2=q;
- q=t[b2].h1;
- if (q==p2) q=t[b2].h2;
- p2=b2;b2=q;
- q=t[a1].h1;
- if (t[q].v1!=a2 && t[q].v2!=a2) q=t[a1].h2;
- a1=q;
- q=t[b1].h1;
- if (t[q].v1!=b2 && t[q].v2!=b2) q=t[b1].h2;
- b1=q;
- }
- }
- inline void horiz(int x1,int y1,int x2,int y2){
- int c1,c2,c3,c4,l1,l2,r3,r4,u1,u3,d2,d4,a1,a2,b1,b2,p1,p2;
- get(x1,y1,x2,y2,c1,c2,c3,c4,l1,l2,r3,r4,u1,u3,d2,d4);
- a1=u1;a2=c1;b1=u3;b2=c3;p1=l1;p2=r3;
- for (int i=1;i<=(y2-y1+1)/2;i++){
- if (t[a2].v1==a1){
- if (t[b2].v1==b1) swap(t[a2].v1,t[b2].v1);
- else swap(t[a2].v1,t[b2].v2);
- }else{
- if (t[b2].v1==b1) swap(t[a2].v2,t[b2].v1);
- else swap(t[a2].v2,t[b2].v2);
- }
- if (t[a1].v1==a2){
- if (t[b1].v1==b2) swap(t[a1].v1,t[b1].v1);
- else swap(t[a1].v1,t[b1].v2);
- }else{
- if (t[b1].v1==b2) swap(t[a1].v2,t[b1].v1);
- else swap(t[a1].v2,t[b1].v2);
- }
- int q=t[a2].h1;
- if (q==p1) q=t[a2].h2;
- p1=a2;a2=q;
- q=t[b2].h1;
- if (q==p2) q=t[b2].h2;
- p2=b2;b2=q;
- q=t[a1].h1;
- if (t[q].v1!=a2 && t[q].v2!=a2) q=t[a1].h2;
- a1=q;
- q=t[b1].h1;
- if (t[q].v1!=b2 && t[q].v2!=b2) q=t[b1].h2;
- b1=q;
- }
- a1=d2;a2=c2;b1=d4;b2=c4;p1=l2;p2=r4;
- for (int i=1;i<=(y2-y1+1)/2;i++){
- if (t[a2].v1==a1){
- if (t[b2].v1==b1) swap(t[a2].v1,t[b2].v1);
- else swap(t[a2].v1,t[b2].v2);
- }else{
- if (t[b2].v1==b1) swap(t[a2].v2,t[b2].v1);
- else swap(t[a2].v2,t[b2].v2);
- }
- if (t[a1].v1==a2){
- if (t[b1].v1==b2) swap(t[a1].v1,t[b1].v1);
- else swap(t[a1].v1,t[b1].v2);
- }else{
- if (t[b1].h1==b2) swap(t[a1].v2,t[b1].v1);
- else swap(t[a1].v2,t[b1].v2);
- }
- int q=t[a2].h1;
- if (q==p1) q=t[a2].h2;
- p1=a2;a2=q;
- q=t[b2].h1;
- if (q==p2) q=t[b2].h2;
- p2=b2;b2=q;
- q=t[a1].h1;
- if (t[q].v1!=a2 && t[q].v2!=a2) q=t[a1].h2;
- a1=q;
- q=t[b1].h1;
- if (t[q].v1!=b2 && t[q].v2!=b2) q=t[b1].h2;
- b1=q;
- }
- a1=l1;a2=c1;b1=r3;b2=c3;p1=u3;p2=u1;
- for (int i=x1;i<=x2;i++){
- if (t[a2].h1==a1){
- if (t[b2].h1==b1) swap(t[a2].h1,t[b2].h1);
- else swap(t[a2].h1,t[b2].h2);
- }else{
- if (t[b2].h1==b1) swap(t[a2].h2,t[b2].h1);
- else swap(t[a2].h2,t[b2].h2);
- }
- if (t[a1].h1==a2){
- if (t[b1].h1==b2) swap(t[a1].h1,t[b1].h1);
- else swap(t[a1].h1,t[b1].h2);
- }else{
- if (t[b1].h1==b2) swap(t[a1].h2,t[b1].h1);
- else swap(t[a1].h2,t[b1].h2);
- }
- int q=t[a2].v1;
- if (q==p1) q=t[a2].v2;
- p1=a2;a2=q;
- q=t[b2].v1;
- if (q==p2) q=t[b2].v2;
- p2=b2;b2=q;
- q=t[a1].v1;
- if (t[q].h1!=a2 && t[q].h2!=a2) q=t[a1].v2;
- a1=q;
- q=t[b1].v1;
- if (t[q].h1!=b2 && t[q].h2!=b2) q=t[b1].v2;
- b1=q;
- }
- }
- void print(){
- for (int i=1;i<=n;i++){
- int p=(m+2)*i+1,q=t[p].h2;
- for (int j=1;j<=m;j++){
- printf("%d",t[q].x);
- if (j!=m) printf(" ");
- if (t[q].h1==p){
- p=q;q=t[q].h2;
- }else{
- p=q;q=t[q].h1;
- }
- }
- if (i!=n) printf("\n");
- }
- }
- int main(){
- char c;
- int x1,x2,y1,y2;
- init();
- while (k--){
- scanf("\n%c %d %d %d %d",&c,&x1,&y1,&x2,&y2);
- if (c=='V') vert(x1,y1,x2,y2);
- else horiz(x1,y1,x2,y2);
- }
- print();
- /* for (int i=1;i<=(n+2)*(m+2);i++){ printf("{%d %d %d %d %d %d}, ",i,t[i].v1,t[i].v2,t[i].h1,t[i].h2,t[i].x);
- if (!(i%(m+2))) printf("\n");
- } */
- }
Advertisement
Add Comment
Please, Sign In to add comment