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

#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define sz(x) ((int)(x).size())
#define endl "\\n"
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(nullptr);
#define all(x) x.begin(), x.end()
#define y1 omg
#define INF 1000000010
#define FOR(x, n) for (int x = 0; x < n; x++)
#define ini(x, n) memset(x, n, sizeof(x));
#define MOD ((int)1e9 + 7)
#define atoi(x) ((x) - '0')
using namespace std;
typedef long long ll;
typedef pair<int, int> ii;
typedef pair<int, ii> iii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<vi> vvi;
#define MAX 500005

int dx[] = {1, -1, 0, 0, 1, 1, -1, -1};
int dy[] = {0, 0, 1, -1, 1, -1, 1, -1};

struct st {
  int lo;
  int hi;
  int index;
} s[MAX];

int n, m, a, b, val = 0, clo = 0, chi = 0, tlo, thi, arr[MAX], cnt[MAX * 50],
                ans[MAX];

bool cmp(st s1, st s2) {
  int tmp1 = s1.lo / sqrt(n);
  int tmp2 = s2.lo / sqrt(n);
  return tmp1 == tmp2 ? s1.hi < s2.hi : tmp1 < tmp2;
}

void my_add(int in) { val += ++cnt[in] == 1 ? 1 : 0; }

void my_sub(int in) { val -= --cnt[in] == 0 ? 1 : 0; }

int main() {
  scanf("%d", &n);
  scanf("%d", &m);
  for (int i = 1; i <= n; i++)
    scanf("%d", &arr[i]);

  for (int i = 0; i < m; i++) {
    scanf("%d%d", &a, &b);
    s[i].lo = a, s[i].hi = b, s[i].index = i;
  }
  sort(s, s + m, cmp);
  for (int i = 0; i < m; i++) {
    tlo = s[i].lo, thi = s[i].hi;
    for (int k = clo; k < tlo; k++)
      my_sub(arr[k]);
    for (int k = chi + 1; k <= thi; k++)
      my_add(arr[k]);
    for (int k = tlo; k < clo; k++)
      my_add(arr[k]);
    for (int k = thi + 1; k <= chi; k++)
      my_sub(arr[k]);
    ans[s[i].index] = val;
    clo = tlo, chi = thi;
    // 1번 실수
  }
  for (int i = 0; i < m; i++)
    printf("%d\\n", ans[i]);
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define sz(x) ((int)(x).size())
#define endl "\\n"
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(nullptr);
#define all(x) x.begin(), x.end()
#define y1 omg
#define INF 1000000010
#define FOR(x, n) for (int x = 0; x < n; x++)
#define ini(x, n) memset(x, n, sizeof(x));
#define MOD ((int)1e9 + 7)
#define atoi(x) ((x) - '0')
using namespace std;
typedef long long ll;
typedef pair<int, int> ii;
typedef pair<int, ii> iii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<vi> vvi;

int dx[] = {1, -1, 0, 0, 1, 1, -1, -1};
int dy[] = {0, 0, 1, -1, 1, -1, 1, -1};

typedef struct {
  int l, r, id;
} Q;
int sz;
int arr[500005];
int cnt[5000005];
int dis = 0;
int ans[500005];
bool cmp(Q a, Q b) {
  return make_tuple(a.r / sz, a.l, a.id) < make_tuple(b.r / sz, b.l, b.id);
}

void add(int i) {
  if (++cnt[i] == 1)
    dis++;
}
void erase(int i) {
  if (--cnt[i] == 0)
    dis--;
}

int main() {
  int n, m;
  scanf("%d", &n);
  scanf("%d", &m);
  for (int i = 0; i < n; i++)
    scanf("%d", &arr[i]);
  vector<Q> v;
  for (int i = 0; i < m; i++) {
    int l, r;
    scanf("%d %d", &l, &r);
    v.push_back({l - 1, r - 1, i});
  }
  sz = sqrt(n);
  sort(v.begin(), v.end(), cmp);
  int l = 0, r = 0;
  add(arr[0]);
  for (int i = 0; i < m; i++) {
    int nl = v[i].l, nr = v[i].r;
    for (int j = l; j < nl; j++)
      erase(arr[j]);
    for (int j = l - 1; j >= nl; j--)
      add(arr[j]);
    for (int j = r + 1; j <= nr; j++)
      add(arr[j]);
    for (int j = r; j > nr; j--)
      erase(arr[j]);
    l = nl;
    r = nr;
    ans[v[i].id] = dis;
  }
  for (int i = 0; i < m; i++) {
    printf("%d\\n", ans[i]);
  }
}