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

#include <bits/stdc++.h>
using namespace std;

#define all(x) (x).begin(), (x).end()
#define YES() printf("YES\\n")
#define NO() printf("NO\\n")
#define Yes() printf("Yes\\n")
#define No() printf("No\\n")
#define ll long long
#define ld long double
#define pb push_back
#define MOD 1000000007
#define LLMN LONG_LONG_MIN
#define LLMX LONG_LONG_MAX
#define LDMX LDBL_MAX
#define LDMN LDBL_MIN
#define MAX 1000010
#define pll pair<ll, ll>
#define vl vector<ll>
#define vll vector<vector<ll>>
#define vb vector<bool>
#define vbb vector<vector<bool>>

ll mulmod(ll, ll);
ll fastpower(ll, ll);
void getprime(vector<bool> &);
void getfact(vector<ll> &, vector<ll> &);
ll ncr(ll, ll, vector<ll> &, vector<ll> &);

ll block;
struct query {
  ll l, r, ind, res;
};

bool compare(query a, query b) {
  if (a.l / block != b.l / block)
    return a.l / block < b.l / block;
  return a.r < b.r;
}

bool compare1(query a, query b) { return a.ind < b.ind; }

void solve() {
  ll n, q;
  cin >> n >> q;
  vl a(n);
  block = sqrt(n);
  vector<query> qry(q);
  for (ll i = 0; i < n; i++)
    cin >> a[i];
  for (ll i = 0; i < q; i++) {
    ll a, b;
    cin >> a >> b;
    a--;
    b--;
    qry[i].l = a;
    qry[i].r = b;
    qry[i].ind = i;
    qry[i].res = 0;
  }

  sort(all(qry), compare);
  vl freq(MAX, 0);
  ll cl = 0, cr = 0, dist = 0;
  for (ll i = 0; i < q; i++) {
    ll L = qry[i].l, R = qry[i].r;

    while (cl < L) {
      freq[a[cl]]--;
      if (freq[a[cl]] == 0)
        dist--;
      cl++;
    }
    while (cl > L) {
      freq[a[cl - 1]]++;
      if (freq[a[cl - 1]] == 1)
        dist++;
      cl--;
    }
    while (cr <= R) {
      freq[a[cr]]++;
      if (freq[a[cr]] == 1)
        dist++;
      cr++;
    }
    while (cr > R + 1) {
      freq[a[cr - 1]]--;
      if (freq[a[cr - 1]] == 0)
        dist--;
      cr--;
    }
    qry[i].res = dist;
  }
  sort(all(qry), compare1);
  for (ll i = 0; i < q; i++)
    cout << qry[i].res << "\\n";
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  ll t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }
  return 0;
}

ll mulmod(ll a, ll b) {
  ll res = 0;
  a = a % MOD;
  while (b > 0) {
    if (b % 2 == 1)
      res = (res + a) % MOD;

    a = (a * 2) % MOD;
    b /= 2;
  }
  return res % MOD;
}

ll fastpower(ll a, ll n) {
  ll res = 1;
  while (n > 0) {
    if (n % 2 != 0) {
      res = (res * a) % MOD;
    }
    n /= 2;
    a = (a * a) % MOD;
  }
  return res;
}

void getprime(vector<bool> &prime) {
  prime[0] = false;
  prime[1] = false;

  for (ll i = 2; i * i <= MAX; i++) {
    if (prime[i]) {
      for (ll j = i * i; j <= MAX; j += i)
        prime[j] = false;
    }
  }
}

void getfact(vector<ll> &fact, vector<ll> &ifact) {
  fact[0] = 1;
  ifact[0] = 1;

  for (ll i = 1; i <= MAX; i++) {
    fact[i] = (fact[i - 1] * i) % MOD;
    ifact[i] = (ifact[i - 1] * fastpower(i, MOD - 2)) % MOD;
  }
}

ll ncr(ll n, ll r, vector<ll> &fact, vector<ll> &ifact) {
  ll res;
  res = (((fact[n] * ifact[r]) % MOD) * ifact[n - r]) % MOD;
  return res;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define all(x) (x).begin(), (x).end()
#define YES() printf("YES\\n")
#define NO() printf("NO\\n")
#define Yes() printf("Yes\\n")
#define No() printf("No\\n")
#define ll long long
#define ld long double
#define pb push_back
#define MOD 1000000007
#define LLMN LONG_LONG_MIN
#define LLMX LONG_LONG_MAX
#define LDMX LDBL_MAX
#define LDMN LDBL_MIN
#define MAX 1000010
#define pll pair<ll, ll>
#define vl vector<ll>
#define vll vector<vector<ll>>
#define vb vector<bool>
#define vbb vector<vector<bool>>

ll mulmod(ll, ll);
ll fastpower(ll, ll);
void getprime(vector<bool> &);
void getfact(vector<ll> &, vector<ll> &);
ll ncr(ll, ll, vector<ll> &, vector<ll> &);

struct query {
  ll l, r, ind, res;
};

bool compare(query a, query b) {
  if (a.l == b.l)
    return a.r < b.r;
  return a.l < b.l;
}

bool compare1(query a, query b) { return a.ind < b.ind; }

void solve() {
  ll n, q;
  cin >> n >> q;
  vl a(n);
  vector<query> qry(q);
  for (ll i = 0; i < n; i++)
    cin >> a[i];
  for (ll i = 0; i < q; i++) {
    ll a, b;
    cin >> a >> b;
    a--;
    b--;
    qry[i].l = a;
    qry[i].r = b;
    qry[i].ind = i;
    qry[i].res = 0;
  }

  sort(all(qry), compare);
  vl freq(MAX, 0);
  ll cl = 0, cr = 0, dist = 0;
  for (ll i = 0; i < q; i++) {
    ll L = qry[i].l, R = qry[i].r;

    while (cl < L) {
      freq[a[cl]]--;
      if (freq[a[cl]] == 0)
        dist--;
      cl++;
    }
    while (cl > L) {
      freq[a[cl - 1]]++;
      if (freq[a[cl - 1]] == 1)
        dist++;
      cl--;
    }
    while (cr <= R) {
      freq[a[cr]]++;
      if (freq[a[cr]] == 1)
        dist++;
      cr++;
    }
    while (cr > R + 1) {
      freq[a[cr - 1]]--;
      if (freq[a[cr - 1]] == 0)
        dist--;
      cr--;
    }
    qry[i].res = dist;
  }
  sort(all(qry), compare1);
  for (ll i = 0; i < q; i++)
    cout << qry[i].res << "\\n";
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  ll t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }
  return 0;
}

ll mulmod(ll a, ll b) {
  ll res = 0;
  a = a % MOD;
  while (b > 0) {
    if (b % 2 == 1)
      res = (res + a) % MOD;

    a = (a * 2) % MOD;
    b /= 2;
  }
  return res % MOD;
}

ll fastpower(ll a, ll n) {
  ll res = 1;
  while (n > 0) {
    if (n % 2 != 0) {
      res = (res * a) % MOD;
    }
    n /= 2;
    a = (a * a) % MOD;
  }
  return res;
}

void getprime(vector<bool> &prime) {
  prime[0] = false;
  prime[1] = false;

  for (ll i = 2; i * i <= MAX; i++) {
    if (prime[i]) {
      for (ll j = i * i; j <= MAX; j += i)
        prime[j] = false;
    }
  }
}

void getfact(vector<ll> &fact, vector<ll> &ifact) {
  fact[0] = 1;
  ifact[0] = 1;

  for (ll i = 1; i <= MAX; i++) {
    fact[i] = (fact[i - 1] * i) % MOD;
    ifact[i] = (ifact[i - 1] * fastpower(i, MOD - 2)) % MOD;
  }
}

ll ncr(ll n, ll r, vector<ll> &fact, vector<ll> &ifact) {
  ll res;
  res = (((fact[n] * ifact[r]) % MOD) * ifact[n - r]) % MOD;
  return res;
}