Problem p02599 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define int int
#define ff first
#define ss second
#define pb push_back

int bexp(int a, int b, int mod) {
  int res = 1;
  if (mod <= 0)
    return 0;

  while (b) {
    if (b & 1)
      res = (res * a) % mod;

    a = (a * a) % mod;
    b >>= 1;
  }
  return res;
}

#define N 500010
#define MOD 1000000007

int gcd(int a, int b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

vector<int> seg[4 * N];
int a[N], b[N];
int n, q;

void build(int node, int l, int r) {
  if (l == r) {
    seg[node] = vector<int>(1, a[l]);
  } else {
    int mid = l + r >> 1;
    build(2 * node, l, mid);
    build(2 * node + 1, mid + 1, r);

    merge(seg[2 * node].begin(), seg[2 * node].end(), seg[2 * node + 1].begin(),
          seg[2 * node + 1].end(), back_inserter(seg[node]));
  }
}

int R;

int query(int node, int l, int r, int x, int y) {
  if (x > y)
    return 0;

  if (l == x && r == y) {
    int k = abs(distance(seg[node].end(),
                         upper_bound(seg[node].begin(), seg[node].end(), R)));
    return k;
  }

  int mid = l + r >> 1;
  return query(2 * node, l, mid, x, min(mid, y)) +
         query(2 * node + 1, mid + 1, r, max(mid + 1, x), y);
}

int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.precision(10);

  cin >> n >> q;

  for (int i = 1; i <= n; ++i) {
    cin >> b[i];
  }

  map<int, int> mi;

  for (int i = n; i > 0; --i) {
    a[i] = (mi[b[i]] == 0 ? INT_MAX : mi[b[i]]);
    mi[b[i]] = i;
  }

  // for (int i = 1; i <= n; ++i)
  // {
  //     cout << a[i] << " ";
  // }
  build(1, 1, n);

  int l, r;
  for (int i = 0; i < q; ++i) {
    cin >> l >> r;
    R = r;
    cout << query(1, 1, n, l, r) << "\\n";
  }
  return 0;
}


Error: TLE

Fixed submission:

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

#define int int
#define ff first
#define ss second
#define pb push_back

int bexp(int a, int b, int mod) {
  int res = 1;
  if (mod <= 0)
    return 0;

  while (b) {
    if (b & 1)
      res = (res * a) % mod;

    a = (a * a) % mod;
    b >>= 1;
  }
  return res;
}

#define N 500010
#define MOD 1000000007

int gcd(int a, int b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

vector<int> seg[4 * N];
int a[N], b[N];
int n, q;

void build(int node, int l, int r) {
  if (l == r) {
    seg[node] = vector<int>(1, a[l]);
  } else {
    int mid = l + r >> 1;
    build(2 * node, l, mid);
    build(2 * node + 1, mid + 1, r);

    merge(seg[2 * node].begin(), seg[2 * node].end(), seg[2 * node + 1].begin(),
          seg[2 * node + 1].end(), back_inserter(seg[node]));
    sort(seg[node].begin(), seg[node].end());
  }
}

int R;

int query(int node, int l, int r, int x, int y) {
  if (x > y)
    return 0;

  if (l == x && r == y) {
    int k = abs(distance(seg[node].end(),
                         upper_bound(seg[node].begin(), seg[node].end(), R)));
    return k;
  }

  int mid = l + r >> 1;
  return query(2 * node, l, mid, x, min(mid, y)) +
         query(2 * node + 1, mid + 1, r, max(mid + 1, x), y);
}

int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.precision(10);

  cin >> n >> q;

  for (int i = 1; i <= n; ++i) {
    cin >> b[i];
  }

  map<int, int> mi;

  for (int i = n; i > 0; --i) {
    a[i] = (mi[b[i]] == 0 ? INT_MAX : mi[b[i]]);
    mi[b[i]] = i;
  }

  // for (int i = 1; i <= n; ++i)
  // {
  //     cout << a[i] << " ";
  // }
  build(1, 1, n);

  int l, r;
  for (int i = 0; i < q; ++i) {
    cin >> l >> r;
    R = r;
    cout << query(1, 1, n, l, r) << endl;
  }
  return 0;
}