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

/*
 *      ┌─┐       ┌─┐ + +
 *   ┌──┘ ┴───────┘ ┴──┐++
 *   │                 │
 *   │       ───       │++ + + +
 *   ███████───███████ │+
 *   │                 │+
 *   │       ─┴─       │
 *   │                 │
 *   └───┐         ┌───┘
 *       │         │
 *       │         │   + +
 *       │         │       Code is far away from bug with the animal protecting
 *       │         └──────────────┐
 *       │                        │
 *       │                        ├─┐
 *       │                        ┌─┘
 *       │                        │
 *       └─┐  ┐  ┌───────┬──┐  ┌──┘  + + + +
 *         │ ─┤ ─┤       │ ─┤ ─┤
 *         └──┴──┘       └──┴──┘  + + + +
 *                神兽保佑
 *               代码无BUG!
 */

#include <bits/stdc++.h>
using namespace std;
class IN {
public:
  template <typename T> IN &operator>>(T &x) {
    read(x);
    return *this;
  }
  IN &operator>>(double &x) {
    scanf("%lf", &x);
    return *this;
  }
  IN &operator>>(char *s) {
    scanf("%s", s);
    return *this;
  }
  IN &operator>>(char &c) {
    c = getchar();
    return *this;
  }

private:
  template <typename T> void read(T &x) {
    T f = 1;
    int c = getchar();
    x = 0;
    while (c < '0' || c > '9') {
      if (c == '-')
        f = -1;
      c = getchar();
    }
    while (c >= '0' && c <= '9')
      x = (x << 3) + (x << 1) + (c ^ 48), c = getchar();
    x *= f;
  }
} in;
class NL {
} nl;
class BK {
} bk;
class OUT {
public:
  template <typename T> OUT &operator<<(T x) {
    write(x);
    return *this;
  }
  OUT &operator<<(const char *s) {
    printf("%s", s);
    return *this;
  }
  OUT &operator<<(char c) {
    putchar(c);
    return *this;
  }
  OUT &operator<<(BK x) {
    putchar(' ');
    return *this;
  }
  OUT &operator<<(NL x) {
    putchar('\\n');
    return *this;
  }

private:
  template <typename T> void write(T x) {
    if (x < 0)
      putchar('-'), x = -x;
    if (x >= 10)
      write(x / 10);
    putchar(x % 10 + 48);
  }
} out;

#define pb(x) push_back(x)
#define fi first
#define se second
#define rep(i, a, n) for (int i = a; i <= n; ++i)
#define per(i, n, a) for (int i = n; i >= a; --i)
#define mem(a, x) memset(a, x, sizeof(a))
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PI;
typedef pair<ll, ll> PL;
typedef vector<int> VI;
typedef vector<ll> VL;
/*****************************************************************************************************/
ll Pow(ll a, ll b, ll mod) {
  ll res = 1;
  a %= mod;
  while (b) {
    if (b & 1)
      (res *= a) %= mod;
    (a *= a) %= mod;
    b >>= 1;
  }
  return res % mod;
}
ll inv(ll a, ll p) {
  if (a == 1)
    return 1;
  return (p - p / a) * inv(p % a, p) % p;
} // p为素数,a<p
ll C(ll n, ll m, ll p) {
  ll res = 1;
  for (ll i = 1, j = n - m + 1; i <= m; ++i, ++j) {
    res = res * inv(i, p) % p * j % p;
  }
  return res;
}
/*****************************************************************************************************/
/*ll ask(int x){ll res=0;while(x)res+=c[x],x-=x&-x;return res;}
void add(ll v,int x){while(x<=n)c[x]+=v,x+=x&-x;}*/
const int p = 1e9 + 7;
const int N = 2e5 + 10;

int main() {
  int x;
  in >> x;
  if (x >= 30)
    puts("Yes");
  else
    puts("No");
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#pragma GCC optimize(2)
using namespace std;
const int N = 3e7 + 10;
inline int read() {
  int x = 0;
  char c = getchar();
  while (c < '0' || c > '9')
    c = getchar();
  while (c >= '0' && c <= '9')
    x = (x << 3) + (x << 1) + (c ^ 48), c = getchar();
  return x;
}
void write(int x) {
  if (x >= 10)
    write(x / 10);
  putchar(x % 10 + 48);
}

struct Node {
  int sum, lc, rc;
};
Node dat[N];
int tot;

int insert(int pos, int v, int rt, int l, int r) {
  int p = ++tot;
  dat[p] = dat[rt];
  if (r - l == 1) {
    dat[p].sum += v;
    return p;
  }
  int m = (l + r) >> 1;
  if (pos < m)
    dat[p].lc = insert(pos, v, dat[rt].lc, l, m);
  else
    dat[p].rc = insert(pos, v, dat[rt].rc, m, r);
  dat[p].sum = dat[dat[p].lc].sum + dat[dat[p].rc].sum;
  return p;
}

int query(int left, int rt, int l, int r) {
  if (left <= l)
    return dat[rt].sum;
  int m = (l + r) >> 1, sum = 0;
  if (left < m)
    sum += query(left, dat[rt].lc, l, m);
  sum += query(left, dat[rt].rc, m, r);
  return sum;
}
int last[N], root[N];

int main() {
  int n, q;
  n = read();
  q = read();
  for (int i = 1; i <= n; ++i) {
    int x = read();
    if (last[x]) {
      root[i] = insert(last[x], -1, root[i - 1], 1, n + 1);
      root[i] = insert(i, 1, root[i], 1, n + 1);
    } else
      root[i] = insert(i, 1, root[i - 1], 1, n + 1);
    last[x] = i;
  }
  while (q--) {
    int x, y;
    x = read();
    y = read();
    write(query(x, root[y], 1, n + 1));
    putchar('\\n');
  }
  return 0;
}