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


#include <bits/stdc++.h>
#define ll int
#define mod 1000000000000000001
#define pi 3.14159265358979323846264338327950288419716939937510
#define setbit(x) __builtin_popcountll(x)
#define db long double
#define pp(x) pair<x, x>
#define ff first
#define ss second
#define FIO                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
#define tc                                                                     \\
  ll tc;                                                                       \\
  cin >> tc;                                                                   \\
  for (ll number = 1; number <= tc; number++)
#define vv(x) vector<x>
#define pb push_back
#define pob pop_back
#define pf push_front
#define pof pop_front
#define endl "\\n"
#define pq priority_queue
#define dbg(x)                                                                 \\
  { cout << #x << ": " << (x) << endl; }
#define dbg2(x, y)                                                             \\
  { cout << #x << ": " << (x) << " , " << #y << ": " << (y) << endl; }
#define bp                                                                     \\
  ll kkk;                                                                      \\
  cin >> kkk;
#define dbgArr(a, n)                                                           \\
  cout << "Array " << #a << endl;                                              \\
  for (ll i = 0; i < n; i++)                                                   \\
    cout << a[i] << " ";                                                       \\
  cout << endl;
#define maxE(a, n) *max_element(a, a + n);
#define minE(a, n) *min_element(a, a + n);
#define forn(i, s, n) for (ll i = s; i < n; i++)
#define fornr(i, e, s) for (ll i = e - 1; i >= s; i--)
#define arr1(a, n)                                                             \\
  for (ll i = 1; i <= n; i++) {                                                \\
    cin >> a[i];                                                               \\
  }
#define file                                                                   \\
  freopen("input.txt", "r", stdin);                                            \\
  freopen("output.txt", "w", stdout)
#define print cout << "Case #" << number << ": "
#define all(x) x.begin(), x.end()
ll power(ll x, ll b) {
  ll p = 1;
  while (b > 0) {
    if (b & 1) {
      p = p * x;
      p %= mod;
    }
    b >>= 1;
    x *= x;
    x %= mod;
  }
  return p % mod;
}
using namespace std;
#define INF 1e15
struct lex_compare {
  bool operator()(const pair<ll, pp(ll)> p1, const pair<ll, pp(ll)> p2) {
    return (p1.first == p2.first) ? p1.ss.ff < p2.ss.ff : p1.first > p2.first;
  }
};
ll n;
ll rtn;

bool comp(pp(ll) p1, pp(ll) p2) {
  if (p1.ff / rtn == p2.ff / rtn)
    return p1.ss < p2.ss;
  else
    return p1.ff / rtn < p2.ff / rtn;
}

int main() {
  FIO;
  ll q;
  cin >> n >> q;
  rtn = sqrt(n);
  ll arr[n];
  forn(i, 0, n) cin >> arr[i];

  pp(ll) allq[q];
  pp(ll) finalq[q];
  map<pp(ll), ll> m;
  forn(i, 0, q) {
    cin >> allq[i].ff >> allq[i].ss;
    allq[i].ff -= 1;
    allq[i].ss -= 1;
    finalq[i].ff = allq[i].ff;
    finalq[i].ss = allq[i].ss;
  }
  sort(allq, allq + q, comp);
  ll freq[n + 5] = {0};
  ;
  ll startmo = allq[0].ff;
  ll endmo = allq[0].ff - 1;
  ll ansf = 0;
  for (auto i : allq) {
    // dbg2(i.ff, i.ss);
    // dbg2(startmo,endmo);
    while (startmo > 0 && startmo > i.ff) {
      startmo--;
      freq[arr[startmo]]++;
      if (freq[arr[startmo]] == 1)
        ansf++;
    }
    while (startmo < n && startmo < i.ff) {

      freq[arr[startmo]]--;
      if (freq[arr[startmo]] == 0)
        ansf--;
      startmo++;
    }

    while (endmo < n - 1 && endmo < i.ss) {
      endmo++;
      freq[arr[endmo]]++;
      if (freq[arr[endmo]] == 1)
        ansf++;
    }
    while (endmo > 0 && endmo > i.ss) {

      freq[arr[endmo]]--;
      if (freq[arr[endmo]] == 0)
        ansf--;
      endmo--;
    }
    // dbg(ansf);
    // dbg2(startmo, endmo);
    m[i] = ansf;
  }
  for (auto i : finalq) {
    cout << m[i] << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:


#include <bits/stdc++.h>
#define ll long long int
#define mod 1000000000000000001
#define pi 3.14159265358979323846264338327950288419716939937510
#define setbit(x) __builtin_popcountll(x)
#define db long double
#define pp(x) pair<x, x>
#define ff first
#define ss second
#define FIO                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
#define tc                                                                     \\
  ll tc;                                                                       \\
  cin >> tc;                                                                   \\
  for (ll number = 1; number <= tc; number++)
#define vv(x) vector<x>
#define pb push_back
#define pob pop_back
#define pf push_front
#define pof pop_front
#define endl "\\n"
#define pq priority_queue
#define dbg(x)                                                                 \\
  { cout << #x << ": " << (x) << endl; }
#define dbg2(x, y)                                                             \\
  { cout << #x << ": " << (x) << " , " << #y << ": " << (y) << endl; }
#define bp                                                                     \\
  ll kkk;                                                                      \\
  cin >> kkk;
#define dbgArr(a, n)                                                           \\
  cout << "Array " << #a << endl;                                              \\
  for (ll i = 0; i < n; i++)                                                   \\
    cout << a[i] << " ";                                                       \\
  cout << endl;
#define maxE(a, n) *max_element(a, a + n);
#define minE(a, n) *min_element(a, a + n);
#define forn(i, s, n) for (ll i = s; i < n; i++)
#define fornr(i, e, s) for (ll i = e - 1; i >= s; i--)
#define arr1(a, n)                                                             \\
  for (ll i = 1; i <= n; i++) {                                                \\
    cin >> a[i];                                                               \\
  }
#define file                                                                   \\
  freopen("input.txt", "r", stdin);                                            \\
  freopen("output.txt", "w", stdout)
#define print cout << "Case #" << number << ": "
#define all(x) x.begin(), x.end()
ll power(ll x, ll b) {
  ll p = 1;
  while (b > 0) {
    if (b & 1) {
      p = p * x;
      p %= mod;
    }
    b >>= 1;
    x *= x;
    x %= mod;
  }
  return p % mod;
}
using namespace std;
#define INF 1e15
struct lex_compare {
  bool operator()(const pair<ll, pp(ll)> p1, const pair<ll, pp(ll)> p2) {
    return (p1.first == p2.first) ? p1.ss.ff < p2.ss.ff : p1.first > p2.first;
  }
};
ll n;
ll rtn;

bool comp(pp(ll) p1, pp(ll) p2) {
  if (p1.ff / rtn == p2.ff / rtn)
    return p1.ss < p2.ss;
  else
    return p1.ff / rtn < p2.ff / rtn;
}

int main() {
  FIO;
  ll q;
  cin >> n >> q;
  rtn = sqrt(n);
  ll arr[n];
  forn(i, 0, n) cin >> arr[i];

  pp(ll) allq[q];
  pp(ll) finalq[q];
  map<pp(ll), ll> m;
  forn(i, 0, q) {
    cin >> allq[i].ff >> allq[i].ss;
    allq[i].ff -= 1;
    allq[i].ss -= 1;
    finalq[i].ff = allq[i].ff;
    finalq[i].ss = allq[i].ss;
  }
  sort(allq, allq + q, comp);
  map<ll, ll> freq;
  ll startmo = allq[0].ff;
  ll endmo = allq[0].ff - 1;
  ll ansf = 0;
  for (auto i : allq) {
    // dbg2(i.ff, i.ss);
    // dbg2(startmo,endmo);
    while (startmo > 0 && startmo > i.ff) {
      startmo--;
      freq[arr[startmo]]++;
      if (freq[arr[startmo]] == 1)
        ansf++;
    }
    while (startmo < n && startmo < i.ff) {

      freq[arr[startmo]]--;
      if (freq[arr[startmo]] == 0)
        ansf--;
      startmo++;
    }

    while (endmo < n - 1 && endmo < i.ss) {
      endmo++;
      freq[arr[endmo]]++;
      if (freq[arr[endmo]] == 1)
        ansf++;
    }
    while (endmo > 0 && endmo > i.ss) {

      freq[arr[endmo]]--;
      if (freq[arr[endmo]] == 0)
        ansf--;
      endmo--;
    }
    // dbg(ansf);
    // dbg2(startmo, endmo);
    m[i] = ansf;
  }
  for (auto i : finalq) {
    cout << m[i] << endl;
  }
  return 0;
}