Problem:
Score : 1000 points

Problem Statement

Ringo has a string S.

He can perform the following N kinds of operations any number of times in any order.

Operation i: For each of the characters from the L_i-th through the R_i-th characters in S, replace it with its succeeding letter in the English alphabet. (That is, replace a with b, replace b with c and so on.) For z, we assume that its succeeding letter is a.

Ringo loves palindromes and wants to turn S into a palindrome.
Determine whether this is possible.

Constraints

1 \leq |S| \leq 10^5

S consists of lowercase English letters.

1 \leq N \leq 10^5

1 \leq L_i \leq R_i \leq |S|

Input

Input is given from Standard Input in the following format:

S
N
L_1 R_1
L_2 R_2
:
L_N R_N

Output

Print YES if it is possible to turn S into a palindrome; print NO if it is impossible.

Sample Input 1

bixzja
2
2 3
3 6

Sample Output 1

YES

For example, if we perform Operation 1, 2 and 1 in this order, S changes as bixzja → bjyzja → bjzakb → bkaakb and becomes a palindrome.

Sample Input 2

abc
1
2 2

Sample Output 2

NO

Sample Input 3

cassert
4
1 2
3 4
1 1
2 2

Sample Output 3

YES

Solution in C++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>
#include <assert.h>
using namespace std;
int main() {
    std::ios::sync_with_stdio(false);
    cin.tie(0);
    
    string s;
    cin >> s;
    int n = s.length();
    int need[n];
    for(int i = 0; i<n; i++){
        int op = n-1-i;
        if(s[op]>=s[i]){
            need [i] = (int)(s[op]-s[i]);
        }
        else{
            need[i] = (int)('z'-s[i]+s[op]-'a')+1;
        }
    }
    bool check[n];
    int add[n];
    for(int i = 0; i<n; i++){
        check[i] = false;
        add[i] = 0;
    }
    int q;
    cin >> q;
    for(int i = 1; i<=q; i++){
        int l, r;
        cin >> l >> r;
        l--;
        r--;
        if(n%2==1 && l==n/2){
            if(l==r){
                continue;
            }
            l++;
        }
        if(n%2==1 && r==n/2){
            r--;
        }
        if(l>=n/2){
            int a = n-r-1;
            int b = n-l-1;
            l = a;
            r = b;
        }
        if(l<n/2 && r>=n/2){
            r = n-1-r;
            int a = min(l,r);
            int b = max(l,r);
            if(a==b){
                continue;
            }
            b--;
            l=a;
            r=b;
        }
        check[l] = true;
        check[r+1] = true;
        add[l]++;
        add[r+1]--;
    }
    bool ok = true;
    vector<int> now;
    int cur = 0;
    for(int i = 0; i<n/2; i++){
        if(check[i]){
            for(int j = 0; j<now.size(); j++){
                ok &= need[now[j]]==need[now[j]];
                ok &= (need[now[j]]==0 || cur>0);
            }
            now.clear();
        }
        now.push_back(i);
        cur += add[i];
    }
    for(int i = 0; i<now.size(); i++){
        ok &= need[now[i]]==need[now[i]];
        ok &= (need[now[i]]==0 || cur>0);
    }
    cout << (ok?"YES":"NO") << endl;
    // string s;
    // cin >> s;
    // int nn = s.length();
    // int need[nn];
    // for(int i = 0; i<nn/2; i++){
    //     int op = nn-1-i;
    //     if(s[i]<=s[op]){
    //         need[i] = (int)(s[op]-s[i]);
    //     }
    //     else{
    //         need[i] = (int)('z'-s[i]+s[op]-'a')+1;
    //     }
    // }
    // bool active[nn];
    // int n;
    // cin >> n;
    // int add[nn];
    // for(int i = 0; i<nn; i++){
    //     active[i] = false;
    //     add[i]=0;
    // }
    // for(int i = 0; i<n; i++){
    //     int l, r;
    //     cin >> l >> r;
    //     l--;
    //     r--;
    //     if(nn%2==1 && l==nn/2){
    //         if(r==l){
    //             continue;
    //         }
    //         l = nn/2+1;
    //     }
    //     if(nn%2==1 && r==nn/2){
    //         if(l==r){
    //             continue;
    //         }
    //         r--;
    //     }
    //     if(l>=nn/2){
    //         assert(nn%2==0 || l!=nn/2);
    //         int ol = nn-1-r;
    //         int orr = nn-1-l;
    //         active[ol] = true;
    //         add[ol]++;
    //         active[orr+1] = true;
    //         add[orr+1]--;
    //         continue;
    //     }
        
    //     if(r<nn/2){
    //         active[l] = true;
    //         active[r+1] = true;
    //         add[l]++;
    //         add[r+1]--;
    //         continue;
    //     }
    //     assert(l<nn/2 && r>=nn/2);
    //     r = nn-1-r;
    //     if(l==r){
    //         continue;
    //     }
    //     int a = min(l,r);
    //     int b = max(l,r)-1;
    //     l = a;
    //     r = b;
    //     active[l] = true;
    //     active[r+1] = true;
    //     add[l]++;
    //     add[r+1]--;
    // }
    // active[nn/2] = true;
    // vector<int> now;
    // bool ok = true;
    // int cur = 0;
    // for(int i = 0; i<=nn/2; i++){
    //     if(active[i]){
    //         for(int j = 0; j<now.size(); j++){
    //             ok&= need[now[j]]==need[now[0]];
    //             ok&= (need[now[j]]==0 || cur>0);
    //         }
    //         now.clear();
    //     }
    //     cur+=add[i];
    //     now.push_back(i);
    // }
    // assert(now.size()==1);
    // cout << (ok?"YES":"NO") <<endl;
    return 0;
}