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

///                 .-~~~~~~~~~-._       _.-~~~~~~~~~-.
///             __.'              ~.   .~              `.__
///           .'//                  \\./                  \\\\`.
///        .'//                     |                     \\\\`.
///       .'// .-~"""""""~~~~-._     |     _,-~~~~"""""""~-. \\\\`.
///     .'//.-"                 `-.  |  .-'                 "-.\\\\`.
///   .'//______.============-..   \\ | /   ..-============.______\\\\`.
/// .'______________________________\\|/______________________________`.
#pragma GCC optimize(2)
#pragma comment(linker, "/STACK:102400000,102400000")
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <time.h>
#include <vector>
using namespace std;

#define pi acos(-1)
#define s_1(x) scanf("%d", &x)
#define s_2(x, y) scanf("%d%d", &x, &y)
#define s_3(x, y, z) scanf("%d%d%d", &x, &y, &z)
#define s_4(x, y, z, X) scanf("%d%d%d%d", &x, &y, &z, &X)
#define S_1(x) scan_d(x)
#define S_2(x, y) scan_d(x), scan_d(y)
#define S_3(x, y, z) scan_d(x), scan_d(y), scan_d(z)
#define PI acos(-1)
#define endl '\\n'
#define srand() srand(time(0));
#define me(x, y) memset(x, y, sizeof(x));
#define foreach(it, a)                                                         \\
  for (__typeof((a).begin()) it = (a).begin(); it != (a).end(); it++)
#define close()                                                                \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);
#define FOR(x, n, i) for (int i = x; i <= n; i++)
#define FOr(x, n, i) for (int i = x; i < n; i++)
#define fOR(n, x, i) for (int i = n; i >= x; i--)
#define fOr(n, x, i) for (int i = n; i > x; i--)
#define W while
#define sgn(x) ((x) < 0 ? -1 : (x) > 0)
#define bug printf("***********\\n");
#define db double
#define ll long long
#define mp make_pair
#define pb push_back
typedef long long LL;
typedef pair<int, int> ii;
const int INF = ~0U >> 1;
const LL LINF = 0x3f3f3f3f3f3f3f3fLL;
const int dx[] = {-1, 0, 1, 0, 1, -1, -1, 1};
const int dy[] = {0, 1, 0, -1, -1, 1, -1, 1};
const int maxn = 5e5 + 10;
const int maxx = 5e6 + 10;
const double EPS = 1e-8;
const double eps = 1e-8;
const int mod = 19260817;
template <class T> inline T min(T a, T b, T c) { return min(min(a, b), c); }
template <class T> inline T max(T a, T b, T c) { return max(max(a, b), c); }
template <class T> inline T min(T a, T b, T c, T d) {
  return min(min(a, b), min(c, d));
}
template <class T> inline T max(T a, T b, T c, T d) {
  return max(max(a, b), max(c, d));
}
template <class T> inline bool scan_d(T &ret) {
  char c;
  int sgn;
  if (c = getchar(), c == EOF) {
    return 0;
  }
  while (c != '-' && (c < '0' || c > '9')) {
    c = getchar();
  }
  sgn = (c == '-') ? -1 : 1;
  ret = (c == '-') ? 0 : (c - '0');
  while (c = getchar(), c >= '0' && c <= '9') {
    ret = ret * 10 + (c - '0');
  }
  ret *= sgn;
  return 1;
}

inline bool scan_lf(double &num) {
  char in;
  double Dec = 0.1;
  bool IsN = false, IsD = false;
  in = getchar();
  if (in == EOF)
    return false;
  while (in != '-' && in != '.' && (in < '0' || in > '9'))
    in = getchar();
  if (in == '-') {
    IsN = true;
    num = 0;
  } else if (in == '.') {
    IsD = true;
    num = 0;
  } else
    num = in - '0';
  if (!IsD) {
    while (in = getchar(), in >= '0' && in <= '9') {
      num *= 10;
      num += in - '0';
    }
  }
  if (in != '.') {
    if (IsN)
      num = -num;
    return true;
  } else {
    while (in = getchar(), in >= '0' && in <= '9') {
      num += Dec * (in - '0');
      Dec *= 0.1;
    }
  }
  if (IsN)
    num = -num;
  return true;
}

void Out(LL a) {
  if (a < 0) {
    putchar('-');
    a = -a;
  }
  if (a >= 10)
    Out(a / 10);
  putchar(a % 10 + '0');
}
void print(LL a) { Out(a), puts(""); }
// freopen( "in.txt" , "r" , stdin );
// freopen( "data.txt" , "w" , stdout );
// cerr << "run time is " << clock() << endl;
// void readString(string &s)
//{
//	static char str[maxn];
//	scanf("%s", str);
//	s = str;
// }
const int MAXM = maxn * 40;
int lc[MAXM], rc[MAXM], tol;
LL sum[MAXM];
int a[maxn], rt[maxn];
map<int, int> vis;

int n, q;
void init() {
  tol = 1;
  vis.clear();
}
void update(int &u, int x, int y, int p, LL v) {
  sum[tol] = sum[u] + v, lc[tol] = lc[u], rc[tol] = rc[u];
  u = tol++;
  if (x == y)
    return;
  int m = (x + y) >> 1;
  if (p <= m)
    update(lc[u], x, m, p, v);
  else
    update(rc[u], m + 1, y, p, v);
}
LL query(int u, int x, int y, int ql, int qr) {
  if (ql <= x && y <= qr)
    return sum[u];
  int m = (x + y) >> 1;
  LL ans = 0;
  if (ql <= m)
    ans += query(lc[u], x, m, ql, qr);
  if (qr > m)
    ans += query(rc[u], m + 1, y, ql, qr);
  return ans;
}

void solve() {
  s_2(n, q);
  init();
  FOR(1, n, i) s_1(a[i]);
  FOR(1, n, i) {
    rt[i] = rt[i - 1];
    if (vis.find(a[i]) != vis.end()) {
      int tmp = rt[i - 1];
      update(tmp, 1, n, vis[a[i]], -1);
      rt[i] = tmp;
      update(rt[i], 1, n, i, 1);
    } else {
      rt[i] = rt[i - 1];
      update(rt[i], 1, n, i, 1);
    }
    vis[a[i]] = i;
  }
  W(q--) {
    int l, r;
    s_2(l, r);
    printf("%lld\\n", query(rt[r], 1, n, l, r));
  }
}

int main() {
  // freopen( "1.in" , "r" , stdin );
  // freopen( "1.out" , "w" , stdout );
  int t = 1;
  // init();
  // s_1(t);
  for (int cas = 1; cas <= t; cas++) {
    // printf("Case #%d: ",cas);
    solve();
  }
}

Error: TLE

Fixed submission:

// Problem: 2120  Time:556 ms  Memory:9508 kb
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#define foru(i, a, b) for (int i = a; i <= b; ++i)
#define m(a, b) memset(a, b, sizeof a)
#define en '\\n'
using namespace std;
typedef long long ll;
const int N = 5e5 + 5, M = 5e5 + 5;
template <class T> void rd(T &x) {
  x = 0;
  int f = 0;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    f |= (ch == '-');
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = (x << 1) + (x << 3) + (ch ^ 48);
    ch = getchar();
  }
  x = f ? -x : x;
  return;
}
struct Query {
  int id, l, r, tim;
} q[M];
struct Update {
  int pos, col, pre;
} u[M];
int c[N], las[N], cnt[N * 10];
int block, dexl, dexr, t, res, bl[N];
int ans[M];
inline int cmp(Query x, Query y) {
  if (bl[x.l] == bl[y.l])
    return (bl[x.r] == bl[y.r]) ? (x.tim < y.tim) : (bl[x.r] < bl[y.r]);
  return bl[x.l] < bl[y.l];
}
void modi_time(int t, int f) {
  if (f == 1) {
    c[u[t].pos] = u[t].col;
    if (dexl <= u[t].pos && u[t].pos <= dexr) {
      if ((--cnt[u[t].pre]) == 0)
        --res;
      if ((++cnt[u[t].col]) == 1)
        ++res;
    }
  } else {
    c[u[t].pos] = u[t].pre;
    if (dexl <= u[t].pos && u[t].pos <= dexr) {
      if ((--cnt[u[t].col]) == 0)
        --res;
      if ((++cnt[u[t].pre]) == 1)
        ++res;
    }
  }
}
void add(int x) {
  if ((++cnt[c[x]]) == 1)
    ++res;
}
void del(int x) {
  if ((--cnt[c[x]]) == 0)
    --res;
}
int main() {
  int n, m;
  rd(n), rd(m);
  block = sqrt(n);
  foru(i, 1, n) rd(c[i]), las[i] = c[i], bl[i] = (i - 1) / block + 1;
  int qq = 0, uu = 0;
  foru(i, 1, m) {
    int x, y;
    scanf("%d%d", &x, &y);
    q[++qq] = (Query){qq, x, y, uu};
  }
  sort(q + 1, q + qq + 1, cmp);
  dexl = 1, dexr = 0, t = 0, res = 0;
  foru(i, 1, m) {
    while (t < q[i].tim)
      modi_time(++t, 1);
    while (t > q[i].tim)
      modi_time(t--, -1);
    while (dexr < q[i].r)
      add(++dexr);
    while (dexl > q[i].l)
      add(--dexl);
    while (dexr > q[i].r)
      del(dexr--);
    while (dexl < q[i].l)
      del(dexl++);
    ans[q[i].id] = res;
  }
  foru(i, 1, qq) printf("%d\\n", ans[i]);
}