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

#include <bits/stdc++.h>
#define FIO                                                                    \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define T()                                                                    \\
  int t;                                                                       \\
  cin >> t;                                                                    \\
  while (t--)
#define rep(i, start, lim) for (int i = start; i < lim; i++)
#define repd(i, start, lim) for (int i = start; i >= lim; i--)
#define scan(x) scanf("%lld", &x)
#define print(x) printf("%lld", x)
#define space printf(" ")
#define br printf("\\n");
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define YES printf("YES\\n")
#define NO printf("NO\\n")
#define all(c) (c).begin(), (c).end()
#define present(c, x) ((c).find(x) != (c).end())
#define cpresent(c, x) (find(all(c), x) != (c).end())
#define debug cout << "here" << endl
#define TRACE
#ifdef TRACE
#define trace1(x) cerr << #x << ": " << x << endl
#define trace2(x, y) cerr << #x << ": " << x << " | " << #y << ": " << y << endl
#define trace3(x, y, z)                                                        \\
  cerr << #x << ":" << x << " | " << #y << ": " << y << " | " << #z << ": "    \\
       << z << endl
#define trace4(a, b, c, d)                                                     \\
  cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \\
       << c << " | " << #d << ": " << d << endl
#define trace5(a, b, c, d, e)                                                  \\
  cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \\
       << c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl
#define trace6(a, b, c, d, e, f)                                               \\
  cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \\
       << c << " | " << #d << ": " << d << " | " << #e << ": " << e << " | "   \\
       << #f << ": " << f << endl
#else
#define trace1(x) printf("")
#define trace2(x, y) printf("")
#define trace3(x, y, z) printf("")
#define trace4(a, b, c, d) printf("")
#define trace5(a, b, c, d, e) printf("")
#define trace6(a, b, c, d, e, f) printf("")
#endif
#define MAX 100005
#define LIMIT 262950
#define INF 1011111111
#define LLINF 1000111000111000111LL
#define EPS (double)1e-10
#define MOD 1000000007
#define PI 3.14159265358979323
using namespace std;
typedef long double ldb;
typedef long long lld;
typedef unsigned long long ulld;
lld powm(lld base, lld exp, lld mod = MOD) {
  lld ans = 1;
  while (exp) {
    if (exp & 1)
      ans = (ans * base) % mod;
    exp >>= 1, base = (base * base) % mod;
  }
  return ans;
}
inline bool ispalin(string s) {
  for (int i = 0, j = s.size() - 1; i <= j; i++, j--)
    if (s[i] != s[j])
      return false;
  return true;
}
inline lld lcm(lld a, lld b) { return (a / __gcd(a, b)) * b; }
///******Lets start coding :-) *******//
/*
struct DSU{
    lld p[MAX];
    void init(lld n){
        rep(i,1,n+1)p[i]=i;
    }
    int find(lld x){
        if(p[x]==x)return x;
        return p[x]=find(p[x]);
    }
    void join(lld x,lld y){
        x=find(x);
        y=find(y);
        if(x==y)return;
        p[y]=x;
    }
}dsu;
*/
#define N 500000
#define MAXN 1000001
int a[N], pre[N], n, m, dummy[MAXN];
struct node {
  int count;
  node *left, *right;
  node(int count, node *left, node *right)
      : count(count), left(left), right(right) {}
  node *insert(int l, int r, int w);
};
node *null = new node(0, NULL, NULL);
node *root[N];
node *node::insert(int l, int r, int w) {
  if (l <= w and w <= r) {
    if (l == r) {
      return new node(this->count + 1, null, null);
    }
    int mid = (l + r) >> 1;
    return new node(this->count + 1, this->left->insert(l, mid, w),
                    this->right->insert(mid + 1, r, w));
  }
  return this;
}
int query(node *a, int start, int end, int l, int r) {
  if (end < l or start > r)
    return 0;
  if (start >= l and end <= r) {
    return a->count;
  }
  int mid = (start + end) >> 1;
  return query(a->left, start, mid, l, r) + query(a->right, mid + 1, end, l, r);
}
int main() {

  scanf("%d%d", &n, &m);
  rep(i, 1, n + 1) {
    scanf("%d", &a[i]);
    dummy[a[i]] = 0;
  }
  rep(i, 1, n + 1) {
    pre[i] = dummy[a[i]];
    dummy[a[i]] = i;
  }
  // rep(i,1,n+1)printf("%d ",pre[i]);
  null->left = null->right = null;
  root[0] = null;
  rep(i, 1, n + 1) {
    root[i] = ((i == 1) ? null : root[i - 1])->insert(0, n, pre[i]);
  }
  // scanf("%d", &m);
  while (m--) {
    int u, v;
    scanf("%d%d", &u, &v);
    int x = query(root[u - 1], 0, n, 0, u - 1);
    int y = query(root[v], 0, n, 0, u - 1);
    printf("%d\\n", y - x);
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define fr(a, b) for (ll i = a; i < b; i++)
#define mod 1000000007
#define inf (1LL << 60)
#define all(x) (x).begin(), (x).end()
#define prDouble(x) cout << fixed << setprecision(10) << x
#define triplet pair<ll, pair<ll, ll>>
#define fast_io                                                                \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL)
const int rootN = 555;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef vector<pll> vop;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<int> vi;
typedef pair<int, int> pii;
#define REP(i, a, b) for (long long i = a; i < b; i++)
#define REPR(i, a, b) for (long long i = a; i >= b; i--)
#define adb(a)                                                                 \\
  for (auto ii : a)                                                            \\
    cout << ii << " ";                                                         \\
  cout << endl
#define adp(a)                                                                 \\
  for (auto ii : a)                                                            \\
    cout << ii.F << " " << ii.S << endl;
#define MP make_pair
#define F first
#define S second
#define endl "\\n"
#define mem(a, b) memset(a, b, sizeof(a))

struct hash_pair {
  template <class T1, class T2> size_t operator()(const pair<T1, T2> &p) const {
    auto hash1 = hash<T1>{}(p.first);
    auto hash2 = hash<T2>{}(p.second);
    return hash1 ^ hash2;
  }
};

struct LessSecond {
  template <typename T, typename U>
  bool operator()(const std::pair<T, U> &x, const std::pair<T, U> &y) const {
    return x.second < y.second;
  }
};

ll power(ll a, ll b, ll m) {
  ll ans = 1;
  a = a % m;
  while (b) {
    if (b & 1)
      ans = (ans * a) % m;
    b /= 2;
    a = (a * a) % m;
  }
  return ans;
}

ll lcm(ll a, ll b) { return (a * b) / __gcd(a, b); }

ll modInverse(ll a, ll m) {
  ll g = __gcd(a, m);
  return power(a, m - 2, m);
}

int freq[500005];
int distinct;
int ar[500005];

struct query {
  int L;
  int R;
  int q_no;
  int block_no;
  bool operator<(query &q2) {
    if (block_no < q2.block_no)
      return 1;
    else if (block_no > q2.block_no)
      return 0;
    else
      return R < q2.R;
  }
};

void Add(int &elem) {
  if (!freq[elem])
    distinct++;
  freq[elem]++;
}
void Remove(int &elem) {
  freq[elem]--;
  if (!freq[elem])
    distinct--;
}

void adjust(int &curr_l, int &curr_r, query &q) {
  while (curr_l > q.L) {
    curr_l--;
    Add(ar[curr_l]);
  }
  while (curr_r < q.R) {
    curr_r++;
    Add(ar[curr_r]);
  }
  while (curr_l < q.L) {
    Remove(ar[curr_l]);
    curr_l++;
  }
  while (curr_r > q.R) {
    Remove(ar[curr_r]);
    curr_r--;
  }
}

void solve(vector<query> &queries) {
  vector<int> answer(queries.size());
  sort(queries.begin(), queries.end());
  memset(freq, 0, sizeof freq);
  distinct = 1;

  int curr_l = queries[0].L;
  int curr_r = queries[0].L;

  freq[ar[curr_l]]++;

  for (query &qr : queries) {
    adjust(curr_l, curr_r, qr);
    answer[qr.q_no] = distinct;
  }

  for (int x : answer)
    cout << x << '\\n';
}

int main() {

  fast_io;
  int n, q;
  cin >> n >> q;

  map<int, int> coordinateCompress;
  int compressed_Num = 1;

  for (int i = 0; i < n; i++) {
    cin >> ar[i];
    if (coordinateCompress.find(ar[i]) != coordinateCompress.end()) {
      ar[i] = coordinateCompress[ar[i]];
    } else {
      coordinateCompress[ar[i]] = compressed_Num;
      ar[i] = compressed_Num++;
    }
  }

  vector<query> queries(q);
  for (int i = 0; i < q; i++) {
    int u, v;
    cin >> u >> v;
    queries[i].L = u - 1;
    queries[i].R = v - 1;
    queries[i].q_no = i;
    queries[i].block_no = u / rootN;
  }
  solve(queries);
  return 0;
}