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

#include <bits/stdc++.h>

using namespace std;
#define ll long long
#define st first
#define nd second
#define pb push_back

const int maxn = (int)5e5;
const int R = 1024 * 512;

int c[maxn];
int tree[2 * R];
int lazy[2 * R];
int lastOccurr[maxn];
int occurrToLeft[maxn];
int ansToCout[maxn];
pair<int, int> queries[maxn];
pair<int, int> sorted[maxn];

void update(int id, int val) {
  tree[id] += val;
  lazy[id] += val;
}
void push(int id) {
  update(2 * id, lazy[id]);
  update(2 * id + 1, lazy[id]);
  lazy[id] = 0;
}
void add(int beg, int end, int id = 1, int l = 1, int r = R) {
  if (l > end || r < beg)
    return;
  if (beg <= l && r <= end) {
    update(id, 1);
    return;
  }
  push(id);
  int left = 2 * id;
  int right = 2 * id + 1;
  int mid = (l + r) / 2;
  add(beg, end, left, l, mid);
  add(beg, end, right, mid + 1, r);
  tree[id] = max(tree[left], tree[right]);
}
int giveAns(int beg, int end, int id = 1, int l = 1, int r = R) {
  if (l > end || r < beg)
    return 0;
  if (beg <= l && r <= end)
    return tree[id];
  push(id);
  int left = 2 * id;
  int right = 2 * id + 1;
  int mid = (l + r) / 2;
  return max(giveAns(beg, end, left, l, mid),
             giveAns(beg, end, right, mid + 1, r));
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n, q;
  cin >> n >> q;
  for (int i = 1; i <= n; i++)
    cin >> c[i];
  vector<vector<int>> querry(n + 1);
  for (int i = 1; i <= q; i++) {
    cin >> queries[i].first >> queries[i].second;
    querry[queries[i].second].push_back(i);
  }
  for (int i = 1; i <= n; i++)
    lastOccurr[i] = 0;
  for (int i = 1; i <= n; i++) {
    occurrToLeft[i] = lastOccurr[c[i]] + 1;
    lastOccurr[c[i]] = i;
  }
  sort(sorted + 1, sorted + q + 1);
  for (int i = 1; i <= n; i++) {
    add(occurrToLeft[i], i);
    for (int j = 0; j < (int)querry[i].size(); j++) {
      int prevI = querry[i][j];
      ansToCout[prevI] = giveAns(queries[prevI].first, n);
    }
  }
  for (int i = 1; i <= q; i++)
    cout << ansToCout[i] << '\\n';
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <bits/stdc++.h>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <string>
#include <vector>

using namespace std;

pair<int, int> T_z[200100], T_or[200100];
int T_l[200100], T_najb[200100], tree[524288], sts[200100];

bool comparison(const pair<int, int> &a, const pair<int, int> &b) {
  return ((a.first > b.first) ||
          ((a.first == b.first) && (a.second > b.second)));
}
void wstaw(int x, int indx, int drz[]) {
  drz[indx] = x;
  while (indx != 1) {
    indx /= 2;
    drz[indx] = drz[indx * 2] + drz[indx * 2 + 1];
  }
}
int suma_prz(int l, int p, int drz[]) {
  if (l == p)
    return drz[l];
  int res = drz[l] + drz[p];
  if (l % 2 == 0)
    res += drz[l + 1];
  if (p % 2 == 1)
    res += drz[p - 1];
  while (l / 2 / 2 != p / 2 / 2) {
    l /= 2;
    p /= 2;
    if (l % 2 == 0)
      res += drz[l + 1];
    if (p % 2 == 1)
      res += drz[p - 1];
  }
  return res;
}
int su_bin(int p1, int p2, int poc, int kon, pair<int, int> T_wys[]) {
  int res, sr;
  while (poc <= kon) {
    sr = (poc + kon) / 2;
    if (T_wys[sr].first < p1) {
      res = sr;
      kon = sr - 1;
      continue;
    }
    if ((T_wys[sr].first == p1) && (T_wys[sr].second <= p2)) {
      res = sr;
      kon = sr - 1;
      continue;
    }
    poc = sr + 1;
  }
  return res;
}

int main() {
  ios_base::sync_with_stdio(0);
  int n, q;
  cin >> n >> q;
  for (int i = 0; i < n; i++)
    cin >> T_l[i];
  for (int i = 0; i < q; i++) {
    cin >> T_z[i].first >> T_z[i].second;
    T_or[i].first = T_z[i].first;
    T_or[i].second = T_z[i].second;
  }
  sort(T_z, T_z + q, comparison);
  int pot;
  for (int i = 1;; i = i * 2) {
    if (i >= n) {
      pot = i;
      break;
    }
  }
  int kt_q = 0;
  bool prz = false;
  for (int i = n - 1; i >= 0; i--) {
    if (prz)
      break;
    wstaw(0, pot + T_najb[T_l[i]], tree);
    wstaw(1, pot + i, tree);
    T_najb[T_l[i]] = i;
    while (T_z[kt_q].first == i + 1) {
      sts[kt_q] =
          suma_prz(T_z[kt_q].first + pot - 1, T_z[kt_q].second + pot - 1, tree);
      kt_q++;
      if (kt_q == q) {
        prz = true;
        break;
      }
    }
  }
  for (int i = 0; i < kt_q; i++) {
    cout << sts[su_bin(T_or[i].first, T_or[i].second, 0, kt_q, T_z)] << "\\n";
  }
}