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

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

#define ll long long int
#define fi first
#define se second
#define pb push_back
#define all(a) (a).begin(), (a).end()
#define debug(a) cout << #a << " = " << a << endl;
#define endl "\\n"
#define MOD (int)1e9 + 7
#define inf (int)(1e18)
const int B = 700;

bool mycomp(pair<pair<ll, ll>, ll> a, pair<pair<ll, ll>, ll> b) {
  return a.first.first / B < b.first.first / B ||
         (a.first.first / B == b.first.first / B &&
          a.first.second < b.first.second);
}
const int N = 500005;
int mp[N] = {0};

void solve() {
  ll n, q;
  cin >> n >> q;
  ll a[n];
  for (long int i = 0; i < n; i++)
    cin >> a[i];

  vector<pair<pair<ll, ll>, ll>> v;
  for (long int i = 0; i < q; i++) {
    ll l, r;
    cin >> l >> r;
    l--, r--;
    v.pb({{l, r}, i});
  }
  sort(v.begin(), v.end(), mycomp);
  ll L = v[0].first.first;
  ll R = v[0].first.second, cnt = 0;
  ll ans[q];
  for (ll i = L; i <= R; i++) {
    mp[a[i]]++;
    if (mp[a[i]] == 1)
      cnt++;
  }
  ans[v[0].second] = cnt;
  for (ll i = 1; i < q; i++) {
    ll pL = v[i].first.first;
    ll pR = v[i].first.second;
    while (L < pL) {
      mp[a[L]]--;
      if (mp[a[L]] == 0)
        cnt--;
      L++;
    }
    while (pL < L) {
      L--;
      mp[a[L]]++;
      if (mp[a[L]] == 1)
        cnt++;
    }
    while (R > pR) {
      mp[a[R]]--;
      if (mp[a[R]] == 0)
        cnt--;
      R--;
    }
    while (R < pR) {
      R++;
      mp[a[R]]++;
      if (mp[a[R]] == 1)
        cnt++;
    }
    ans[v[i].second] = cnt;
  }
  for (ll i = 0; i < q; i++) {
    cout << ans[i] << endl;
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long int t = 1;
  // cin >> t;
  for (long int tt = 1; tt <= t; tt++) {
    solve();
  }
  return 0;
}

Error: TLE

Fixed submission:

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

#define ll long long int
#define fi first
#define se second
#define pb push_back
#define all(a) (a).begin(), (a).end()
#define debug(a) cout << #a << " = " << a << endl;
#define endl "\\n"
#define MOD (int)1e9 + 7
#define inf (int)(1e18)

const int MAXN = 5 * 100005;

int a[MAXN];
vector<int> segTree[MAXN * 4];

void seg_build(int idx, int l, int r) {
  if (l > r)
    return;
  if (l == r) {
    segTree[idx].push_back(a[l]);
    return;
  }
  int mid = (l + r) / 2;
  seg_build(2 * idx, l, mid);
  seg_build(2 * idx + 1, mid + 1, r);

  segTree[idx].resize(segTree[2 * idx].size() + segTree[2 * idx + 1].size());
  segTree[idx] = segTree[2 * idx];
  segTree[idx].insert(segTree[idx].end(), segTree[2 * idx + 1].begin(),
                      segTree[2 * idx + 1].end());
}

unordered_set<int> p;
void seg_query(int idx, int lo, int hi, int ql, int qr) {
  if (lo > qr || hi < ql) {
    return;
  }
  if (lo >= ql && hi <= qr) {
    p.insert(segTree[idx].begin(), segTree[idx].end());
    return;
    ;
  }
  int mid = (lo + hi) / 2;
  seg_query(2 * idx, lo, mid, ql, qr);
  seg_query(2 * idx + 1, mid + 1, hi, ql, qr);
  return;
}
map<pair<int, int>, int> mp;
void solve() {
  long int n, k;
  cin >> n >> k;
  for (long int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  seg_build(1, 1, n);
  while (k--) {
    long int l, r;
    cin >> l >> r;
    if (mp.find({l, r}) != mp.end()) {
      cout << mp[{l, r}] << endl;
    } else {
      p.clear();
      seg_query(1, 1, n, l, r);
      //        cout<<endl;
      cout << p.size() << endl;
    }
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long int t = 1;
  //    cin >> t;
  for (long int tt = 1; tt <= t; tt++) {
    solve();
  }
  return 0;
}