Problem p03527 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

int main() {
  string s;
  cin >> s;
  int m = s.size();
  int n;
  cin >> n;
  vector<int> l(n), r(n);
  vector<vector<int>> sg(m);
  for (int i = 0; i < n; ++i) {
    cin >> l[i] >> r[i];
    --l[i], --r[i];
    if (m % 2 == 0) {
      if (l[i] < m / 2 && r[i] >= m / 2) {
        int g = min(m / 2 - l[i], r[i] - m / 2 + 1);
        int tl = m / 2 - g, tr = m / 2 + g - 1;
        if (tl == l[i] && tr == r[i])
          continue;
        if (tl == l[i])
          l[i] = tr + 1;
        else
          r[i] = tl - 1;
      }
    } else {
      if (l[i] <= m / 2 && r[i] >= m / 2) {
        int g = min(m / 2 - l[i], r[i] - m / 2);
        int tl = m / 2 - g, tr = m / 2 + g;
        if (tl == l[i] && tr == r[i])
          continue;
        if (tl == l[i])
          l[i] = tr + 1;
        else
          r[i] = tl - 1;
      }
    }
    // printf("l = %d r = %d\n", l[i], r[i]);
    if (r[i] < m / 2)
      sg[l[i]].push_back(r[i]);
    else
      sg[m - 1 - r[i]].push_back(m - 1 - l[i]);
  }
  for (int i = 0; i < m / 2; ++i) {
    if (sg[i].empty())
      continue;
    sort(sg[i].begin(), sg[i].end());
    sg[i].resize(unique(sg[i].begin(), sg[i].end()) - sg[i].begin());
    int r = sg[i][0];
    for (int j = 1; j < sg[i].size(); ++j)
      sg[r + 1].push_back(sg[i][j]);
    sg[i].resize(1);
  }
  vector<int> t(m + 1);
  int sum = 0;
  for (int i = 0; i < m / 2; ++i) {
    sum += t[i];
    int ch = ((((s[i] - 'a') + sum) % 26) + 26) % 26;
    int diff = ((s[m - 1 - i] - 'a') - ch + 26) % 26;
    // printf("i = %d diff = %d\n", i, diff);
    if (diff != 0 && sg[i].empty()) {
      puts("NO");
      return 0;
    }
    if (diff != 0) {
      sum += diff;
      t[sg[i][0] + 1] += 26 - diff;
    }
  }
  puts("YES");
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int main() {
  string s;
  cin >> s;
  int m = s.size();
  int n;
  cin >> n;
  vector<int> l(n), r(n);
  vector<vector<int>> sg(m);
  for (int i = 0; i < n; ++i) {
    cin >> l[i] >> r[i];
    --l[i], --r[i];
    if (m % 2 == 0) {
      if (l[i] < m / 2 && r[i] >= m / 2) {
        int g = min(m / 2 - l[i], r[i] - m / 2 + 1);
        int tl = m / 2 - g, tr = m / 2 + g - 1;
        if (tl == l[i] && tr == r[i])
          continue;
        if (tl == l[i])
          l[i] = tr + 1;
        else
          r[i] = tl - 1;
      }
    } else {
      if (l[i] <= m / 2 && r[i] >= m / 2) {
        int g = min(m / 2 - l[i], r[i] - m / 2);
        int tl = m / 2 - g, tr = m / 2 + g;
        if (tl == l[i] && tr == r[i])
          continue;
        if (tl == l[i])
          l[i] = tr + 1;
        else
          r[i] = tl - 1;
      }
    }
    // printf("l = %d r = %d\n", l[i], r[i]);
    if (r[i] < m / 2)
      sg[l[i]].push_back(r[i]);
    else
      sg[m - 1 - r[i]].push_back(m - 1 - l[i]);
  }
  for (int i = 0; i < m / 2; ++i) {
    if (sg[i].empty())
      continue;
    sort(sg[i].begin(), sg[i].end());
    sg[i].resize(unique(sg[i].begin(), sg[i].end()) - sg[i].begin());
    for (int j = 0; j + 1 < sg[i].size(); ++j)
      sg[sg[i][j] + 1].push_back(sg[i][j + 1]);
    sg[i].resize(1);
  }
  vector<int> t(m + 1);
  int sum = 0;
  for (int i = 0; i < m / 2; ++i) {
    sum += t[i];
    int ch = ((((s[i] - 'a') + sum) % 26) + 26) % 26;
    int diff = ((s[m - 1 - i] - 'a') - ch + 26) % 26;
    // printf("i = %d diff = %d\n", i, diff);
    if (diff != 0 && sg[i].empty()) {
      puts("NO");
      return 0;
    }
    if (diff != 0) {
      sum += diff;
      t[sg[i][0] + 1] += 26 - diff;
    }
  }
  puts("YES");
}