Codeforces Round #367 (Div. 2)E
2016-08-12 17:02
363 查看
题意:
给定一个矩阵,每次操作给出两个子矩阵参数,交换这两个子矩阵,
子矩阵满足:
不相交,没有公共边
要求最后输出这个矩阵
题解:构建四向链表,存每个点上面下面左边右边的点,每次修改就先通过链表找出每个子矩阵的左上角,然后
四条边去瞎搞一通O(N)
给定一个矩阵,每次操作给出两个子矩阵参数,交换这两个子矩阵,
子矩阵满足:
不相交,没有公共边
要求最后输出这个矩阵
题解:构建四向链表,存每个点上面下面左边右边的点,每次修改就先通过链表找出每个子矩阵的左上角,然后
四条边去瞎搞一通O(N)
#include<iostream> #include<cstdio> #include<queue> #include<vector> #include<bitset> #include<algorithm> #include<cstring> #include<map> #include<stack> #include<set> #include<cmath> using namespace std; const int maxn = 1010; int n,m,q,num[maxn][maxn],x,y,pos[maxn][maxn], le[maxn*maxn + 4*maxn],ri[maxn*maxn + 4*maxn], up[maxn*maxn + 4*maxn],dw[maxn*maxn + 4*maxn]; int getpos(int a,int b) { int now = pos[a][0]; for (int i = 1; i <= b; i++) now = ri[now]; return now; } int Change(int now) { x = now/m + 1; if (now % m == 0) --x; y = now%m; if (!y) y = m; } int main() { #ifdef DMC freopen("DMC.txt","r",stdin); #endif cin >> n >> m >> q; int tot = 0; for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) scanf("%d",&num[i][j]),pos[i][j] = ++tot; for (int i = 1; i <= n; i++) { pos[i][0] = ++tot; pos[i][m+1] = ++tot; for (int j = 1; j <= m; j++) { le[pos[i][j]] = pos[i][j-1]; ri[pos[i][j]] = pos[i][j+1]; up[pos[i][j]] = pos[i-1][j]; dw[pos[i][j]] = pos[i+1][j]; } ri[pos[i][0]] = pos[i][1]; le[pos[i][m+1]] = pos[i][m]; } for (int i = 1; i <= m; i++) { pos[0][i] = ++tot; pos[n+1][i] = ++tot; dw[pos[0][i]] = pos[1][i]; up[pos[1][i]] = pos[0][i]; up[pos[n+1][i]] = pos [i]; dw[pos [i]] = pos[n+1][i]; } while (q--) { int A1,B1,A2,B2,h,w; scanf("%d%d%d%d%d%d",&A1,&B1,&A2,&B2,&h,&w); int posA = getpos(A1,B1); int posB = getpos(A2,B2); for (int i = A1,j = A2; i < A1 + h; i++,j++) { swap(ri[le[posA]],ri[le[posB]]); swap(le[posA],le[posB]); posA = dw[posA]; posB = dw[posB]; } posA = up[posA]; posB = up[posB]; for (int i = B1,j = B2; i < B1 + w; i++,j++) { swap(up[dw[posA]],up[dw[posB]]); swap(dw[posA],dw[posB]); posA = ri[posA]; posB = ri[posB]; } posA = le[posA]; posB = le[posB]; for (int i = A1 + h - 1,j = A2 + h - 1; i >= A1; i--,j--) { swap(le[ri[posA]],le[ri[posB]]); swap(ri[posA],ri[posB]); posA = up[posA]; posB = up[posB]; } posA = dw[posA]; posB = dw[posB]; for (int i = B1 + w - 1, j = B2 + w - 1; i >= B1; i--,j--) { swap(dw[up[posA]],dw[up[posB]]); swap(up[posA],up[posB]); posA = le[posA]; posB = le[posB]; } } 4000 for (int i = 1; i <= n; i++) { int now = pos[i][0]; for (int j = 1; j <= m; j++) { now = ri[now]; Change(now); printf("%d ",num[x][y]); } printf("\n"); } return 0; }
相关文章推荐
- Codeforces Round #367 (Div. 2) E. Working routine(十字链表)
- Codeforces Round #367 (Div. 2) E 十字链表
- Codeforces Round #367 (Div. 2) C. Hard problem (dp)
- Codeforces Round #367 (Div. 2) B. Interesting drink(二分)
- Codeforces Round #367 (Div. 2) B Interesting drink【二分】
- 【Codeforces Round 367 (Div 2) A】【水题】Beru-taxi
- Codeforces Round #367 (Div. 2) 字典树-xor
- 【Codeforces Round 367 (Div 2) B】【二分查找】Interesting drink
- Codeforces Round #367 (Div. 2) [D] Vasiliy's Multiset(01字典树模板)
- 【Codeforces Round 367 (Div 2) E】Working routine 【十字链表 边框维护】 ★
- 【Codeforces Round 367 (Div 2) C】【简单DP】Hard problem
- 【Codeforces Round 367 (Div 2) D】【字典树典型题】Vasiliy's Multiset
- Codeforces Round #367 (Div. 2) E. Working routine (十字链表)
- Codeforces Round #367 (Div. 2) D Vasiliy's Multiset(01Trie)
- Codeforces Round #367 (Div. 2)-Vasiliy's Multiset-最大异或和-trie
- 【Codeforces Round 367 (Div 2) E】【十字链表 边框维护】Working routine nm矩形交换q次子矩形的最终矩形
- Codeforces Round #367 (Div. 2)-C - Hard problem-DP
- Codeforces Round #367 (Div. 2)C. Hard problem
- Codeforces Round #367 (Div. 2) B. Interesting drink 树状数组
- Educational Codeforces Round 33 (Rated for Div. 2)