跳到正文

题解

ElisaOI R3 题解

steambird约 48 分钟
原文
文章信息与版本

文章信息

发布
2024年11月17日
收录
2025年12月12日
更新
2026年11月14日
篇幅
约 48 分钟

版本

  1. v1当前

ElisaOI R3 题解

大概是 AFO 前最后拉一场邀请赛。

统计数据

统计时间:11/23 16:53

报名人数:2222 | 有效参与人数:1010

最高分:__Seniorious__,3500pts3500pts。

排行:

各题实际难度:

题目ABCDEFGHIJK
参考难度A\textcolor{red}{\text{A}}B\textcolor{green}{\text{B}}C\textcolor{orange}{\text{C}}D\textcolor{green}{\text{D}}E\textcolor{orange}{\text{E}}F\textcolor{red}{\text{F}}G\textcolor{green}{\text{G}}H\textcolor{blue}{\text{H}}I\textcolor{purple}{\text{I}}J\textcolor{green}{\text{J}}K\textcolor{blue}{\text{K}}

(方便阅读,红和橙都标红,黄标橙)

[A] Explosive Klee

Trick: 破环成链的活用 or 确定题目限制。

做法 1

类比破环成链,构造 T=S+ST=S+S 即可满足所有限制。时间复杂度 O(∣S∣)O(|S|)。

C++
#include <bits/stdc++.h>
using namespace std;
inline void train() {
	   ios::sync_with_stdio(false);
	   cin.tie(0);
	   cout.tie(0);
}
int main() {

	train();
	
	int n,m,x;
	cin>>n>>m;
	for (int i = 1; i <= m; i++) cin>>x;
	string s;
	cin>>s;
	cout<<(s+s)<<endl;
	
	//cout<<flush;

	return 0;
}

做法 2

由 __Seniorious__ 提供。

考虑一个限制 xx 强制使得 [1,x−1],[x+n,n][1,x-1],[x+n,n] 为原字符串,处理出自由区域的左端点和右端点即可。

这也是 Ectau 提出的 SPJ 写法。

下面给出 __Seniorious__ 的 AC 代码:

C++
/**
 * @author : cyb1010
 * @date   : 2024-11-17 09:34:57
 * @file   : U_504782_ElisaOI_R_3_A_Explosive_Klee.cpp
 */
#include <bits/stdc++.h>
using namespace std;
#define fo(s) freopen(s ".in", "r", stdin), freopen(s ".out", "w", stdout)
#define fi first
#define se second
#define ALL(v) (v).begin(), (v).end()
#define SZ(v) int(v.size())
typedef double db;
typedef long double ldb;
typedef long long ll;
typedef unsigned long long ull;
void ckmx(int &x, int y) { x < y && (x = y); }
void ckmn(int &x, int y) { x > y && (x = y); }
int __, n, m, mx, mn = 100010;
string s, t;
int main() {
    // fo("U_504782_ElisaOI_R_3_A_Explosive_Klee");
    cin.tie(0)->sync_with_stdio(0);
    cin >> n >> m;
    for (int x; m--;) cin >> x, ckmx(mx, x), ckmn(mn, x);
    cin >> s, t = s.substr(0, mx - 1);
    for (int i = mx; i < mn + n; i++) t.push_back('a');
    t += s.substr(mn - 1, n - mn + 1);
    cout << t << '\n';
    return 0 ^ __ ^ 0;
}

[B] String Appearance

Trick: 状态设计与状态压缩。

原题 SP25180。

[C] Ectau Hacks LZY

Trick: 对期望的理解;分类讨论。

当 n=1n = 1 时,答案显然为 a1a_1。

当 n>1n > 1 时,对于任意操作序列,只要反着操作,一定能使得最终答案为该操作序列结果的相反数,因此所有可能性抵消为 00。

注意负数取模。

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

inline void train() {
	   ios::sync_with_stdio(false);
	   cin.tie(0);
	   cout.tie(0);
}

inline int maxi(int a, int b) {
	return a > b ? a : b;
}

inline int mini(int a, int b) {
	return a < b ? a : b;
}

constexpr long long MODER = 998244353;

int main() {

	train();
	
	int n;
	cin>>n;
	if (n == 1) {
		long long x;
		cin>>x;
		if (x >= 0) cout<<(x%MODER)<<endl;
		else cout<<((MODER - ((-x) % MODER))%MODER)<<endl;
	} else {
		cout<<0<<endl;
	}
	
	//cout<<flush;

	return 0;
}

[D] Road Construction

Trick: 二分答案;图论建模

150 分部分分做法

树上两点之间路径固定,可以二分答案(因为答案越大,维修付出一定越大),考虑每条边需要进行多少次维修并判断是否超出预算或维修次数限制。

满分做法

在二分的基础上继续考虑:对于答案 xx,相当于我们在一张满足下面条件的新图上跑最短路(要满足这些条件走过去需要付出的代价):

  • ti≥xt_i \ge x,边权为 00(走过去不需要花费额外代价);
  • ti<xt_i < x 时:
  1. x−ti>dix-t_i>d_i 时,边权为 +∞+\infin(走不过去);
  2. 否则,边权为 ci×(x−ti)c_i \times (x-t_i)。

在这张图上 aa 到 bb 的最短路即为 xx 答案下的最小代价,若 ≤k\le k 则满足条件,反之不满足。

Code:

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

inline void train() {
	   ios::sync_with_stdio(false);
	   cin.tie(0);
	   cout.tie(0);
}

struct edge {
	int from, to, t, c, d;
};

struct nedge {
	int to;
	long long v;
	nedge() {
		
	}
	nedge(int to, long long v) : to(to), v(v) {
		
	}
};

bool operator > (const nedge &a, const nedge &b) {
	return a.v > b.v;
}

priority_queue<nedge, vector<nedge>, greater<nedge> > pq;

constexpr int N = 1e4+9, M = 1e5+9, INF = (INT_MAX >> 2) - 9;

vector<nedge> graph[N];
edge es[M];

int n,m,k,a,b,dis[N];

// If able to reach x...
inline bool check(int x) {
	for (int i = 1; i <= n; i++) {
		graph[i].clear();
		dis[i] = INF;
	}
	dis[a] = 0;
	for (int i = 0; i < m; i++) {
		if (es[i].t >= x) {
			graph[es[i].from].push_back(nedge(es[i].to, 0));
			graph[es[i].to].push_back(nedge(es[i].from, 0));
		} else {
			int required = x - es[i].t;
			if (required <= es[i].d) {
				long long cost = required;
				cost *= es[i].c;
				if (cost <= k) {
					graph[es[i].from].push_back(nedge(es[i].to, cost));
					graph[es[i].to].push_back(nedge(es[i].from, cost));
				}	// else : No chance
			} // else : No connection
		}
	}
	pq.push(nedge(a, 0));
	while (!pq.empty()) {
		nedge cur = pq.top();
		pq.pop();
		if (cur.v > dis[cur.to]) continue;
		dis[cur.to] = cur.v;
		for (auto &j : graph[cur.to]) {
			if (cur.v + j.v < dis[j.to]) {
				dis[j.to] = cur.v + j.v;
				pq.push(nedge(j.to, dis[j.to]));
			}
		}
	}
	return dis[b] <= k;
}

int main() {

	train();
	
	int g;
	cin>>n>>m>>k>>a>>b>>g;
	
	for (int i = 0; i < m; i++) {
		cin>>es[i].from>>es[i].to>>es[i].t>>es[i].c>>es[i].d;
	}
	
	int l = 1, r = 2e7+10, ans;
	while (l <= r) {
		int mid = (l + r) >> 1;
		if (check(mid)) {
			l = mid + 1;
			ans = mid;
		} else {
			r = mid - 1;
		}
	}
	
	cout<<ans<<endl;
	//cout<<flush;

	return 0;
}

[E] Numeric Operation

Trick: 观察循环性质 or 数位dp;缩小问题规模(类似分治)。

做法 1

注意到对于一个正整数 xx,x+1x+1 和 x+10x+10 进行单次操作后答案要么相同,要么差 99(考虑进位)。最终会操作到一位正整数,而只有两位时具有 +9+9 不变性。因此,所有正整数均具有 +9+9 不变性。注意需要特殊考虑 00。

由此,可以得到对于 xx,答案为 ((x−1) mod 9)+1((x-1) \bmod 9) + 1。因而容易得到下面的计数代码:

C++
#include <bits/stdc++.h>
using namespace std;
int extra[10];
int main() {
	long long l,r;
	cin>>l>>r;
	int zero = 0;
	if (l == 0) zero = 1, l = 1;
	int inits = ((l-1) % 9) + 1;
	long long tlen = r-l+1;
	long long divs = tlen / 9, rems = tlen % 9;
	for (int i = 0; i < rems; inits = (inits % 9) + 1, i++) extra[inits]++;
	cout<<zero<<' ';
	for (int i = 1; i <= 9; i++) cout<<(divs+extra[i])<<' ';
	cout<<endl;
	return 0;
}

做法 2

由 __Seniorious__ 提供。

观察到每一个 xx 操作一次后得到的值不会超过 18×9=16218 \times 9 = 162。因此可以处理出 [L,R][L,R] 内每个数的数位和(这可以数位 dp),然后预处理 [0,162][0, 162] 内数的答案,将每个数位和对应到实际答案即可。

C++
/**
 * @author : cyb1010
 * @date   : 2024-11-17 11:35:32
 * @file   : U_505274_ElisaOI_R_3_E_Numeric_Operation.cpp
 */
#include <bits/stdc++.h>
using namespace std;
#define fo(s) freopen(s ".in", "r", stdin), freopen(s ".out", "w", stdout)
#define fi first
#define se second
#define ALL(v) (v).begin(), (v).end()
#define SZ(v) int(v.size())
typedef double db;
typedef long double ldb;
typedef long long ll;
typedef unsigned long long ull;
void ckmx(int &x, int y) { x < y && (x = y); }
void ckmn(int &x, int y) { x > y && (x = y); }
typedef array<ll, 10> A;
const int mx = 180;
int __, s[200];
ll L, R, f[20][200][2], pw[20] = {1};
void init() {
    for (int i = 1; i <= 18; i++) pw[i] = pw[i - 1] * 10;
    iota(s, s + 10, 0);
    for (int i = 10; i <= mx; i++) {
        int v = i, f = 0;
        while (v) f += v % 10, v /= 10;
        s[i] = s[f];
    }
}
A solve(ll r) {
    if (r < 0) return A{};
    memset(f, 0, sizeof(f)), f[19][0][1] = 1;
    for (int i = 18, nw; ~i; i--) {
        nw = r / pw[i] % 10;
        for (int s = 0; s < mx; s++) {
            for (int p = 0; p < nw; p++)
                f[i][s + p][0] += f[i + 1][s][0] + f[i + 1][s][1];
            f[i][s + nw][0] += f[i + 1][s][0];
            f[i][s + nw][1] += f[i + 1][s][1];
            for (int p = nw + 1; p < 10; p++) f[i][s + p][0] += f[i + 1][s][0];
        }
    }
    A res = {};
    for (int i = 0; i < mx; i++) res[s[i]] += (f[0][i][0] + f[0][i][1]);
    return res;
}
int main() {
    // fo("U_505274_ElisaOI_R_3_E_Numeric_Operation");
    cin.tie(0)->sync_with_stdio(0);
    cin >> L >> R, init();
    A x = solve(R), y = solve(L - 1);
    for (int i = 0; i < 10; i++) cout << x[i] - y[i] << " \n"[i == 9];
    return 0 ^ __ ^ 0;
}

[F] Ectau Has a Good Idea

trick: 求和式的性质;分类讨论;缩小问题规模

裂项相消:

i!×i=(i+1)!−i!i! \times i = (i+1)! - i!

即

∑i=1n(i!×i)=(i+1)!−1\sum_{i=1}^n (i! \times i) = (i+1)! - 1

又模数 M=998224353=7×9×661×23971M = 998224353 = 7 \times 9 \times 661 \times 23971,因此当 (n+1)≥23971(n+1) \ge 23971 时,答案必然为 M−1M-1。nn 较小时暴力计算即可。

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

constexpr long long MODER = 998224353;

int main() {
	long long n;
	cin>>n;
	if (n >= 23972) cout<<(MODER-1)<<endl;
	else {
		long long ans = 0ll, fact = 1ll;
		for (int i = 1; i <= n; i++) {
			fact = (fact * i) % MODER;
			ans = (ans + (i * fact % MODER)) % MODER; 
		}
		cout<<ans<<endl;
	}
	return 0;
}

[G] Tree and Chain

trick: 树上状态设计。

50 分做法

保证原图为一棵树时,原图为若干条链的充要条件为:

  • 每个点的度数均不超过 22。

因此暴力枚举 2n−12^{n-1} 种删边方式即可。

满分做法

设 dpi,sdp_{i,s} 表示节点 ii 保留 ss 个儿子的方案,其中 s∈{0,1,2}s \in \{0,1,2\}。则显然有下面转移:

dpi,0=∑son∈sonsmin⁡(dpson,0,dpson,1,dpson,2)+wdpi,1=min⁡s∈sons(∑t≠s(min⁡(dpt,0,dpt,1,dpt,2)+w)+min⁡(dps,0,dps,1))dpi,2=min⁡r,s∈sons(∑t∉{r,s}(min⁡(dpt,0,dpt,1,dpt,2)+w)+min⁡(dps,0,dps,1)+min⁡(dpr,0,dpr,1))dp_{i,0} = \sum_{son \in sons} {\min(dp_{son,0},dp_{son,1},dp_{son,2}) + w} \\ dp_{i,1} = \min_{s \in sons}({\sum_{t \ne s}}(\min(dp_{t,0},dp_{t,1},dp_{t,2}) + w) + \min(dp_{s,0},dp_{s,1})) \\ dp_{i,2} = \min_{r,s \in sons}({\sum_{t \notin \{r,s\}}}(\min(dp_{t,0},dp_{t,1},dp_{t,2}) + w) + \min(dp_{s,0},dp_{s,1}) + \min(dp_{r,0},dp_{r,1}))

对每个结点存储最小值和次小值即可转移。

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

inline void train() {
	   ios::sync_with_stdio(false);
	   cin.tie(0);
	   cout.tie(0);
}

inline long long mini(long long a, long long b) {
	return a < b ? a : b;
}

constexpr int N = 2e5+4;
struct edge {
	int to;
	long long weight;
	edge() {
		
	}
	edge(int to, long long weight) : to(to), weight(weight) {
		
	}
};
vector<edge> tree[N];
long long dp[N][3];
constexpr long long INF = 3e16;
int n;

void dfs(int cur, int fa) {
	
	long long resv1 = LLONG_MAX, resv2 = LLONG_MAX;
	int resid1 = 0, resid2 = 0, sonsize = 0;
//	long long resw1 = 0, resw2 = 0;
	long long total = 0ll;
	
	for (auto &it : tree[cur]) {
		int i = it.to;
		if (i == fa) continue;
		sonsize++;
		dfs(i, cur);
		long long shall = mini(mini(dp[i][0], dp[i][1]), dp[i][2]) + it.weight;
		long long connected = mini(dp[i][0], dp[i][1]) - shall;
		if (connected <= resv1) {
			resv2 = resv1;
			resid2 = resid1;
//			resw2 = resw1;
			resv1 = connected;
			resid1 = i;
//			resw1 = it.weight;
		} else if (connected <= resv2) {
			resv2 = connected;
			resid2 = i;
//			resw2 = it.weight;
		}
		total += shall;
	}
	dp[cur][0] = total;
	if (sonsize >= 1) {
		assert(resid1);
		dp[cur][1] = total + resv1;
	} else dp[cur][1] = INF;
	if (sonsize >= 2) {
		assert(resid1 && resid2);
		dp[cur][2] = total + resv1 + resv2;
	} else dp[cur][2] = INF;
//	cerr << "dp[" << cur << "] = " << dp[cur][0] << "," << dp[cur][1] << "," << dp[cur][2] << endl;
}

int main() {

	train();
	
	cin>>n;
	for (int i = 0; i < n-1; i++) {
		int a,b;
		long long w;
		cin>>a>>b>>w;
		tree[a].push_back(edge(b,w)); tree[b].push_back(edge(a,w));
	}
	dfs(1, 1);
	cout<<mini(mini(dp[1][0], dp[1][1]), dp[1][2])<<endl;
	
	//cout<<flush;

	return 0;
}

[H] Second Best Position

trick: 类 KMP 时间复杂度(线性复杂度跳转)

下面仅讨论求 jj。求 ll 就是把求 jj 的过程倒过来。

令 pre1,ipre_{1,i} 表示 ii 对应的 kk,pre2,ipre_{2,i} 表示 ii 对应的 jj。容易用单调栈求出 pre1,ipre_{1,i}。

对于 pre2,ipre_{2,i},可以先令 pre2,i←pre1,i−1pre_{2,i} \leftarrow pre_{1,i} - 1,然后执行 pre2,i←pre1,pre2,ipre_{2,i} \leftarrow pre_{1,pre_{2,i}} 直到 apre2,i>aia_{pre_{2,i}} > a_i。

  • 正确性:当前位置如果不满足要求,必然找到上一个比当前位置较大的位置,才可能满足要求。
  • 复杂度:由 KMP 复杂度的证明,每个位置至多被跳到 22 次,故该操作的复杂度为线性。

事实上 pre1,ipre_{1,i} 也可以用这种方式求解。

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

inline void train() {
	   ios::sync_with_stdio(false);
	   cin.tie(0);
	   cout.tie(0);
}

inline int maxi(int a, int b) {
	return a > b ? a : b;
}

inline int mini(int a, int b) {
	return a < b ? a : b;
}

constexpr long long MODER = 998244353;
constexpr int N = 1e6+4;
int a[N], pre1[N], pre2[N], suf1[N], suf2[N], n;

inline void subgroup() {
	cin>>n;
	for (int i = 1; i <= n; i++) cin>>a[i];
	for (int i = 1; i <= n; i++) {
		pre1[i] = i-1;
		while (pre1[i] && a[pre1[i]] <= a[i]) {
			pre1[i] = pre1[pre1[i]];
		}
//		cerr << "pre1[" << i << "] = " << pre1[i] << endl;
		pre2[i] = maxi(0, pre1[i] - 1);
		while (pre2[i] && a[pre2[i]] <= a[i]) {
			pre2[i] = pre1[pre2[i]];
		} 
//		cerr << "pre2[" << i << "] = " << pre2[i] << endl;
	}
	suf1[n+1] = suf2[n+1] = n+1;
	for (int i = n; i >= 1; i--) {
		suf1[i] = i+1;
		while (suf1[i] <= n && a[suf1[i]] <= a[i]) {
			suf1[i] = suf1[suf1[i]];
		}
//		cerr << "suf1[" << i << "] = " << suf1[i] << endl;
//		suf2[i] = suf1[suf1[i]];
		suf2[i] = mini(suf1[i] + 1, n+1);
		while (suf2[i] <= n && a[suf2[i]] <= a[i]) {
			suf2[i] = suf1[suf2[i]];
		}
//		cerr << "suf2[" << i << "] = " << suf2[i] << endl;
	}
	for (int i = 1; i <= n; i++) {
		if (pre2[i] == 0) cout<<"-1 ";
		else cout<<pre2[i]<<' ';
	}
	cout<<'\n';
	for (int i = 1; i <= n; i++) {
		if (suf2[i] == (n+1)) cout<<"-1 ";
		else cout<<suf2[i]<<' ';
	}
	cout<<endl;
}


int main() {

	train();
	subgroup();
	//cout<<flush;

	return 0;
}

[I] Data Structure

trick: 位运算的性质。

码量最大的一道题。本题有很多做法。

做法 1

300 分做法

用韦恩图或真值表可以得到下面的式子成立:

aand⁡(bxor⁡c)=(aand⁡b)xor⁡(aand⁡c)a \operatorname{and} (b \operatorname{xor} c) = (a \operatorname{and} b) \operatorname{xor} (a \operatorname{and} c)

即类似分配律。此外,显然这两个运算满足交换律和结合律。因此,可以把与运算看成“乘法”,异或看作“加法”,然后套线段树 2 的板子。

需要注意的细节是“乘以 11”应当改成“与上 261−12^{61} - 1”。

然后缝暴力,即可获得 300300 分。

满分做法

接下来解决或运算的问题。可以观察到,或运算相当于强制将区间内给定的二进制位设为 11。容易想到下面的操作:

  • 先用与运算将二进制位全部设为 00。
  • 再用异或运算,由于二进制位已经全部设为 00,此时相应二进制位一定会设为 11。其余二进制位不受影响。
C++
#include <iostream>
#include <memory.h>
using namespace std;

inline void train() {
	   ios::sync_with_stdio(false);
	   cin.tie(0);
	   cout.tie(0);
}

#define int long long
constexpr long long N = 4e6+3;

long long q,m,x,y,k;
int n,op;
constexpr long long RANGEMAX = (1ll<<62)-1ll;
//long long val[N];
long long dataof[N], ltimer[N], ladder[N];
int lof[N], rof[N];	// -1: node does not exist

void build(int l, int r, int cur = 1) {
	if (l > r) return;
	lof[cur] = l;
	rof[cur] = r;
	ltimer[cur] = RANGEMAX;	// Fulfilling all bits
	ladder[cur] = 0;
	if (l == r) {
		cin >> dataof[cur];
	} else {
		int mid = (l+r)>>1;
		build(l, mid, cur<<1);
		build(mid+1, r, cur<<1|1);
		dataof[cur] = dataof[cur<<1] ^ dataof[cur<<1|1];
	}
}

template <typename Ty>
Ty maxi(const Ty& a, const Ty& b) {
	return a>b?a:b;
} 

template <typename Ty>
Ty mini(const Ty& a, const Ty& b) {
	return a<b?a:b;
} 

// [l, r] seg tree.

inline void dispatch(int l, int r, int cur) {
	if (lof[cur] < rof[cur]) {

		// HINT: When you can't evalute when it merges, you should
		// dispatch tags
		// Subtree exists
		dataof[cur<<1] = (dataof[cur<<1] & ltimer[cur]);
		dataof[cur<<1|1] = (dataof[cur<<1|1] & ltimer[cur]);

		// Add current situation rather than total situation
		ltimer[cur<<1] = (ltimer[cur] & ltimer[cur<<1]);
		ltimer[cur<<1|1] = (ltimer[cur] & ltimer[cur<<1|1]);
		 
		ladder[cur << 1] = (ltimer[cur] & ladder[cur << 1]);
		ladder[cur << 1 | 1] = (ltimer[cur] & ladder[cur << 1 | 1]);

		ladder[cur<<1] = (ladder[cur<<1] ^ ladder[cur]);	// Will this actually work?
		ladder[cur<<1|1] = (ladder[cur<<1|1] ^ ladder[cur]);
		
//		lor[cur<<1] |= lor[cur]; lor[cur<<1|1] |= lor[cur];

		long long dis1 = rof[cur<<1] - lof[cur<<1] + 1;
		if (dis1&1) dataof[cur<<1] = (dataof[cur<<1] ^ ladder[cur]);
		
		long long dis2 = rof[cur<<1|1] - lof[cur<<1|1] + 1;
		if (dis2&1) dataof[cur<<1|1] = (dataof[cur<<1|1] ^ ladder[cur]);

		ltimer[cur] = RANGEMAX;
		ladder[cur] = 0;
	}
}

// method: whether is timing
// Adders should have been multiplied
void update(int l, int r, char method, long long val, int cur = 1) {
	if (l > r) return;
	// Update all "included" parts:
	// You don't have to evalute the actual value
	if (lof[cur] >= l && rof[cur] <= r) {
		if (method == 1) {
			ltimer[cur] = (ltimer[cur] & val);// % m;
			ladder[cur] = (ladder[cur] & val);// % m;
			dataof[cur] = (dataof[cur] & val);// % m;	// Can perform multiplier.
			// Otherwise, you should dynamicly get the value: through the multiplier tag?
		} else {
			// Completely inside, so can directly work
			// SINGLE ONLY
			ladder[cur] = (ladder[cur] ^ val);// % m;
			long long dis = rof[cur]-lof[cur]+1;
			if (dis&1) dataof[cur] ^= val;
		}
	} else {
		int mid = (lof[cur] + rof[cur]) >> 1;
		dispatch(l, r, cur);
		update(maxi(l, lof[cur]), mini(mid, r), method, val, cur << 1);
		update(maxi(l, mid+1), mini(r, rof[cur]), method, val, cur << 1 | 1);
		// Pushup is here!
		dataof[cur] = (dataof[cur << 1] ^ dataof[cur << 1 | 1]);
	}
}

// There's no need to dispatch at all?
int query(int l, int r, int cur = 1) {
	if (l > r) return 0;	// Change it as 'No-merging'
	if (lof[cur] >= l && rof[cur] <= r) return dataof[cur];
	else dispatch(l, r, cur);	// You have to know the exact value
	int mid = (lof[cur] + rof[cur]) >> 1;
	int lresult = query(maxi(l, lof[cur]), mini(mid, r), cur << 1);
	int rresult = query(maxi(mid + 1, l), mini(r, rof[cur]), cur << 1 | 1);
	//dataof[cur] = (dataof[cur << 1] + dataof[cur << 1 | 1]) % m;
	return (lresult ^ rresult);	// Actual scope is SMALLER THAN indication scope!
}

signed main() {
	memset(lof, -1, sizeof(lof));
	memset(rof, -1, sizeof(rof));
	train();
//	int n,q,m;
	cin>>n>>q; 
	build(1, n);
	for (int i = 0; i < q; i++) {
		cin>>op>>x>>y;
		if (op != 4) {
			cin>>k;
			if (op == 2) {
				update(x, y, 1, RANGEMAX ^ k);
				update(x, y, 3, k);
			}
			else {
				update(x, y, op, k);
			}
			
		} else {
			cout<<query(x,y)<<'\n';
		}
	}
		
	cout<<flush;

	return 0;
}

做法 2

由 __Seniorious__ 提供。

看到这个做法,突然觉得应当给 232^3 种部分分。

维护三个标记(与运算,或运算,异或运算)。强制规定,当三个标记都存在时,按照异或 xx、或 oo、与 aa 的顺序进行操作(因为异或最难处理,其它容易覆盖)。显然,与运算初始值为 inf⁡=261−1\inf = 2^{61} - 1,其余为 00。

接下来,按照下面的方法处理标记:

  • 与操作时,o←oand⁡v,a←aand⁡vo \leftarrow o \operatorname{and} v, a \leftarrow a \operatorname{and} v。

  • 或操作时,o←oor⁡v,a←aor⁡vo \leftarrow o \operatorname{or} v, a \leftarrow a \operatorname{or} v。

  • 异或操作最难维护。这时:

    • x←xxor⁡vx \leftarrow x \operatorname{xor} v;
    • 原先被与操作清除的部分,如果与 vv 有交集,则用或操作加入;
    • 原先被或操作加入的部分,如果与 vv 有交集,则用与操作清除。
C++
/**
 * @author : cyb1010
 * @date   : 2024-11-17 14:00:06
 * @file   : U_504760_ElisaOI_R_3_I_Data_Structure.cpp
 */
#include <bits/stdc++.h>
using namespace std;
#define fo(s) freopen(s ".in", "r", stdin), freopen(s ".out", "w", stdout)
#define fi first
#define se second
#define ALL(v) (v).begin(), (v).end()
#define SZ(v) int(v.size())
typedef double db;
typedef long double ldb;
typedef long long ll;
typedef unsigned long long ull;
void ckmx(int &x, int y) { x < y && (x = y); }
void ckmn(int &x, int y) { x > y && (x = y); }
const ll inf = (1ll << 61) - 1;
const int N = 1000010;
int __, n, m;
ll a[N], V;
struct SGT {
    ll at, ot, xt, val;
} t[N << 2];
void psu(int p) { t[p].val = (t[p << 1].val ^ t[p << 1 | 1].val); }
void build(int p, int l, int r) {
    t[p].at = inf;
    if (l == r) return t[p].val = a[l], void();
    int mid = l + r >> 1;
    build(p << 1, l, mid), build(p << 1 | 1, mid + 1, r), psu(p);
}
void AT(int p, ll v) { t[p].at &= v, t[p].ot &= v, t[p].val &= v; }
void OT(int p, int len, ll v) {
    t[p].at |= v, t[p].ot |= v;
    (len & 1) ? (t[p].val |= v) : (t[p].val &= (inf ^ v));
}
void XT(int p, int len, ll x) {
    t[p].xt ^= x, (len & 1) && (t[p].val ^= x);
    ll va = (t[p].at ^ inf) & x, vo = (t[p].ot & x) ^ inf;
    t[p].ot |= va, t[p].at |= va;
    t[p].at &= vo, t[p].ot &= vo;
}
void psd(int p, int l, int r) {
    int mid = l + r >> 1;
    l = mid - l + 1, r -= mid;
    XT(p << 1, l, t[p].xt), XT(p << 1 | 1, r, t[p].xt), t[p].xt = 0;
    OT(p << 1, l, t[p].ot), OT(p << 1 | 1, r, t[p].ot), t[p].ot = 0;
    AT(p << 1, t[p].at), AT(p << 1 | 1, t[p].at), t[p].at = inf;
    assert(t[p].val == (t[p << 1].val ^ t[p << 1 | 1].val));
}
void A(int p, int l, int r, int le, int ri, ll v) {
    if (l > ri || r < le) return;
    if (l >= le && r <= ri) return AT(p, v);
    int mid = l + r >> 1;
    psd(p, l, r), A(p << 1, l, mid, le, ri, v);
    A(p << 1 | 1, mid + 1, r, le, ri, v), psu(p);
}
void O(int p, int l, int r, int le, int ri, ll v) {
    if (l > ri || r < le) return;
    if (l >= le && r <= ri) return OT(p, r - l + 1, v);
    int mid = l + r >> 1;
    psd(p, l, r), O(p << 1, l, mid, le, ri, v);
    O(p << 1 | 1, mid + 1, r, le, ri, v), psu(p);
}
void X(int p, int l, int r, int le, int ri, ll v) {
    if (l > ri || r < le) return;
    if (l >= le && r <= ri) return XT(p, r - l + 1, v);
    int mid = l + r >> 1;
    psd(p, l, r), X(p << 1, l, mid, le, ri, v);
    X(p << 1 | 1, mid + 1, r, le, ri, v), psu(p);
}
ll qry(int p, int l, int r, int le, int ri) {
    if (l > ri || r < le) return 0;
    if (l >= le && r <= ri) return t[p].val;
    int mid = l + r >> 1;
    psd(p, l, r);
    return qry(p << 1, l, mid, le, ri) ^ qry(p << 1 | 1, mid + 1, r, le, ri);
}
int main() {
    // fo("U_504760_ElisaOI_R_3_I_Data_Structure");
    cin.tie(0)->sync_with_stdio(0);
    cin >> n >> m;
    for (int i = 1; i <= n; i++) cin >> a[i];
    build(1, 1, n);
    for (int op, l, r; m--;) {
        cin >> op >> l >> r;
        if (op == 1) cin >> V, A(1, 1, n, l, r, V);
        if (op == 2) cin >> V, O(1, 1, n, l, r, V);
        if (op == 3) cin >> V, X(1, 1, n, l, r, V);
        if (op == 4) cout << qry(1, 1, n, l, r) << '\n';
    }
    return 0 ^ __ ^ 0;
}

[J] Polygon Generator

trick: 极限思维。

原题 [HNYR-01 D] 多边形,题解传送门。

感谢我的初中同班同学。

[K] Xuejun Geometry

trick: 寻找守恒量。

显然,经过若干次平移和旋转操作后,所得点集组成的图形和原图形全等。

考虑我们有 SSS(三边长度对应相等)判断三角形全等。对于多个点的图形,我们可以将其拆为若干个三角形,判断每个三角形是否全等,也就是计算任意两点对之间距离组成的可重集 SA,SBS_A,S_B。如果 SA=SBS_A=S_B,我们就认为两个图形全等。

注意,允许 10−210^{-2} 的坐标误差相当于允许 10−410^{-4} 的距离误差。

实现 1

标算实现。

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

int n;

struct coord {
	double x,y;
};

constexpr int N = 5003;
coord a[N], b[N];

multiset<double> ta, tb;

inline double pow2(double x) {
	return x*x; 
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	
	cin>>n;
	for (int i = 1; i <= n; i++) {
		cin>>a[i].x>>a[i].y;
	}
	for (int i = 1; i <= n; i++) {
		cin>>b[i].x>>b[i].y;
	}
	for (int i = 1; i <= n; i++) {
		for (int j = i+1; j <= n; j++) {
			ta.insert(sqrt(pow2(a[i].y-a[j].y)+pow2(a[i].x-a[j].x)));
			tb.insert(sqrt(pow2(b[i].y-b[j].y)+pow2(b[i].x-b[j].x)));
		}
	}
	auto fa = ta.begin(), fb = tb.begin();
	for (; fa != ta.end(); fa++, fb++) {
		if (fabs((*fa) - (*fb)) > 1e-4) {
			cout<<"No"<<endl;
			return 0;
		}
	}
	cout<<"Yes"<<endl;
	return 0;
}

实现 2(仅供参考)

由 xurui2007 提供(不是正解)。

把 multiset 改成哈希,暴力调参即可。

  • Bonus: 把这个做法 hack 掉的方式为把两点间距离尽可能靠近。
C++
#include<bits/stdc++.h>
using namespace std;
long long n;
__int128 ha=0,hb=0;
__int128 haa=0,hbb=0;
struct p {
	long double x,y;
} a[1010],b[1010];
long double dis(long double x,long double y,long double xx,long double yy) {
	return sqrt((x-xx)*(x-xx)+(y-yy)*(y-yy));
}
int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin>>n;
	for(long long i=1; i<=n; i++) {
		cin>>a[i].x>>a[i].y;
		a[i].x=((__int128)(a[i].x*10000000))*1.0/10000000;
		a[i].y=((__int128)(a[i].y*10000000))*1.0/10000000;
	}
	for(long long i=1; i<=n; i++) {
		cin>>b[i].x>>b[i].y;
		b[i].x=((__int128)(b[i].x*10000000))*1.0/10000000;
		b[i].y=((__int128)(b[i].y*10000000))*1.0/10000000;
	}
	for(long long i=1; i<=n; i++) {
		for(long long j=i+1; j<=n; j++) {
			haa^=(__int128)(dis(a[i].x,a[i].y,a[j].x,a[j].y)-0.472);
			ha^=(__int128)(dis(a[i].x,a[i].y,a[j].x,a[j].y)-0.35);
		}
	}
	for(long long i=1; i<=n; i++) {
		for(long long j=i+1; j<=n; j++) {
			hbb^=(__int128)(dis(b[i].x,b[i].y,b[j].x,b[j].y)-0.472);
			hb^=(__int128)(dis(b[i].x,b[i].y,b[j].x,b[j].y)-0.35);
		}
	}
	if(ha==hb||haa==hbb) cout<<"Yes";
	else cout<<"No";
	return 0;
}

相关推荐

[REOI436 Day1 T2] Binary Computer 更多位运算技巧

原文