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

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

typedef long double ld; // XD
#define int long long
#define PB push_back
#define all(x) (x).begin(), (x).end()
#define ii pair<int, int>
#define mii map<int, int>
#define vi vector<int>
#define vii vector<ii>
#define F first
#define S second
#define rep(i, a, b) for (int i = a; i < b; i++)
#define repd(i, a, b) for (int i = a; i >= b; --i)
#define sz(x) (int)((x).size())
const double pi = 2 * acos(0.0);
inline void io() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
}

const int mod = 1e9 + 7;
const int INF = 1e18;
const int maxn = 5e5 + 5;

// MO's algo

int ar[maxn];
int freq[maxn];
int BLOCK;
int cnt[maxn];
int ans[maxn];

struct Mo {
  int l, r, idx;
} Q[maxn];

int dis = 0;
inline void Add(int pos) {
  cnt[ar[pos]]++;
  if (cnt[ar[pos]] == 1)
    dis++;
}
inline void Remove(int pos) {
  cnt[ar[pos]]--;
  if (cnt[ar[pos]] == 0)
    dis--;
}
bool comp(Mo a, Mo b) {
  if (a.l / BLOCK != b.l / BLOCK)
    return a.l < b.l;
  return a.l / BLOCK % 2 ? a.r < b.r : a.r > b.r;
}
void solve() {
  int n, q;
  cin >> n >> q;
  rep(i, 1, n + 1) cin >> ar[i];
  BLOCK = 1201;
  int lt, rt;
  rep(i, 0, q) {
    cin >> lt >> rt;
    Q[i].l = lt;
    Q[i].r = rt;
    Q[i].idx = i;
  }
  sort(Q, Q + q, comp);
  int lb, rb, cl = 0, cr = 0;

  rep(i, 0, q) {
    lb = Q[i].l;
    rb = Q[i].r;
    while (cl < lb)
      Remove(cl), cl++;

    while (cl > lb)
      Add(cl - 1), cl--;

    while (cr <= rb)
      Add(cr), cr++;

    while (cr > (rb + 1))
      Remove(cr - 1), cr--;

    ans[Q[i].idx] = dis;
  }
  rep(i, 0, q) cout << ans[i] << "\\n";
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0); // io();
  cout << setprecision(6);
  int tc = 1; // cin>>tc;
  while (tc--) {
    solve(); // cout<<'\\n';
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int maxn = 5e5 + 5;
int arr[maxn];
// Each segment of the segment tree would be a set
// to maintain distinct elements
set<int> *segment;

// Build the segment tree
// i denotes current node, s denotes start and
// e denotes the end of range for current node
void build(int i, int s, int e, int arr[]) {

  // If start is equal to end then
  // insert the array element
  if (s == e) {
    segment[i].insert(arr[s]);
    return;
  }

  // Else divide the range into two halves
  // (start to mid) and (mid+1 to end)
  // first half will be the left node
  // and the second half will be the right node
  build(2 * i, s, (s + e) / 2, arr);
  build(1 + 2 * i, 1 + (s + e) / 2, e, arr);

  // Insert the sets of right and left
  // node of the segment tree
  segment[i].insert(segment[2 * i].begin(), segment[2 * i].end());

  segment[i].insert(segment[2 * i + 1].begin(), segment[2 * i + 1].end());
}

// Query in an range a to b
set<int> query(int node, int l, int r, int a, int b) {
  set<int> left, right, result;

  // If the range is out of the bounds
  // of this segment
  if (b < l || a > r)
    return result;

  // If the range lies in this segment
  if (a <= l && r <= b)
    return segment[node];

  // Else query for the right and left
  // leaf node of this subtree
  // and insert them into the set
  left = query(2 * node, l, (l + r) / 2, a, b);
  result.insert(left.begin(), left.end());

  right = query(1 + 2 * node, 1 + (l + r) / 2, r, a, b);
  result.insert(right.begin(), right.end());

  // Return the result
  return result;
}

// Initialize the segment tree
void init(int n) {
  // Get the height of the segment tree
  int h = (int)ceil(log2(n));
  h = (2 * (pow(2, h)));

  // Initialize the segment tree
  segment = new set<int>[h];
}

// Function to get the result for the
// subarray from arr[l] to arr[r]
void getDistinct(int l, int r, int n) {
  set<int> ans = query(1, 0, n - 1, l, r);

  cout << ans.size() << "\\n";
}

// Driver code
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0); // io();

  int n, t;
  cin >> n >> t;
  for (int i = 0; i < n; i++)
    cin >> arr[i];
  init(n);
  build(1, 0, n - 1, arr);
  while (t--) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    getDistinct(x, y, n);
  }

  return 0;
}