1878D - Reverse Madness

  • if we look at [li,ri] as subsegments for each i, notice that they are disjoint

  • treat each interval as a seperate test case

  • the modifications x x x and $n−x+1 $ are equivalent

  • the modifications are symmetrical with respect to the middle of the string.

  • if a modification “touches” index i it also touches index n−i+1 , and also because of the symmetry, i will always be swapped with n−i+1 , and no other index.

    批注:可以理解为,一旦索引为 i 的位置需要进行 reverse,它一定是和索引为 n-i+1 的元素进行交换。

  • the order of modifications doesn’t matter

  • given index i , all modifications such that x≤i affect this index

    批注:这里的 affect 和上文的 touch 是一个意思

// Problem: D. Reverse Madness
// Contest: Codeforces - Codeforces Round 900 (Div. 3)
// URL: https://codeforces.com/contest/1878/problem/D
// Memory Limit: 256 MB
// Time Limit: 1000 ms

#include<iostream>
#include<algorithm>
#include<cstring>	// memset
#include<string>
#define x first
#define y second

using namespace std;
typedef pair<int, int> PII;

const int N = 200010;

int n, k;
string s;
int l[N], r[N];
int q;
int x[N];
int cnt[N];

int main(void) {
	int t;
	cin >> t;
	while (t -- ) {
		memset(cnt, 0, sizeof cnt);
		cin >> n >> k;
		cin >> s;
		for (int i = 0; i < k; i ++ ) {cin >> l[i]; l[i] -- ;}
		for (int i = 0; i < k; i ++ ) {cin >> r[i]; r[i] -- ;}
		cin >> q;
		for (int i = 0; i < q; i ++ ) {cin >> x[i]; x[i] -- ; cnt[x[i]] ++ ;}
		
		string res = "";
		for (int i = 0; i < k; i ++ ) {
			int sum = 0;
			string son = s.substr(l[i], r[i]-l[i]+1);
			for (int j = l[i]; j <= (l[i]+r[i])/2; j ++ ) {
				sum += (cnt[j] + cnt[(l[i]+r[i]) - j]);
				if (sum&1) {
					swap(son[j-l[i]], son[r[i]-j]); // 子串和母串存在一个偏移 l[i]
				}
			}
			res += son;
		}
		cout << res << endl;
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值