跳到正文

个人记录

【国庆集训】模拟赛第一场题解

xiao49约 46 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年3月15日
篇幅
约 46 分钟

版本

  1. v3当前
  2. v2
  3. v1

入门组

T1 好的序列(A)

贪心从左往右找到第一个 11,然后接着找到第一个 22,第一个 33,以此类推,时间复杂度为 O(n)O(n)。

C++
#include <bits/stdc++.h>
using namespace std;
int main() {
    int n;
    cin >> n;
    int ans = 0;
    while (n--) {
        int x;
        scanf("%d", &x);
        if (ans + 1 == x)
            ans++;
    }
    cout << ans << endl;
}

T2 爬山(B)

对于前30%30\%的数据,暴力搜索以每一个平台为终点的最大长度再输出最大值即可。

考虑使用动态规划,dp[i][j]dp[i][j]表示走到第ii行第jj列的平台时能经过的最大长度。

注意到状态只能从低处到高处单向转移,可推出状态转移方程:

C++
	dp[i][j]=max(dp[i-1][j],dp[i+1][j],dp[i][j+1],dp[i][j-1])

加上只能从更低处转移的限制条件即可。

时间复杂度为O(mnlogmn)O(mnlog{mn})。

改用记忆化搜索可以使时间复杂度降为O(mn)O(mn)。

C++
#include<bits/stdc++.h>
using namespace std;
int ma=0;
int ind=0;
struct rua {
	int x;
	int y;
	int zhi;
} a[10000005];
bool cmp(rua a,rua b) {
	return a.zhi>b.zhi;
}
int main() {
	int r,c;
	cin>>r>>c;
    int dp[c+5][r+5];
    int mk[c+5][r+5];
    for(int i=0;i<r+5;i++){
        for(int j=0;j<c+5;j++){
            dp[j][i]=0;
            mk[j][i]=0;
        }
    }
	for(int i=1; i<=r; i++) {
		for(int j=1; j<=c; j++) {
			ind++;
			cin>>a[ind].zhi;
			a[ind].x=j;
			a[ind].y=i;
			mk[j][i]=a[ind].zhi;
		}
	}
	sort(a+1,a+r*c+1,cmp);
	for(int i=1; i<=r*c; i++) {
		if(mk[a[i].x+1][a[i].y]>a[i].zhi) dp[a[i].x][a[i].y]=max(dp[a[i].x+1][a[i].y],dp[a[i].x][a[i].y]);
		if(mk[a[i].x-1][a[i].y]>a[i].zhi) dp[a[i].x][a[i].y]=max(dp[a[i].x-1][a[i].y],dp[a[i].x][a[i].y]);
		if(mk[a[i].x][a[i].y+1]>a[i].zhi) dp[a[i].x][a[i].y]=max(dp[a[i].x][a[i].y+1],dp[a[i].x][a[i].y]);
		if(mk[a[i].x][a[i].y-1]>a[i].zhi) dp[a[i].x][a[i].y]=max(dp[a[i].x][a[i].y-1],dp[a[i].x][a[i].y]);
		dp[a[i].x][a[i].y]+=1;
		ma=max(ma,dp[a[i].x][a[i].y]);
	}
	cout<<ma;
	return 0;
}

T3 勇士god(C)

  • 30%: 对于每轮,每个人O(n)的查询离自己最远的未被淘汰的人,总复杂度O(n3)O(n^3)
  • 50%: 先排序,发现每次只会把票投给最两端的的人之一,只需找到每个人投哪个即可,比30%减少了O(n)O(n)查询
  • 100%: 会存在一个分界线,分界线左侧的人投给最最右边的,右侧反之。 那么每轮投票时,二分这个分界线,然后判断淘汰哪侧人即可
C++
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
#define in inline
const int _ = 1e6 + 23;
#define get getchar()

int read()
{
	int x = 1, t = 0;
	char ch = get;
	while ((ch < '0' || ch > '9') && ch != '-')
		ch = get;
	if (ch == '-')
		x = -1, ch = get;
	while (ch <= '9' && ch >= '0')
		t = t * 10 + ch - '0', ch = get;
	return t * x;
}

struct yzx
{
	int x, id;
} a[1000100];
int n;

in int cmp(yzx a, yzx b)
{
	return a.x < b.x;
}

in int check(int u, int l, int r)
{
	if (a[u].x - a[l].x > a[r].x - a[u].x)
		return 0;
	return 1;
}

int main()
{
	cin >> n;
	for (int i = 1; i <= n; ++i)

	{
		a[i].x = read();
		a[i].id = i;
	}
	sort(a + 1, a + n + 1, cmp);
	int L = 1, R = n;
	while (R - L > 1)
	{
		int l = L, r = R - 1, ans = l;
		while (l <= r)
		{
			int mid = l + r >> 1;
			if (check(mid, L, R))
				ans = mid, l = mid + 1;
			else
				r = mid - 1;
		}
		if (ans - L + 1 >= R - ans)
			R--;
		else
			L++;
	}
	cout << a[L].id << endl;
}

遥远的她 (distance) 距离(D)

30 分的做法当然直接 O(n4)O(n^4) 暴力枚举,简直是出题人良心的馈赠。

将平面绕原点旋转45度,并缩放2倍。然后,原本在 (X,Y)(X,Y) 的点移动到 (X+Y,X−Y)(X+Y,X-Y)。

令每个点 PiP_i 在此变换后的坐标为 (xi,yi)(x_i, y_i)。则有 xi=Xi+Yix_i = X_i + Y_i 和 yi=Xi−Yiy_i = X_i - Y_i。

接下来,我们考虑 dist(A,B)\text{dist}(A,B) 的定义是如何变化的。

在原始定义中,兔子可以从 (X,Y)(X,Y) 跳到 (X+1,Y+1)(X+1,Y+1), (X+1,Y−1)(X+1,Y-1), (X−1,Y+1)(X-1,Y+1), 和 (X−1,Y−1)(X-1,Y-1);

因此,在变换后,它可以从 (X+Y,X−Y)(X+Y,X-Y) 跳到 (X+Y+2,X−Y)(X+Y+2,X-Y), (X+Y,X−Y+2)(X+Y,X-Y+2), (X+Y,X−Y−2)(X+Y,X-Y-2), 和 (X+Y−2,X−Y)(X+Y-2,X-Y)。

将 x=X+Yx=X+Y 和 y=X−Yy=X-Y 替换后,它可以从 (x,y)(x,y) 跳到 (x+2,y)(x+2,y), (x,y+2)(x,y+2), (x,y−2)(x,y-2), 和 (x−2,y)(x-2,y)。

从 AA 到 BB 所需的最小跳跃次数是 dist(A,B)\text{dist}(A,B) 的定义(若无法到达,则 dist(A,B)=0\text{dist}(A,B)=0)。

接下来,我们考虑变换后的问题。

即,设 Pi=(xi,yi)P_i=(x_i,y_i),定义 dist(A,B)\text{dist}(A,B),并考虑如下的和:

∑i=1N−1∑j=i+1Ndist(Pi,Pj)\sum_{i=1}^{N-1} \sum_{j=i+1}^{N} \text{dist}(P_i, P_j),使用上述定义的 dist(A,B)\text{dist}(A,B)。

显然,这会得到与原始问题相同的答案。

我们进一步考虑 A=(x1,y1)A=(x_1,y_1) 和 B=(x2,y2)B=(x_2,y_2) 之间的 dist(A,B)\text{dist}(A,B)。

如果 x1≢x2(mod2)x_1 \not\equiv x_2 \pmod{2} 或 y1≢y2(mod2)y_1 \not\equiv y_2 \pmod{2},兔子无法从 AA 到达 BB,所以 dist(A,B)=0\text{dist}(A,B)=0。

否则,它正好等于曼哈顿距离的一半,即 12(∣x1−x2∣+∣y1−y2∣)\frac{1}{2}(|x_1 - x_2| + |y_1 - y_2|)。

注意到 xi=yi+2Yi≡yi(mod2)x_i = y_i + 2Y_i \equiv y_i \pmod{2} 对所有 ii 成立,这些 NN 个点可以分为两组:xix_i 和 yiy_i 都是偶数,或者 xix_i 和 yiy_i 都是奇数。

对于属于不同组的两个点 AA 和 BB,dist(A,B)=0\text{dist}(A,B)=0;对于属于同一组的两个不同点,dist(A,B)=12(∣x1−x2∣+∣y1−y2∣)\text{dist}(A,B)= \frac{1}{2}(|x_1 - x_2| + |y_1 - y_2|)。

设 E={E1,E2,…,E∣E∣}E=\{E_1, E_2, \ldots, E_{|E|}\} 是一组点,其中 xix_i 和 yiy_i 都是偶数,那么

∑i=1∣E∣−1∑j=i+1∣E∣dist(PEi,PEj)=12∑i=1∣E∣−1∑j=i+1∣E∣∣xEj−xEi∣+12∑i=1∣E∣−1∑j=i+1∣E∣∣yEj−yEi∣. \sum_{i=1}^{|E|-1} \sum_{j=i+1}^{|E|} \text{dist}(P_{E_i}, P_{E_j}) = \frac{1}{2} \sum_{i=1}^{|E|-1} \sum_{j=i+1}^{|E|} |x_{E_j} - x_{E_i}| + \frac{1}{2} \sum_{i=1}^{|E|-1} \sum_{j=i+1}^{|E|} |y_{E_j} - y_{E_i}|.

我们可以找到如下和:

∑i=1∣E∣−1∑j=i+1∣E∣∣xEj−xEi∣=∑i=1∣E∣−1∑j=i+1∣E∣(xj′−xi′)=∑i=1∣E∣(−∣E∣+1+2i)xi′, \sum_{i=1}^{|E|-1} \sum_{j=i+1}^{|E|} |x_{E_j} - x_{E_i}| = \sum_{i=1}^{|E|-1} \sum_{j=i+1}^{|E|} (x_j' - x_i') = \sum_{i=1}^{|E|} (-|E|+1+2i)x_i',

其中 (x1′,x2′,…,x∣E∣′)(x_1', x_2', \ldots, x_{|E|}') 是对 (xE1,xE2,…,xE∣E∣)(x_{E_1}, x_{E_2}, \ldots, x_{E_{|E|}}) 进行升序排序后的序列。

应用相同的讨论到 yy 坐标,可以找到 ∑i=1∣E∣−1∑j=i+1∣E∣dist(PEi,PEj)\sum_{i=1}^{|E|-1} \sum_{j=i+1}^{|E|} \text{dist}(P_{E_i}, P_{E_j});同样的讨论适用于 xix_i 和 yiy_i 都是奇数的组,最终可以找到最终的和。

对两组的 xx 和 yy 坐标进行排序的时间复杂度为 O(Nlog⁡N)O(N \log N),其余计算的时间复杂度为 O(N)O(N),所以这个问题可以在 O(Nlog⁡N)O(N \log N) 的时间内解。

C++
#include <bits/stdc++.h>
using namespace std;

int main(void) {
    freopen("distance.in", "r", stdin);
    freopen("distance.out", "w", stdout);
    int n, sz;
    long long x, y, ans = 0;
    vector<long long> a[4];
    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> x >> y;
        if ((x + y) % 2 == 0) {
            a[0].push_back(x + y);
            a[1].push_back(x - y);
//            |a[0][i]-a[0][j]|
//            |a[1][i]-a[1][j]|
        } else {
            a[2].push_back(x + y);
            a[3].push_back(x - y);
//            |a[2][i]-a[2][j]|
//            |a[3][i]-a[3][j]|
        }
    }
    for (int i = 0; i < 4; i++) {
        sort(a[i].begin(), a[i].end());
        sz = a[i].size();
//        a[i][j] ������ (sz-j-1)
//		a[i][j] ������ (j)
//		((j)-(sz-j-1))*a[i][j]
        for (int j = 0; j < sz; j++) ans += a[i][j] * (2 * j + 1 - sz);
    }
//    j
//    a[0][i]-a[0][j]

    cout << (ans / 2) << endl;
    return 0;
}

*/

提高组

T1 A. 简单数学题

算法一

按照题意模拟分数加减和乘法,期望通过 subtask1(30pts)

算法二

对于 subtask2,算法一可能会有两个问题:

  1. 分数加减时可能会爆 long long
  2. 太多次 gcd 可能导致复杂度太高

通过适当的改变运算顺序可以解决上述两个问题通过 subtask2(40pts)

算法三

对于和我们可以直接用一个变量 sum 记录前 ii 个元素的和

对于平均值可以直接计算 Bi=sum⁡/iB_i=\operatorname{sum} / i 并输出

对于方差,有 1i∑j=1i(Aj−Bi)2=1i(∑j=1iAj2−2∑j=1iAjBi+∑j=1iBi2)\frac{1}{i} \sum_{j=1}^i\left(A_j-B_i\right)^2=\frac{1}{i}\left(\sum_{j=1}^i A_j^2-2 \sum_{j=1}^i A_j B_i+\sum_{j=1}^i B_i^2\right) =1i(∑j=1iAj2)−Bi2=\frac{1}{i}\left(\sum_{j=1}^i A_j^2\right)-B_i^2

再记录一个元素表示平方和即可直接计算方差。同时容易发现通过该式计算方差不会导致分子爆 long long。

C++
#include <bits/stdc++.h>
using namespace std;
typedef long long ull;
ull gcd(ull a, ull b) { return b > 0 ? gcd(b, a % b) : a; }
void print(ull a, ull b, char end = '\n')
{
	ull d = gcd(a, b);
	a /= d;
	b /= d;
	if (b == 1)
		printf("%lld", a);
	else
		printf("%lld/%lld", a, b);
	printf("%c", end);
}
int main()
{
	int n;
	scanf("%d", &n);
	ull sum = 0, sum2 = 0;
	for (int i = 1; i <= n; ++i)
	{
		int x;
		scanf("%d", &x);
		sum += x;
		sum2 += 1ll * x * x;
		print(sum, 1, ' ');
		print(sum, i, ' ');
		print(sum2 * i - sum * sum, 1ll * i * i, '\n');
	}
	return 0;
}

T2 B. 简单算法题

算法一

暴力还原括号串然后暴力枚举区间判断是否合法,8pts

算法二

暴力还原括号串,然后考虑使用数据结构维护。

考虑将 ( 视为 1,将 ) 视为 -1,做前缀和得到数组 ss,区间 [l,r][l, r] 合法当且仅当 sr=sl−1s_r=s_{l-1} 且对于任意 k∈[l,r]k \in[l, r],有 sk≥srs_k \geq s_r

这意味着如果有 i<ji<j 且 sj<sis_j<s_i,则对于任意 k≥jk \geq j,区间 [i,k][i, k] 都不合法。所以我们可以尝试用一个栈维护可能合法的左端点。

当加入一个新的元素 sis_i 时,我们只需要弹出栈顶大于 sis_i 的元素然后将等于 sis_i 的元素贡献进答案。

为了保证复杂度我们可以把值相同的元素一起处理。

能过 subtask1,2, 24pts。

算法三

对于 cic_i 大的情况,可以将枚举左端点所在块和右端点所在块,然后计算中间部分至少要在左侧加几个左括号,至少要在右侧加几个右括号。

复杂度 Θ(n3)\Theta\left(n^3\right) 或 Θ(n2)\Theta\left(n^2\right)。

然后 subtask5 可以随便特判一下,最高可以得56pts。

算法四

结合算法二和算法三,把公差为 1 的 sis_i 等差数列合在一起处理。复杂度 O(n)O(n),可以 AC\mathrm{AC}。

顺便提一下,结合代码可以发现答案不会超过 n(n+max⁡{m0,m1})n\left(n+\max \left\{m_0, m_1\right\}\right),所以不会爆 longlong。

其它想法

可以用线段树之类的方法做到 O(nlg⁡n)O(n \lg n),这样会TLE。数据是随机的,说不定随便写个乱搞能过。

C++
#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
ull pop_back(stack<pair<ull, int>> &stk, ull c)
{
	ull ans = 0;
	while (c > 0)
	{
		pair<ull, int> x = stk.top();
		stk.pop();
		ull d = min(c, x.first);
		x.first -= d;
		c -= d;
		if (x.first > 0)
			stk.push(x);
		ans += 1llu * d * x.second;
	}
	return ans;
}
int main()
{
	int n;
	scanf("%d", &n);
	vector<int> c(n);
	int x, y, z, m[2];
	scanf("%d%d%d%d%d%d%d", &x, &y, &z, &m[0], &m[1], &c[0], &c[1]);
	for (int i = 2; i < n; ++i)
		c[i] = (1ll * c[i - 1] * x + 1ll * c[i - 2] * y + z) % m[i % 2] + 1;
	//	for(int i=0;i<n;++i) cerr<<c[i]<<" ";
	stack<pair<ull, int>> stk;
	stk.push(make_pair((ull)1e18, 0));
	stk.push(make_pair(1, 1));
	ull ans = 0;
	ull ss1 = 0, ss2 = 0;
	for (int i = 0; i < n; ++i)
	{
		if (~i & 1)
		{
			ss1 += c[i];
			stk.push(make_pair(c[i], 1));
		}
		else
		{
			ss2 += c[i];
			pop_back(stk, 1);
			ans += pop_back(stk, c[i] - 1);
			int t = stk.top().second;
			ans += pop_back(stk, 1);
			stk.push(make_pair(1, t + 1));
			//			printf("<%lld>",ans);
		}
	}
	printf("%lld\n", ans);
	//	cerr<<ss1<<" "<<ss2<<endl;
	return 0;
}

T3 C. 子序列计数

算法一

爆搜,能过 subtask1,15pts。

算法二

随手写个 dp\mathrm{dp},比如 f(i,x,y,z)f(i, x, y, z) 表示决策前 ii 个元素,最后一个取了 xx,倒数第二个取了 yy,倒数第三个取了 zz。转移的话如果下一个加进去不合法就不取,否则枚举下一个取不取。

复杂度 Θ(n4)\Theta\left(n^4\right),可以过前两个subtask,35pts。

算法三

压缩一下之前的状态,f(x,y,z)f(x, y, z) 表示最后一个取的是 xx,倒数第二个是 yy,倒数第三个是 zz 的方案数。

考虑一般情况,可以有转移:

f(x,y,z)=∑i<z,ax⊕ay⊕az⊕ai≠sf(y,z,i)f(x, y, z)=\sum_{i<z, a_x \oplus a_y \oplus a_z \oplus a_i \neq s} f(y, z, i)

可以考虑把 ∑i<z,ai=ax⊕ay⊕az⊕sf(y,z,i)\sum_{i<z, a_i=a_x \oplus a_y \oplus a_z \oplus s} f(y, z, i) 的部分用另外一个数组维护一下,即可 O(1)O(1) 转移。

复杂度 Θ(n3+n2m)\Theta\left(n^3+n 2^m\right),可以过 subtask1,2,3,60pts。

算法四

考虑发掘一些性质,由于 aia_i 互不相同,所以对于子序列中连续的 5 个元素,如果前四个异或和为 ss,那么后四个异或和一定不为 ss。

根据这个性质考虑再次压缩状态:f(x,y)f(x, y) 表示最后一个取的是 xx,倒数第二个是 yy 的方案数。那么有转移方程:

f(x,y)=∑z<y(f(y,z)−∑i<z,ax⊕ay⊕az⊕ai=sf(z,i))f(x, y)=\sum_{z<y}\left(f(y, z)-\sum_{i<z, a_x \oplus a_y \oplus a_z \oplus a_i=s} f(z, i)\right)

考虑拆一下:

f(x,y)=∑z<yf(y,z)−∑i<z<y,ai⊕ay⊕az⊕s=axf(z,i)f(x, y)=\sum_{z<y} f(y, z)-\sum_{i<z<y, a_i \oplus a_y \oplus a_z \oplus s=a_x} f(z, i)

两部分都可以拿个数组维护一下,复杂度 Θ(n(n+2m))\Theta\left(n\left(n+2^m\right)\right),可以 AC\mathrm{AC}。

C++
#include <bits/stdc++.h>
using namespace std;
const int N = 1 << 12 | 5, MOD = 998244353;
inline void mo(int &x) { x >= MOD ? x -= MOD : 0; }
inline int mo1(int x) { return x >= MOD ? x - MOD : x; }
int a[N], n, f[N][N], g[N], h[N][N], s;
int main()
{
	int n, m, s;
	scanf("%d%d%d", &n, &m, &s);
	for (int i = 1; i <= n; ++i)
		scanf("%d", &a[i]);
	int ans = n;
	for (int i = 1; i <= n; ++i)
	{
		g[i] = 0;
		for (int j = 1; j < i; ++j)
		{
			f[i][a[j]] = (1ll + g[j] + MOD - h[j - 1][a[i] ^ a[j] ^ s]) % MOD;
			mo(g[i] += f[i][a[j]]);
		}
		mo(ans += g[i]);
		for (int t = 0; t < 1 << m; ++t)
			h[i][t] = (h[i - 1][t] + f[i][a[i] ^ t]) % MOD;
	}
	printf("%d\n", ans);
	return 0;
}

T4 D. 邮递员

首先,当 n,m≤6n, m \leq 6 时,我们爆搜出所有合法的解。接下来,不妨设 max⁡(n,m)>6\max (n, m)>6。

若行数大于 6,且最顶部两行中不包含起点与终点,则我们可以直接删除顶端两行,并将得到的合法方案进行拼接即可:

同样地,若最左侧、最右侧、最底侧不包含起点与终点,我们同样可以进行上述操作。而当起点与终点位于这些区域时,由于 max⁡(n,m)≤6\max (n, m) \leq 6,因此必有两行只包含了起点或终点,我们同样进行调整,得到新的起点后删除上述两行即可。

可以证明,只要步数的奇偶性正确,在 n,m≥4n, m \geq 4 时一定有解,因此我们通过上述操作后一定能得到合法的解。

时间复杂度为 O( nm)O(\mathrm{~nm})。

C++
#include <bits/stdc++.h>

std::mt19937 hua(20041115);

const int N = 1005;

int n, m, sx, sy, tx, ty;
const int dx[5] = {1, 0, -1, 0, 0x3f3f3f3f};
const int dy[5] = {0, 1, 0, -1, 0x3f3f3f3f};
const char *dir = "RDLU"; // Polish character??
int st[N][N];
bool vis[N][N];

int get_id(int x, int y)
{
	for (int i = 0; i < 4; ++i)
		if (dx[i] == x && dy[i] == y)
			return i;
	throw;
}
int parse_en(char ch)
{
	// I don't understand Polish
	if (ch == 'R')
		return 0;
	if (ch == 'D')
		return 1;
	if (ch == 'L')
		return 2;
	if (ch == 'U')
		return 3;
	throw;
}
bool inside(int lx, int rx, int ly, int ry, int x, int y)
{
	return lx <= x && x <= rx && ly <= y && y <= ry;
}
bool ok(int lx, int rx, int ly, int ry, int x, int y)
{
	return inside(lx, rx, ly, ry, x, y) && !vis[x][y];
}

bool dfs(int lx, int rx, int ly, int ry, int sx, int sy, int tx, int ty, int steps)
{
	if (sx == tx && sy == ty)
	{
		if (steps == 1)
			return true;
		return false;
	}
	if (steps == 1)
		return false;
	for (int o = 0; o < 4; ++o)
	{
		int nx = sx + dx[o], ny = sy + dy[o];
		if (!ok(lx, rx, ly, ry, nx, ny))
			continue;
		st[sx][sy] = o;
		vis[nx][ny] = true;
		if (dfs(lx, rx, ly, ry, nx, ny, tx, ty, steps - 1))
			return true;
		vis[nx][ny] = false;
	}
	return false;
}

int cur_x, cur_y;
void set_dir(int x1, int y1, int x2, int y2, bool visited = false)
{
	// printf("set dir %d %d %d %d\n", x1, y1, x2, y2);
	assert(cur_x == x1);
	assert(cur_y == y1);
	st[x1][y1] = get_id(x2 - x1, y2 - y1);
	if (vis[x2][y2] != visited)
	{
		throw;
	}
	vis[x2][y2] = true;
	cur_x = x2, cur_y = y2;
}
void cancel(int x, int y)
{
	if (!vis[x][y])
	{
		throw;
	}
	int nx = x + dx[st[x][y]];
	int ny = y + dy[st[x][y]];
	vis[nx][ny] = false;
	st[x][y] = 0;
}
void move(int lx, int rx, int ly, int ry, int dx, int dy, bool mode)
{
	/* mode:
			0: move one step
			1: keep moving
	*/
	// fprintf(stderr, "move %d %d %d %d %d %d %d\n", lx, rx, ly, ry, dx, dy, mode);
	// fprintf(stderr, "curx = %d, cury = %d\n", cur_x, cur_y);
	int o = get_id(dx, dy);
	if (mode == false)
	{
		set_dir(cur_x, cur_y, cur_x + dx, cur_y + dy);
	}
	else
	{
		while (ok(lx, rx, ly, ry, cur_x + dx, cur_y + dy))
		{
			set_dir(cur_x, cur_y, cur_x + dx, cur_y + dy);
		}
	}
}

void solve(int lx, int rx, int ly, int ry, int sx, int sy, int tx, int ty)
{
	if (rx - lx + 1 <= 6 && ry - ly + 1 <= 6)
	{
		// for (int cx = lx; cx <= rx; ++cx)
		//	for (int cy = ly; cy <= ry; ++cy)
		//		assert(!vis[cx][cy]);
		if (!dfs(lx, rx, ly, ry, sx, sy, tx, ty, (rx - lx + 1) * (ry - ly + 1)))
		{
			puts("NIE"); // gg
			exit(0);
		}

		return;
	}
	auto go = [&](int dx, int dy, bool mode)
	{
		move(lx, rx, ly, ry, dx, dy, mode);
	};
	auto gogo = [&](char ch, int len)
	{
		int o = parse_en(ch);
		assert(len >= -1);
		int total_steps = 0;
		if (len == -1)
		{
			while (ok(lx, rx, ly, ry, cur_x + dx[o], cur_y + dy[o]))
			{
				++total_steps;
				go(dx[o], dy[o], false);
			}
			return total_steps;
		}
		else
		{
			for (int i = 0; i < len; ++i)
			{
				go(dx[o], dy[o], false);
			}
			return len;
		}
	};
	auto extend_path = [&](std::string s)
	{
		bool mode = false;
		for (auto ch : s)
		{
			int o = parse_en(ch);
			go(dx[o], dy[o], mode);
			mode = !mode;
		}
	};
	auto remake = [&](int x, int y)
	{
		cancel(x, y);
		cur_x = x, cur_y = y;
	};
	auto bounded = [&](int lx, int rx, int ly, int ry)
	{
		return inside(lx, rx, ly, ry, sx, sy) && inside(lx, rx, ly, ry, tx, ty);
	};
	auto ensure = [&](int x0, int y0)
	{
		if (x0 != cur_x || y0 != cur_y)
		{
			// ��
			throw;
		}
	};
	auto ensure_R = [&](int x0, int x1, int y0, int y1)
	{
		if (!(x0 <= cur_x && cur_x <= x1 && y0 <= cur_y && cur_y <= y1))
		{
			throw;
		}
	};
	auto validate_shrink = [&](int l, int r)
	{
		return r - l + 1 > 6;
	};
	if (validate_shrink(lx, rx) && bounded(lx + 2, rx, ly, ry))
	{
		// Case 1A: Left Free
		solve(lx + 2, rx, ly, ry, sx, sy, tx, ty);
		bool ok = false;
		for (int j = ly; j <= ry; ++j)
		{
			if (dy[st[lx + 2][j]] == 1)
			{
				assert(j < ry);
				remake(lx + 2, j);
				extend_path("LULDRUR");
				ensure(lx + 2, j + 1);
				ok = true;
				break;
			}
			else if (dy[st[lx + 2][j]] == -1)
			{
				assert(j > ly);
				remake(lx + 2, j);
				extend_path("LDLURDR");
				ensure(lx + 2, j - 1);
				ok = true;
				break;
			}
		}
		assert(ok);
	}
	else if (validate_shrink(lx, rx) && bounded(lx, rx - 2, ly, ry))
	{
		// Case 1B: Right Free
		solve(lx, rx - 2, ly, ry, sx, sy, tx, ty);
		bool ok = false;
		for (int j = ly; j <= ry; ++j)
		{
			if (dy[st[rx - 2][j]] == 1)
			{
				remake(rx - 2, j);
				extend_path("RURDLUL");
				ensure(rx - 2, j + 1);
				ok = true;
				break;
			}
			else if (dy[st[rx - 2][j]] == -1)
			{
				remake(rx - 2, j);
				extend_path("RDRULDL");
				ensure(rx - 2, j - 1);
				ok = true;
				break;
			}
		}
		assert(ok);
	}
	else if (validate_shrink(ly, ry) && bounded(lx, rx, ly + 2, ry))
	{
		// Case 1C: Up Free
		solve(lx, rx, ly + 2, ry, sx, sy, tx, ty);
		bool ok = false;
		for (int j = lx; j <= rx; ++j)
		{
			if (dx[st[j][ly + 2]] == -1)
			{
				remake(j, ly + 2);
				extend_path("URULDRD");
				ensure(j - 1, ly + 2);
				ok = true;
				break;
			}
			else if (dx[st[j][ly + 2]] == 1)
			{
				remake(j, ly + 2);
				extend_path("ULURDLD");
				ensure(j + 1, ly + 2);
				ok = true;
				break;
			}
		}
		assert(ok);
	}
	else if (validate_shrink(ly, ry) && bounded(lx, rx, ly, ry - 2))
	{
		// Case 1D: Down Free
		solve(lx, rx, ly, ry - 2, sx, sy, tx, ty);
		bool ok = false;
		for (int j = lx; j <= rx; ++j)
		{
			if (dx[st[j][ry - 2]] == -1)
			{
				remake(j, ry - 2);
				extend_path("DRDLURU");
				ensure(j - 1, ry - 2);
				ok = true;
				break;
			}
			else if (dx[st[j][ry - 2]] == 1)
			{
				remake(j, ry - 2);
				extend_path("DLDRULU");
				ensure(j + 1, ry - 2);
				ok = true;
				break;
			}
		}
		assert(ok);
	}
	// Case 2-5: keep moving until the length of each border <= 6
	else if (validate_shrink(lx, rx) && inside(lx, lx, ly, ry, sx, sy))
	{
		// Case 2A: left bound
		// Note that (ry - ly + 1) >= 4
		cur_x = sx, cur_y = sy;
		if (sy == ry)
		{
			// Case 2AA
			gogo('U', -1);
			gogo('R', 1);
			gogo('D', -1);
			gogo('R', 1);
			ensure_R(lx + 2, rx, ly, ry);
			solve(lx + 2, rx, ly, ry, cur_x, cur_y, tx, ty);
		}
		else if (sy + 1 == ry)
		{
			// Case 2AB
			gogo('D', 1);
			gogo('R', 1);
			gogo('U', 2);
			gogo('L', 1);
			gogo('U', -1);
			gogo('R', 1);
			gogo('D', -1);
			gogo('R', 1);
			ensure_R(lx + 2, rx, ly, ry);
			solve(lx + 2, rx, ly, ry, cur_x, cur_y, tx, ty);
		}
		else
		{
			// Case 2AC
			int len = gogo('U', -1);
			gogo('R', 1);
			gogo('D', len + 1);
			gogo('L', 1);
			gogo('D', -1);
			gogo('R', 1);
			gogo('U', -1);
			gogo('R', 1);
			ensure_R(lx + 2, rx, ly, ry);
			solve(lx + 2, rx, ly, ry, cur_x, cur_y, tx, ty);
		}
	}
	else if (validate_shrink(lx, rx) && inside(lx + 1, lx + 1, ly, ry, sx, sy))
	{
		// Case 2B: left strip
		cur_x = sx, cur_y = sy;
		if (sy == ry)
		{
			// Case 2BA
			gogo('L', 1);
			gogo('U', -1);
			gogo('R', 1);
			gogo('D', -1);
			gogo('R', 1);
			ensure_R(lx + 2, rx, ly, ry);
			solve(lx + 2, rx, ly, ry, cur_x, cur_y, tx, ty);
		}
		else
		{
			// Case 2BB
			gogo('U', -1);
			gogo('L', 1);
			gogo('D', -1);
			gogo('R', 1);
			gogo('U', -1);
			gogo('R', 1);
			ensure_R(lx + 2, rx, ly, ry);
			solve(lx + 2, rx, ly, ry, cur_x, cur_y, tx, ty);
		}
	}
	else if (validate_shrink(lx, rx) && inside(rx, rx, ly, ry, sx, sy))
	{
		// Case 3A: right bound
		cur_x = sx, cur_y = sy;
		if (sy == ry)
		{
			// Case 2AA
			gogo('U', -1);
			gogo('L', 1);
			gogo('D', -1);
			gogo('L', 1);
			ensure_R(lx, rx - 2, ly, ry);
			solve(lx, rx - 2, ly, ry, cur_x, cur_y, tx, ty);
		}
		else if (sy + 1 == ry)
		{
			// Case 2AB
			gogo('D', 1);
			gogo('L', 1);
			gogo('U', 2);
			gogo('R', 1);
			gogo('U', -1);
			gogo('L', 1);
			gogo('D', -1);
			gogo('L', 1);
			ensure_R(lx, rx - 2, ly, ry);
			solve(lx, rx - 2, ly, ry, cur_x, cur_y, tx, ty);
		}
		else
		{
			// Case 2AC
			int len = gogo('U', -1);
			gogo('L', 1);
			gogo('D', len + 1);
			gogo('R', 1);
			gogo('D', -1);
			gogo('L', 1);
			gogo('U', -1);
			gogo('L', 1);
			ensure_R(lx, rx - 2, ly, ry);
			solve(lx, rx - 2, ly, ry, cur_x, cur_y, tx, ty);
		}
	}
	else if (validate_shrink(lx, rx) && inside(rx - 1, rx - 1, ly, ry, sx, sy))
	{
		// Case 3B: right strip
		cur_x = sx, cur_y = sy;
		if (sy == ry)
		{
			// Case 3BA
			gogo('R', 1);
			gogo('U', -1);
			gogo('L', 1);
			gogo('D', -1);
			gogo('L', 1);
			ensure_R(lx, rx - 2, ly, ry);
			solve(lx, rx - 2, ly, ry, cur_x, cur_y, tx, ty);
		}
		else
		{
			// Case 3BB
			gogo('U', -1);
			gogo('R', 1);
			gogo('D', -1);
			gogo('L', 1);
			gogo('U', -1);
			gogo('L', 1);
			ensure_R(lx, rx - 2, ly, ry);
			solve(lx, rx - 2, ly, ry, cur_x, cur_y, tx, ty);
		}
	}
	else if (validate_shrink(ly, ry) && inside(lx, rx, ly, ly, sx, sy))
	{
		// Case 4A: Up bound
		cur_x = sx, cur_y = sy;
		if (sx == rx)
		{
			// Case 4AA
			gogo('L', -1);
			gogo('D', 1);
			gogo('R', -1);
			gogo('D', 1);
			ensure_R(lx, rx, ly + 2, ry);
			solve(lx, rx, ly + 2, ry, cur_x, cur_y, tx, ty);
		}
		else if (sx + 1 == rx)
		{
			gogo('R', 1);
			gogo('D', 1);
			gogo('L', 2);
			gogo('U', 1);
			gogo('L', -1);
			gogo('D', 1);
			gogo('R', -1);
			gogo('D', 1);
			ensure_R(lx, rx, ly + 2, ry);
			solve(lx, rx, ly + 2, ry, cur_x, cur_y, tx, ty);
		}
		else
		{
			int steps = gogo('L', -1);
			gogo('D', 1);
			gogo('R', steps + 1);
			gogo('U', 1);
			gogo('R', -1);
			gogo('D', 1);
			gogo('L', -1);
			gogo('D', 1);
			ensure_R(lx, rx, ly + 2, ry);
			solve(lx, rx, ly + 2, ry, cur_x, cur_y, tx, ty);
		}
	}
	else if (validate_shrink(ly, ry) && inside(lx, rx, ly + 1, ly + 1, sx, sy))
	{
		// Case 4B: Up strip
		cur_x = sx, cur_y = sy;
		if (sx == rx)
		{
			gogo('U', 1);
			gogo('L', -1);
			gogo('D', 1);
			gogo('R', -1);
			gogo('D', 1);
			ensure_R(lx, rx, ly + 2, ry);
			solve(lx, rx, ly + 2, ry, cur_x, cur_y, tx, ty);
		}
		else
		{
			gogo('L', -1);
			gogo('U', 1);
			gogo('R', -1);
			gogo('D', 1);
			gogo('L', -1);
			gogo('D', 1);
			ensure_R(lx, rx, ly + 2, ry);
			solve(lx, rx, ly + 2, ry, cur_x, cur_y, tx, ty);
		}
	}
	else if (validate_shrink(ly, ry) && inside(lx, rx, ry, ry, sx, sy))
	{
		// Case 5A: Down bound
		cur_x = sx, cur_y = sy;
		if (sx == rx)
		{
			// Case 5A
			gogo('L', -1);
			gogo('U', 1);
			gogo('R', -1);
			gogo('U', 1);
			ensure_R(lx, rx, ly, ry - 2);
			solve(lx, rx, ly, ry - 2, cur_x, cur_y, tx, ty);
		}
		else if (sx + 1 == rx)
		{
			gogo('R', 1);
			gogo('U', 1);
			gogo('L', 2);
			gogo('D', 1);
			gogo('L', -1);
			gogo('U', 1);
			gogo('R', -1);
			gogo('U', 1);
			ensure_R(lx, rx, ly, ry - 2);
			solve(lx, rx, ly, ry - 2, cur_x, cur_y, tx, ty);
		}
		else
		{
			int steps = gogo('L', -1);
			gogo('U', 1);
			gogo('R', steps + 1);
			gogo('D', 1);
			gogo('R', -1);
			gogo('U', 1);
			gogo('L', -1);
			gogo('U', 1);
			ensure_R(lx, rx, ly, ry - 2);
			solve(lx, rx, ly, ry - 2, cur_x, cur_y, tx, ty);
		}
	}
	else if (validate_shrink(ly, ry) && inside(lx, rx, ry - 1, ry - 1, sx, sy))
	{
		// Case 4B: Up strip
		cur_x = sx, cur_y = sy;
		if (sx == rx)
		{
			gogo('D', 1);
			gogo('L', -1);
			gogo('U', 1);
			gogo('R', -1);
			gogo('U', 1);
			ensure_R(lx, rx, ly, ry - 2);
			solve(lx, rx, ly, ry - 2, cur_x, cur_y, tx, ty);
		}
		else
		{
			gogo('L', -1);
			gogo('D', 1);
			gogo('R', -1);
			gogo('U', 1);
			gogo('L', -1);
			gogo('U', 1);
			ensure_R(lx, rx, ly, ry - 2);
			solve(lx, rx, ly, ry - 2, cur_x, cur_y, tx, ty);
		}
	}
	else
	{
		puts("TBD");
		exit(0);
	}
}

void check_NIE()
{
	int steps = n * m - 1;
	int parity = (sx ^ sy) ^ (tx ^ ty);
	if ((steps & 1) != (parity & 1))
	{
		fprintf(stderr, "gg1\n");
		puts("NIE");
		exit(0);
	}
	if (steps % 2 == 0)
	{
		if ((sx ^ sy) & 1)
		{
			fprintf(stderr, "gg2\n");
			puts("NIE");
			exit(0);
		}
	}
}

void construct_solution()
{
	while (sx != tx || sy != ty)
	{
		int o = st[sx][sy];
		putchar(dir[o]);
		sx += dx[o];
		sy += dy[o];
	}
}

int main()
{
	std::ios::sync_with_stdio(false);
	std::cin >> n >> m >> sx >> sy >> tx >> ty;
	check_NIE();
	vis[sx][sy] = 1;
	st[tx][ty] = 4;
	solve(1, n, 1, m, sx, sy, tx, ty);
	construct_solution();
}
原文