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

// mai iss duniyaa ka sbse bada chutiyaaa huu

#include <bits/stdc++.h>
#include <chrono>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define mod 1000000007
#define mod2 998244353
#define int long long
#define endl '\\n'
#define p_b push_back
#define m_p make_pair
#define fastIO ios_base::sync_with_stdio(false), cin.tie(NULL)
using namespace std;
using namespace __gnu_pbds;
using namespace std::chrono;

template <class T>
using _ost = tree<T, null_type, less_equal<T>, rb_tree_tag,
                  tree_order_statistics_node_update>;

char alphabet[26] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
                     'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r',
                     's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};

// rectangle intersection
// x5 = max(x1, x3);
// y5 = max(y1, y3);
// x6 = min(x2, x4);
// y6 = min(y2, y4);

const int mx = 500005;

// char get_next(char c) {
// 	for(int i=0; i<25; i++) if(c == alphabet[i]) return alphabet[i+1];
// 	return 'a';
// }

struct no {
  int l, r, id;
} q[mx];

int bl[mx];
int col[mx];
int mp[mx];

void get_blocks(int n) {
  int sqroot = sqrt(n) + 1;
  for (int i = 0; i <= n; i++)
    bl[i] = i / sqroot;
}
bool mo_sort(no a, no b) {
  return bl[a.l] == bl[b.l] ? a.r < b.r : bl[a.l] < bl[b.l];
}
int diff = 0;
void add(int id) {
  if (!mp[col[id]])
    diff++;
  mp[col[id]]++;
}
void rem(int id) {
  mp[col[id]]--;
  if (mp[col[id]] == 0)
    diff--;
}
int32_t main() {
  fastIO;
  int tc;
  // cin >> tc;
  tc = 1;
  while (tc--) {
    int n, qry;
    cin >> n >> qry;
    get_blocks(n);
    for (int i = 0; i < n; i++)
      cin >> col[i];
    for (int i = 0; i < qry; i++) {
      cin >> q[i].l >> q[i].r;
      q[i].id = i;
    }
    int ans[qry];
    sort(q, q + qry, mo_sort);
    int curl = 0, curr = -1;
    for (int i = 0; i < qry; i++) {
      int l = q[i].l - 1, r = q[i].r - 1;
      while (curl > l) {
        curl--;
        add(curl);
      }
      while (curr < r) {
        curr++;
        add(curr);
      }
      while (curl < l) {
        rem(curl);
        curl++;
      }
      while (curr > r) {
        rem(curr);
        curr--;
      }
      ans[q[i].id] = diff;
    }
    for (int i = 0; i < qry; i++)
      cout << ans[i] << endl;
  }
}


Error: TLE

Fixed submission:

// mai iss duniyaa ka sbse bada chutiyaaa huu

#include <bits/stdc++.h>
#include <chrono>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define mod 1000000007
#define mod2 998244353
#define int long long
#define endl '\\n'
#define p_b push_back
#define m_p make_pair
#define fastIO ios_base::sync_with_stdio(false), cin.tie(NULL)
using namespace std;
using namespace __gnu_pbds;
using namespace std::chrono;

template <class T>
using _ost = tree<T, null_type, less_equal<T>, rb_tree_tag,
                  tree_order_statistics_node_update>;

char alphabet[26] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
                     'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r',
                     's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};

// rectangle intersection
// x5 = max(x1, x3);
// y5 = max(y1, y3);
// x6 = min(x2, x4);
// y6 = min(y2, y4);

const int mx = 500005;

// char get_next(char c) {
// 	for(int i=0; i<25; i++) if(c == alphabet[i]) return alphabet[i+1];
// 	return 'a';
// }

struct no {
  int l, r, id;
} q[mx];

int bl[mx];
int col[mx];
unordered_map<int, int> mp;

void get_blocks(int n) {
  int sqroot = sqrt(n) + 1;
  for (int i = 0; i <= n; i++)
    bl[i] = i / sqroot;
}
bool mo_sort(no a, no b) {
  return bl[a.l] == bl[b.l] ? a.r < b.r : bl[a.l] < bl[b.l];
}
int diff = 0;
void add(int id) {
  if (!mp[col[id]])
    diff++;
  mp[col[id]]++;
}
void rem(int id) {
  mp[col[id]]--;
  if (mp[col[id]] == 0)
    diff--;
}
int32_t main() {
  fastIO;
  int tc;
  // cin >> tc;
  tc = 1;
  while (tc--) {
    int n, qry;
    cin >> n >> qry;
    get_blocks(n);
    for (int i = 0; i < n; i++)
      cin >> col[i];
    for (int i = 0; i < qry; i++) {
      cin >> q[i].l >> q[i].r;
      q[i].id = i;
    }
    int ans[qry];
    sort(q, q + qry, mo_sort);
    int curl = 0, curr = -1;
    for (int i = 0; i < qry; i++) {
      int l = q[i].l - 1, r = q[i].r - 1;
      while (curl > l) {
        curl--;
        add(curl);
      }
      while (curr < r) {
        curr++;
        add(curr);
      }
      while (curl < l) {
        rem(curl);
        curl++;
      }
      while (curr > r) {
        rem(curr);
        curr--;
      }
      ans[q[i].id] = diff;
    }
    for (int i = 0; i < qry; i++)
      cout << ans[i] << endl;
  }
}