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

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pb push_back
#define all(a) (a).begin(), (a).end()
#define sz(x) (int)x.size()
#define F first
#define S second
#define endl "\\n"
#define inf (long long)1000000007
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define bigint 1e18
mt19937 RNG(chrono::steady_clock::now().time_since_epoch().count());
#define SHUF(v) shuffle(aint(v), RNG);
// Use mt19937_64 for 64 bit random numbers.

template <typename T> T ceil(T a, T b) { return (a + b - 1) / b; }

template <typename T> T binpow(T a, T b, T m) {
  a %= m;
  T res = 1;
  while (b > 0) {
    if (b & 1)
      res = res * a % m;
    a = a * a % m;
    b >>= 1;
  }
  return res;
}
const int BLOCK = 700;

struct Query {
  int l, r, idx;
};

bool cmp(Query x, Query y) {
  if (x.l / BLOCK != y.l / BLOCK) {
    // different blocks, so sort by block.
    return x.l / BLOCK < y.l / BLOCK;
  }
  // same block, so sort by R value
  return x.r < y.r;
}

int arr[511111], cnt[511111], ans = 0;

void add(int idx) {
  cnt[arr[idx]]++;
  if (cnt[arr[idx]] == 1) {
    ans++;
  }
}
void remove(int idx) {
  cnt[arr[idx]]--;
  if (cnt[arr[idx]] == 0) {
    ans--;
  }
}

vector<int> mo_s_algorithm(vector<Query> &queries) {
  vector<int> res(queries.size());
  sort(all(queries), cmp);

  int cur_l = 0;
  int cur_r = -1;
  for (Query q : queries) {
    while (cur_l > q.l) {
      cur_l--;
      add(cur_l);
    }
    while (cur_r < q.r) {
      cur_r++;
      add(cur_r);
    }
    while (cur_l < q.l) {
      remove(cur_l);
      cur_l++;
    }
    while (cur_r > q.r) {
      remove(cur_r);
      cur_r--;
    }
    res[q.idx] = ans;
  }
  return res;
}
void solve() {
  int n, a, b, q;
  cin >> n >> q;
  FOR(i, 0, n) cin >> arr[i];
  Query qq;
  vector<Query> qu;
  FOR(i, 0, q) {
    cin >> a >> b;
    a--, b--;
    qq.l = a, qq.r = b, qq.idx = i;
    qu.pb(qq);
  }
  vector<int> res = mo_s_algorithm(qu);
  for (int x : res) {
    cout << x << endl;
  }
}
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int t = 1;
  // cin>>t;
  FOR(i, 1, t + 1) {
    // cout<<"Case #"<<i<<": ";
    solve();
  }
  cerr << "\\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\\n";
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pb push_back
#define all(a) (a).begin(), (a).end()
#define sz(x) (int)x.size()
#define F first
#define S second
#define endl "\\n"
#define inf (long long)1000000007
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define bigint 1e18
mt19937 RNG(chrono::steady_clock::now().time_since_epoch().count());
#define SHUF(v) shuffle(aint(v), RNG);
// Use mt19937_64 for 64 bit random numbers.

template <typename T> T ceil(T a, T b) { return (a + b - 1) / b; }

template <typename T> T binpow(T a, T b, T m) {
  a %= m;
  T res = 1;
  while (b > 0) {
    if (b & 1)
      res = res * a % m;
    a = a * a % m;
    b >>= 1;
  }
  return res;
}
const int BLOCK = 600;

struct Query {
  int l, r, idx;
};

bool cmp(Query x, Query y) {
  if (x.l / BLOCK != y.l / BLOCK) {
    // different blocks, so sort by block.
    return x.l / BLOCK < y.l / BLOCK;
  }
  // same block, so sort by R value
  return x.r < y.r;
}

int arr[511111], cnt[511111], ans = 0;

void add(int idx) {
  cnt[arr[idx]]++;
  if (cnt[arr[idx]] == 1) {
    ans++;
  }
}
void remove(int idx) {
  cnt[arr[idx]]--;
  if (cnt[arr[idx]] == 0) {
    ans--;
  }
}

vector<int> mo_s_algorithm(vector<Query> &queries) {
  vector<int> res(queries.size());
  sort(all(queries), cmp);

  int cur_l = 0;
  int cur_r = -1;
  for (Query q : queries) {
    while (cur_l > q.l) {
      cur_l--;
      add(cur_l);
    }
    while (cur_r < q.r) {
      cur_r++;
      add(cur_r);
    }
    while (cur_l < q.l) {
      remove(cur_l);
      cur_l++;
    }
    while (cur_r > q.r) {
      remove(cur_r);
      cur_r--;
    }
    res[q.idx] = ans;
  }
  return res;
}
void solve() {
  int n, a, b, q;
  cin >> n >> q;
  FOR(i, 0, n) cin >> arr[i];
  Query qq;
  vector<Query> qu;
  FOR(i, 0, q) {
    cin >> a >> b;
    a--, b--;
    qq.l = a, qq.r = b, qq.idx = i;
    qu.pb(qq);
  }
  vector<int> res = mo_s_algorithm(qu);
  for (int x : res) {
    cout << x << endl;
  }
}
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int t = 1;
  // cin>>t;
  FOR(i, 1, t + 1) {
    // cout<<"Case #"<<i<<": ";
    solve();
  }
  cerr << "\\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\\n";
  return 0;
}