跳到正文

个人记录

题解:AT_abc419_d [ABC419D] Substr Swap

Clouds_dream约 2 分钟
原文
文章信息与版本

文章信息

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

版本

  1. v1当前

个人感觉 D 比 C 简单。 本题可以用 差分 来做。

题目大意

题目传送门

题目分析

如果你暴力

时间复杂度为 O(nm)O(nm),直接 TLE。

我们可以使用差分数组来记录翻转操作,然后通过前缀异或来计算每个位置的翻转状态。

代码实现

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

using namespace std;

#define endl '\n'

#define int long long

#define fst ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);

const int P = 998244353;

const int INF = 0x3f3f3f3f3f3f3f3f;

const int N = 1e6+10, M = 1e8 + 10;
int n,m;
string s,t;
int ch[N];// 差分数组
int cnt=0;

signed main()
{
	fst
	cin>>n>>m;
	cin>>s>>t;
	for(int i=1;i<=m;i++){
		int l,r;
		cin>>l>>r;
		ch[l]^=1;// 起点
		if(r+1<=n)ch[r+1]^=1;// 终点
	}
	for(int i=1;i<=n;i++){
		cnt^=ch[i];// 当前状态
		if(cnt)cout<<t[i-1];
		else cout<<s[i-1];
	}
	return 0;
}
原文