裁剪表格
题目背景:
分析:链表······
当看到题目中的高级数据结构就开始胡思乱想的一定不止我一个人,然而这道题被暴力操的一干二净,然而给平衡树的点暴力秒秒钟卡过,直接就AC了······我也是······(论出题人崩溃的内心)
首先对于30%的数据,很显然的直接swap就好,但是你会发现其实60%的数据,其实暴力也可以很方便的卡过去······(然而加一个register连100%的数据都卡过了······)
Source:
/*
created by scarlyw
*/
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <cctype>
#include <set>
#include <map>
#include <vector>
#include <queue>
inline char read() {
static const int IN_LEN = 1024 * 1024;
static char buf[IN_LEN], *s, *t;
if (s == t) {
t = (s = buf) + fread(buf, 1, IN_LEN, stdin);
if (s == t) return -1;
}
return *s++;
}
/*
template<class T>
inline void R(T &x) {
static bool iosig;
static char c;
for (iosig = false, c = read(); !isdigit(c); c = read()) {
if (c == -1) return ;
if (c == '-') iosig = true;
}
for (x = 0; isdigit(c); c = read()) x = ((x << 2) + x << 1) + (c ^ '0');
if (iosig) x = -x;
}
*/
const int OUT_LEN = 1024 * 1024;
char obuf[OUT_LEN], *oh = obuf;
inline void write_char(char c) {
if (oh == obuf + OUT_LEN) fwrite(obuf, 1, OUT_LEN, stdout), oh = obuf;
*oh++ = c;
}
template<class T>
inline void W(T x) {
static int buf[30], cnt;
if (x == 0) write_char('0');
else {
if (x < 0) write_char('-'), x = -x;
for (cnt = 0; x; x /= 10) buf[++cnt] = x % 10 + 48;
while (cnt) write_char(buf[cnt--]);
}
}
inline void flush() {
fwrite(obuf, 1, oh - obuf, stdout);
}
///*
template<class T>
inline void R(T &x) {
static bool iosig;
static char c;
for (iosig = false, c = getchar(); !isdigit(c); c = getchar()) {
if (c == -1) return ;
if (c == '-') iosig = true;
}
for (x = 0; isdigit(c); c = getchar()) x = ((x << 2) + x << 1) + (c ^ '0');
if (iosig) x = -x;
}
//*/
const int MAXN = 1000 + 10;
int n, m, q;
int table[MAXN][MAXN];
inline void read_in() {
R(n), R(m), R(q);
for (register int i = 1; i <= n; ++i)
for (register int j = 1; j <= m; ++j)
R(table[i][j]);
}
inline void solve() {
register int x1, y1, x2, y2, h, w;
while (q--) {
R(x1), R(y1), R(x2), R(y2), R(h), R(w);
for (register int i = 0; i < h; ++i)
for (register int j = 0; j < w; ++j)
std::swap(table[x1 + i][y1 + j], table[x2 + i][y2 + j]);
}
for (register int i = 1; i <= n; ++i) {
for (register int j = 1; j <= m; ++j)
W(table[i][j]), write_char(' ');
write_char('\n');
}
}
int main() {
// freopen("table.in", "r", stdin);
// freopen("table.out", "w", stdout);
read_in();
solve();
flush();
return 0;
}
但是出题人表示,本来60%的档是给平衡树的,你对于每一行开一颗平衡树,然后暴力提取区间再接到对应位置即可。
我们来讲讲真正的正解,正解是链表······4向链表,往四个方向都维护一下就好,但是实际操作上可以直接只维护两个防线即可,对于每一次操作相当于断掉当前两个矩阵周围一圈向外面的指针,然后依次交换就好,因为矩阵的周长之和和找到对应位置所需要的次数之和是O(n)的所以整体复杂度就是O(nq)的,详见代码
Source:
/*
created by scarlyw
*/
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <cctype>
#include <set>
#include <map>
#include <vector>
#include <queue>
#include <ctime>
inline char read() {
static const int IN_LEN = 1024 * 1024;
static char buf[IN_LEN], *s, *t;
if (s == t) {
t = (s = buf) + fread(buf, 1, IN_LEN, stdin);
if (s == t) return -1;
}
return *s++;
}
///*
template<class T>
inline void R(T &x) {
static bool iosig;
static char c;
for (iosig = false, c = read(); !isdigit(c); c = read()) {
if (c == -1) return ;
if (c == '-') iosig = true;
}
for (x = 0; isdigit(c); c = read()) x = ((x << 2) + x << 1) + (c ^ '0');
if (iosig) x = -x;
}
//*/
const int OUT_LEN = 1024 * 1024;
char obuf[OUT_LEN], *oh = obuf;
inline void write_char(char c) {
if (oh == obuf + OUT_LEN) fwrite(obuf, 1, OUT_LEN, stdout), oh = obuf;
*oh++ = c;
}
template<class T>
inline void W(T x) {
static int buf[30], cnt;
if (x == 0) write_char('0');
else {
if (x < 0) write_char('-'), x = -x;
for (cnt = 0; x; x /= 10) buf[++cnt] = x % 10 + 48;
while (cnt) write_char(buf[cnt--]);
}
}
inline void flush() {
fwrite(obuf, 1, oh - obuf, stdout);
}
/*
template<class T>
inline void R(T &x) {
static bool iosig;
static char c;
for (iosig = false, c = getchar(); !isdigit(c); c = getchar()) {
if (c == -1) return ;
if (c == '-') iosig = true;
}
for (x = 0; isdigit(c); c = getchar()) x = ((x << 2) + x << 1) + (c ^ '0');
if (iosig) x = -x;
}
//*/
const int MAXX = 1000000 + 50000;
const int MAXN = 1000 + 10;
int n ,m ,q, ind;
int num[MAXN][MAXN], f[MAXX][2], v[MAXX];
inline void read_in() {
R(n), R(m), R(q);
for (register int i = 1; i <= n; ++i)
for (register int j = 1; j <= m; ++j)
R(v[num[i][j] = ++ind]);
for (register int i = 0; i <= m + 1; ++i) num[0][i] = ++ind;
for (register int i = 0; i <= m + 1; ++i) num[n + 1][i] = ++ind;
for (register int i = 1; i <= n; ++i) num[i][0] = ++ind;
for (register int i = 1; i <= n; ++i) num[i][m + 1] = ++ind;
for (register int i = 0; i <= n; ++i)
for (register int j = 0; j <= m; ++j)
f[num[i][j]][0] = num[i][j + 1], f[num[i][j]][1] = num[i + 1][j];
}
inline int get_num(int x, int y) {
int pos = num[0][0];
for (register int i = 1; i <= x; ++i) pos = f[pos][1];
for (register int i = 1; i <= y; ++i) pos = f[pos][0];
return pos;
}
inline void solve() {
register int x1, y1, x2, y2, h, w;
while (q--) {
R(x1), R(y1), R(x2), R(y2), R(h), R(w);
int pos1 = get_num(x1 - 1, y1 - 1), pos2 = get_num(x2 - 1, y2 - 1);
register int i, temp1, temp2;
for (i = 1, temp1 = pos1, temp2 = pos2; i <= w; ++i)
std::swap(f[temp1 = f[temp1][0]][1], f[temp2 = f[temp2][0]][1]);
for (i = 1; i <= h; ++i)
std::swap(f[temp1 = f[temp1][1]][0], f[temp2 = f[temp2][1]][0]);
for (i = 1, temp1 = pos1, temp2 = pos2; i <= h; ++i)
std::swap(f[temp1 = f[temp1][1]][0], f[temp2 = f[temp2][1]][0]);
for (i = 1; i <= w; ++i)
std::swap(f[temp1 = f[temp1][0]][1], f[temp2 = f[temp2][0]][1]);
}
for (register int i = 1, pos = num[0][0]; i <= n; ++i, write_char('\n'))
for (register int j = 1, temp = pos = f[pos][1]; j <= m; ++j)
W(v[temp = f[temp][0]]), write_char(' ');
}
int main() {
read_in();
solve();
flush();
return 0;
}