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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; (i) < (n); i++)
#define rep1(i, n) for (int i = 1; (i) <= (n); i++)
#define mst(a, b) memset(a, b, sizeof(a))
#define scd(a) scanf("%d", &a)
#define scdd(a, b) scanf("%d%d", &a, &b)
#define scddd(a, b, c) scanf("%d%d%d", &a, &b, &c)
#define IOS ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define dbg(a) cout << "* " << #a << " : " << a << endl
#define fr first
#define se second
#define ls x << 1
#define rs x << 1 | 1
#define ll long long
#define ull unsigned long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pdd pair<double, double>
#define lowbit(x) x & (-x)
#define ac cout << ans << endl
using namespace std;
const double pi = acos(-1.0);
const double eps = 1e-10;
const ull hashp = 131;
const ll INF = 0x3f3f3f3f3f3f3f3fll;
const int inf = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const int maxn = 5e5 + 100;
struct node {
  int l, r, id;
} Q[maxn];
inline int read() {
  int s = 0, w = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      w = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9')
    s = s * 10 + ch - '0', ch = getchar();
  return s * w;
}
int ans[maxn];
int answer[maxn];
int flag[maxn];
int a[maxn];
bool cmp(node x, node y) {
  if (ans[x.l] == ans[y.l])
    return x.r < y.r;
  return ans[x.l] < ans[y.l];
}
int n, m, k;
int L = 1, R = 0;
int Ans = 0;
void add(int x) {
  if (!flag[a[x]])
    Ans++;
  flag[a[x]]++;
}
void del(int x) {
  flag[a[x]]--;
  if (!flag[a[x]])
    Ans--;
}
void solve() {
  int n = read(), m = read();
  int sz = sqrt(n);
  for (int i = 1; i <= n; i++) {
    a[i] = read();
    ans[i] = i / sz;
  }
  for (int i = 1; i <= m; i++) {
    Q[i].l = read();
    Q[i].r = read();
    Q[i].id = i;
  }
  sort(Q + 1, Q + m + 1, cmp);
  for (int i = 1; i <= m; i++) {
    while (L < Q[i].l) {
      del(L);
      L++;
    }
    while (R > Q[i].r) {
      del(R);
      R--;
    }
    while (L > Q[i].l) {
      L--;
      add(L);
    }
    while (R < Q[i].r) {
      R++;
      add(R);
    }

    answer[Q[i].id] = Ans;
  }
  for (int i = 1; i <= m; i++) {
    printf("%d\\n", answer[i]);
  }
}
int main() {
  // freopen("D://data.txt","r",stdin);
  // freopen("D://out.txt","w",stdout);
  solve();
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; (i) < (n); i++)
#define rep1(i, n) for (int i = 1; (i) <= (n); i++)
#define mst(a, b) memset(a, b, sizeof(a))
#define scd(a) scanf("%d", &a)
#define scdd(a, b) scanf("%d%d", &a, &b)
#define scddd(a, b, c) scanf("%d%d%d", &a, &b, &c)
#define IOS ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define dbg(a) cout << "* " << #a << " : " << a << endl
#define fr first
#define se second
#define ls x << 1
#define rs x << 1 | 1
#define ll long long
#define ull unsigned long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pdd pair<double, double>
#define lowbit(x) x & (-x)
#define ac cout << ans << endl
using namespace std;
const double pi = acos(-1.0);
const double eps = 1e-10;
const ull hashp = 131;
const ll INF = 0x3f3f3f3f3f3f3f3fll;
const int inf = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const int maxn = 5e5 + 100;
struct node {
  int l, r, id;
} Q[maxn];
inline int read() {
  int s = 0, w = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      w = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9')
    s = s * 10 + ch - '0', ch = getchar();
  return s * w;
}
int ans[maxn];
int answer[maxn];
int flag[maxn];
int a[maxn];
bool cmp(node x, node y) {
  if (ans[x.l] == ans[y.l])
    return x.r < y.r;
  return ans[x.l] < ans[y.l];
}
int n, m, k;
int L = 1, R = 0;
int Ans = 0;
void add(int x) {
  if (!flag[a[x]])
    Ans++;
  flag[a[x]]++;
}
void del(int x) {
  flag[a[x]]--;
  if (!flag[a[x]])
    Ans--;
}
void solve() {
  int n = read(), m = read();
  int sz = sqrt(n);
  for (int i = 1; i <= n; i++) {
    a[i] = read();
    ans[i] = i / sz;
  }
  for (int i = 1; i <= m; i++) {
    Q[i].l = read();
    Q[i].r = read();
    Q[i].id = i;
  }
  sort(Q + 1, Q + m + 1, cmp);
  for (int i = 1; i <= m; i++) {
    while (L < Q[i].l) {
      del(L);
      L++;
    }
    while (L > Q[i].l) {
      L--;
      add(L);
    }
    while (R < Q[i].r) {
      R++;
      add(R);
    }
    while (R > Q[i].r) {
      del(R);
      R--;
    }
    answer[Q[i].id] = Ans;
  }
  for (int i = 1; i <= m; i++) {
    printf("%d\\n", answer[i]);
  }
}
int main() {
  // freopen("D://data.txt","r",stdin);
  // freopen("D://out.txt","w",stdout);
  solve();
  return 0;
}