Language: C++

#include<cstdio>
#include<vector>
#include<cassert>
#include<string>
#include<algorithm>

using namespace std;

struct UnionFind{
	int par[100100];
	void init(int N){
		for(int i = 0; i < N; ++i){
			par[i] = i;
		}
	}
	int find(int x){
		if(x == par[x]) return x;
		return par[x] = find(par[x]);
	}
	void unite(int x, int y){
		x = find(x);
		y = find(y);
		if(x != y) par[x] = y;
	}
	bool same(int x, int y){
		return find(x) == find(y);
	}
};

UnionFind uf;

string ini;

//char S[100100], T[100100];
int target[100100];
int dif[100100];

int L[100100];
int R[100100];
int N;
int M;

int flip(int x){
	return min(x, (int)ini.size() + 1 - x);
}

vector<int> comps[26];

char ch[100100];

void input(){
	scanf("%s", ch);
	ini = ch;
	scanf("%d", &M);
	int k = ini.size() / 2;
	if(ini.size() % 2 == 1){
		string str = ini.substr(0, k) + ini.substr(k + 1, 100100);
		ini = str;
	}
	for(int i = 0; i < M; ++i){
		scanf("%d%d", L + i, R + i);
		if(R[i] >= k + 1) R[i]--;
		if(L[i] >= k + 1) L[i]--;
		if(L[i] == k && R[i] == k) L[i] = 1, R[i] = str.size();
	}
	N = ini.size();
}

int sum[100100];

pair<int, int> change(int l, int r){
	int k = (N + 1) / 2;
	bool odd = (N % 2 == 1);
	if(odd && l == k && r == k) return make_pair(-1, 0);
	if(r <= k){
		if(odd && (r == k)) r--;
		return make_pair(l, r);
	}
	else if(l <= k){
		int x = k - l + 1;
		int y = r - k;
		int mi = min(x, y);
		int ma = max(x, y);
		int l = k - ma + 1;
		int r = k - mi ;
		//printf("%d %d %d %d\n", mi, ma, l ,r);
		if(odd && r == k) r--;
		return make_pair(l, r);
	}
	else{
		l = flip(l);
		r = flip(r);
		swap(l, r);
		return make_pair(l, r);
	}
}

bool solve(){
	uf.init(N);
//	if(N % 2 == 1) assert(0);
	for(int i = 0; i < M; ++i){
		pair<int, int> tmp = change(L[i], R[i]);
		int l = tmp.first, r = tmp.second;
		if(l > r) continue;
		//printf("%d %d\n", l - 1, r);
		if(l == -1) continue;
		uf.unite(l - 1, r);
//		printf("%d %d\n", l - 1, r);
/*		int l = L[i], r = R[i];
		l--; r--;
		l = flip(l);
		r = flip(r);
		if(l > r) swap(l, r);
		l++; r++;
		if(N + 1 == r * 2) r--;*/
//		printf("%d %d\n", l, r);
	//	printf("uf %d %d\n", l, r);
//		uf.unite(l - 1, r);
	//	printf("%d %d\n", l - 1, r);
	}
	/*for(int i = 0; i < N; ++i){
		printf("%d ", uf.find(i));
	}
	printf("\n");
	fflush(stdout);*/
	for(int i = 0; i <= N; ++i){
		target[i] = -1;
		dif[i] = -1;
	}
	target[0] = 0;
	for(int i = 1; i < N + 1 - i; ++i){
		target[i] = (int)ini[N + 1 - i - 1] - ini[i - 1];
		if(target[i] < 0) target[i] += 26;
	//	printf("%d %d\n", target[i], i);
	}/*
	for(int i = 0; i + 1 <= N; ++i){
		if(target[i + 1] == -1) break;
		dif[i] = target[i + 1] - target[i];
		while(dif[i] < 0) dif[i] += 26;
		while(dif[i] >= 26) dif[i] -= 26;
	}
	for(int i = 0; i <= N; ++i) printf("%d %d d\n", dif[i], i);
	printf("\n");*/
	/*for(int i = 0; i <= N; ++i){
		if(dif[i] == -1) continue;
		comps[dif[i]].push_back(uf.find(i));
	}
	for(int i = 0; i < 26; ++i){
		sort(comps[i].begin(), comps[i].end());
		if(comps[i].size() == 0) continue;
		if(comps[i][0] != comps[i][comps[i].size() - 1]) return false;
	}
	return true;*/
	/*
	for(int i = 0; i <= N; ++i){
		if(target[i + 1] == -1) break;
		if(target[i + 1] != target[i]){
			printf("check %d %d\n", i,i + 1);
			if(uf.same(i, i + 1)){
				//ok
			}else{
				return false;
			}
		}
	}
	return true;*/
	int k = (N + 1) / 2;
	target[k + 1] = 0;
	int m = k;
	if(N % 2 == 1) m--;
	for(int i = 0; i <= m; ++i){
		sum[uf.find(i)] += (target[i + 1] - target[i]);
	}
	for(int i = 0; i < 100100; ++i){
		if(sum[i] % 26 != 0){
			if((N % 2 == 1)){
				if(uf.find(k - 1) == i) continue;
			}
			//printf("%d\n", i);
			return false;
		}
	}
	return true;
}

int main(){
	input();
	bool ans = solve();
	if(ans) printf("YES\n");
	else printf("NO\n");
	return 0;
}


Compiler output:
a.cc: In function 'void input()':
a.cc:64:61: error: 'str' was not declared in this scope; did you mean 'std'?
   64 |                 if(L[i] == k && R[i] == k) L[i] = 1, R[i] = str.size();
      |                                                             ^~~
      |                                                             std