Problem p03537 (C++) — buggy submission, verdict: Runtime Error

#pragma comment(linker, "/STACK:512000000")
#define _CRT_SECURE_NO_WARNINGS
/*#if !defined(YCM) && 0
#define _FORTIFY_SOURCE 0
#pragma GCC optimize("Ofast,no-stack-protector")
#pragma GCC target("avx,tune=native")
#include <stdio.h>
#endif*/
// #include "testlib.h"
#include <bits/stdc++.h>
using namespace std;

#define all(a) a.begin(), a.end()
typedef long long li;
typedef long double ld;
void solve(bool);
void precalc();
clock_t start;
int main() {
#ifdef AIM
  freopen("/home/alexandero/ClionProjects/ACM/input.txt", "r", stdin);
  // freopen("/home/alexandero/ClionProjects/ACM/output.txt", "w", stdout);
  // freopen("out.txt", "w", stdout);
#else
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
#endif
  start = clock();
  int t = 1;
  cout.sync_with_stdio(0);
  cin.tie(0);
  precalc();
  cout.precision(10);
  cout << fixed;
  // cin >> t;
  int testNum = 1;
  while (t--) {
    // cout << "Case #" << testNum++ << ": ";
    // cerr << testNum << endl;
    solve(true);
    // cerr << testNum - 1 << endl;
  }
  cout.flush();
#ifdef AIM1
  while (true) {
    solve(false);
  }
#endif

#ifdef AIM
  cerr << "\\n\\n time: " << (clock() - start) / 1.0 / CLOCKS_PER_SEC << "\\n\\n";
#endif

  return 0;
}

// BE CAREFUL: IS INT REALLY INT?

template <typename T> T binpow(T q, T w, T mod) {
  if (!w)
    return 1 % mod;
  if (w & 1)
    return q * 1LL * binpow(q, w - 1, mod) % mod;
  return binpow(q * 1LL * q % mod, w / 2, mod);
}

template <typename T> T gcd(T q, T w) {
  while (w) {
    q %= w;
    swap(q, w);
  }
  return q;
}
template <typename T> T lcm(T q, T w) { return q / gcd(q, w) * w; }

template <typename T> void make_unique(vector<T> &a) {
  sort(all(a));
  a.erase(unique(all(a)), a.end());
}

template <typename T> void relax_min(T &cur, T val) { cur = min(cur, val); }

template <typename T> void relax_max(T &cur, T val) { cur = max(cur, val); }

void precalc() {}

// #define int li
// const int mod = 1000000007;

void solve(bool read) {
  string s;
  if (read) {
    cin >> s;
  } else {
    s = string(1000, 'a');
  }
  int n;
  if (read) {
    cin >> n;
  } else {
    n = 1000;
  }
  int is_odd = (int)s.length() % 2;
  int half = (int)s.length() / 2;
  int len = (int)s.length();
  vector<pair<int, int>> segs;
  for (int i = 0; i < n; ++i) {
    int l, r;
    if (read) {
      cin >> l >> r;
    } else {
      do {
        l = rand() % n + 1;
        r = rand() % n + 1;
      } while (l > r);
    }
    --l;
    --r;
    if (l >= half) {
      l = len - 1 - l;
      r = len - 1 - r;
      swap(l, r);
    }
    if (l == half) {
      assert(is_odd);
      continue;
    }
    if (is_odd && r == half) {
      --r;
    }
    if (r < half) {
      segs.push_back({l, r});
      continue;
    }
    if (is_odd) {
      if (half - l < r - half) {
        l = len - 1 - l;
        r = len - 1 - r;
        swap(l, r);
      }
      segs.push_back({l, len - 1 - r - 1});
      continue;
    }
    if (half - 1 - l < r - half) {
      l = len - 1 - l;
      r = len - 1 - r;
      swap(l, r);
    }
    segs.push_back({l, len - 1 - r - 1});
  }

  vector<vector<int>> starts(half);
  vector<int> diff(half);
  for (int i = 0; i < half; ++i) {
    diff[i] = (s[i] + 26 - s[len - 1 - i]) % 26;
  }
  for (int cur = 0; cur < segs.size(); ++cur) {
    auto item = segs[cur];
    if (item.second >= item.first) {
      starts[item.first].push_back(item.second + 1);
    }
  }
  vector<int> add(half + 1, 0);

  for (int i = 0; i < half; ++i) {
    make_unique(starts[i]);
    for (int j = 1; j < starts[i].size(); ++j) {
      starts[starts[i][j - 1]].push_back(starts[i][j]);
    }
    if (i) {
      add[i] += add[i - 1];
    }
    int need = (diff[i] - add[i] + 26) % 26;
    if (need) {
      if (starts[i].empty()) {
        cout << "NO\\n";
        return;
      }
      add[i] += need;
      add[starts[i][0]] -= need;
    }
  }
  cout << "YES\\n";
}

Error: 0

Fixed submission:

#pragma comment(linker, "/STACK:512000000")
#define _CRT_SECURE_NO_WARNINGS
/*#if !defined(YCM) && 0
#define _FORTIFY_SOURCE 0
#pragma GCC optimize("Ofast,no-stack-protector")
#pragma GCC target("avx,tune=native")
#include <stdio.h>
#endif*/
// #include "testlib.h"
#include <bits/stdc++.h>
using namespace std;

#define all(a) a.begin(), a.end()
typedef long long li;
typedef long double ld;
void solve(bool);
void precalc();
clock_t start;
int main() {
#ifdef AIM
  freopen("/home/alexandero/ClionProjects/ACM/input.txt", "r", stdin);
  // freopen("/home/alexandero/ClionProjects/ACM/output.txt", "w", stdout);
  // freopen("out.txt", "w", stdout);
#else
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
#endif
  start = clock();
  int t = 1;
  cout.sync_with_stdio(0);
  cin.tie(0);
  precalc();
  cout.precision(10);
  cout << fixed;
  // cin >> t;
  int testNum = 1;
  while (t--) {
    // cout << "Case #" << testNum++ << ": ";
    // cerr << testNum << endl;
    solve(true);
    // cerr << testNum - 1 << endl;
  }
  cout.flush();
#ifdef AIM1
  while (true) {
    solve(false);
  }
#endif

#ifdef AIM
  cerr << "\\n\\n time: " << (clock() - start) / 1.0 / CLOCKS_PER_SEC << "\\n\\n";
#endif

  return 0;
}

// BE CAREFUL: IS INT REALLY INT?

template <typename T> T binpow(T q, T w, T mod) {
  if (!w)
    return 1 % mod;
  if (w & 1)
    return q * 1LL * binpow(q, w - 1, mod) % mod;
  return binpow(q * 1LL * q % mod, w / 2, mod);
}

template <typename T> T gcd(T q, T w) {
  while (w) {
    q %= w;
    swap(q, w);
  }
  return q;
}
template <typename T> T lcm(T q, T w) { return q / gcd(q, w) * w; }

template <typename T> void make_unique(vector<T> &a) {
  sort(all(a));
  a.erase(unique(all(a)), a.end());
}

template <typename T> void relax_min(T &cur, T val) { cur = min(cur, val); }

template <typename T> void relax_max(T &cur, T val) { cur = max(cur, val); }

void precalc() {}

// #define int li
// const int mod = 1000000007;

void solve(bool read) {
  string s;
  cin >> s;
  int n;
  cin >> n;
  int is_odd = (int)s.length() % 2;
  int half = (int)s.length() / 2;
  int len = (int)s.length();
  vector<pair<int, int>> segs;
  for (int i = 0; i < n; ++i) {
    int l, r;
    cin >> l >> r;
    --l;
    --r;
    if (l >= half) {
      l = len - 1 - l;
      r = len - 1 - r;
      swap(l, r);
    }
    if (l == half) {
      continue;
    }
    if (r <= half) {
      segs.push_back({l, min(r, half - is_odd)});
      continue;
    }
    if (is_odd) {
      if (half - l < r - half) {
        l = len - 1 - l;
        r = len - 1 - r;
        swap(l, r);
      }
      segs.push_back({l, len - 1 - r - 1});
      continue;
    }
    if (half - 1 - l < r - half) {
      l = len - 1 - l;
      r = len - 1 - r;
      swap(l, r);
    }
    segs.push_back({l, len - 1 - r - 1});
  }

  vector<vector<int>> starts(half);
  vector<vector<int>> ends(half + 1);
  vector<int> diff(half);
  for (int i = 0; i < half; ++i) {
    diff[i] = (s[i] + 26 - s[len - 1 - i]) % 26;
  }
  for (int cur = 0; cur < segs.size(); ++cur) {
    auto item = segs[cur];
    if (item.second >= item.first) {
      starts[item.first].push_back(cur);
      ends[item.second + 1].push_back(cur);
    }
  }

  set<pair<int, int>> a;
  for (int i = 0; i < half; ++i) {
    /*if (starts[i].empty() && ends[i].empty() && i > 0 && diff[i] != diff[i -
    1]) { cout << "NO\\n"; return;
    }*/
    for (int id : starts[i]) {
      a.insert({i, id});
    }
    int min_deleted = i;
    for (int id : ends[i]) {
      min_deleted = min(min_deleted, segs[id].first);
      a.erase({segs[id].first, id});
    }
    if (a.empty() && diff[i] != 0) {
      cout << "NO\\n";
      return;
    }
    if (!a.empty() && min_deleted > a.rbegin()->first && min_deleted &&
        diff[min_deleted - 1] != diff[i]) {
      cout << "NO\\n";
      return;
    }
  }

  cout << "YES\\n";
}