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

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

#define $(x) (int)((x).size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define endl '\\n'
#define x first
#define y second
// #define int long long
#define double long double
#define pii pair<int, int>
#define pb push_back
#define vec vector
#define beg begin
#define dbg(x) cerr << #x << " = " << x << endl;

template <class T> ostream &operator<<(ostream &str, vector<T> &a) {
  for (auto &i : a) {
    str << i << " ";
  }
  return str;
}

template <class T> istream &operator>>(istream &str, vector<T> &a) {
  for (auto &i : a) {
    str >> i;
  }
  return str;
}

template <class T> ostream &operator<<(ostream &str, pair<T, T> &a) {
  str << a.first << " " << a.second;
  return str;
}

template <class T> istream &operator>>(istream &str, pair<T, T> &a) {
  str >> a.first >> a.second;
  return str;
}

void solve();
signed main() {
#ifdef LOCAL
  freopen("C:\\\\Users\\\\artma\\\\CLionProjects\\\\problems\\\\input.txt", "r", stdin);
#endif
  srand(time(0));
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cout.setf(ios::fixed);
  cout.precision(6);
  solve();
  return 0;
}

const int MOD = 1e9 + 7, MAXN = 5e5 + 10, INF = 1e18 + 10, BASE = 37;

struct bit {
  vec<int> t;
  bit() { t.resize(MAXN); }
  void upd(int i, int x) {
    while (i < MAXN) {
      t[i] += x;
      i = i | (i + 1);
    }
  }
  int sum(int i) {
    int cur = 0;
    while (i >= 0) {
      cur += t[i];
      i = (i & (i + 1)) - 1;
    }
    return cur;
  }
};

void solve() {
  int n, q;
  cin >> n >> q;
  vec<int> c(n + 1);
  for (int i = 1; i <= n; ++i) {
    cin >> c[i];
  }
  vec<pii> qs(q);
  for (int i = 0; i < q; ++i) {
    int l, r;
    cin >> l >> r;
    qs[i] = {l, r};
  }

  vec<int> nxt(n + 1);
  map<int, int> id;
  for (auto it : c) {
    id[it] = n + 1;
  }
  for (int i = n; i >= 1; --i) {
    nxt[i] = id[c[i]];
    id[c[i]] = i;
  }

  bit t;
  map<pii, int> ans;
  vec<vec<int>> R(MAXN), L(MAXN);
  set<pii> se;
  for (auto it : qs) {
    se.insert(it);
  }
  for (auto it : se) {
    R[it.y].pb(it.x);
    L[it.x].pb(it.y);
  }

  for (int i = 1; i <= n; ++i) {
    for (auto r : L[i]) {
      ans[{i, r}] -= (t.sum(n + 1) - t.sum(r));
    }
    t.upd(nxt[i], 1);
    for (auto l : R[i]) {
      ans[{l, i}] += (t.sum(n + 1) - t.sum(i));
    }
  }
  for (auto it : qs) {
    cout << ans[it] << endl;
  }
}

Error: TLE

Fixed submission:

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

#define $(x) (int)((x).size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define endl '\\n'
#define x first
#define y second
#define int long long
#define double long double
#define pii pair<int, int>
#define pb push_back
#define vec vector
#define beg begin
#define dbg(x) cerr << #x << " = " << x << endl;

template <class T> ostream &operator<<(ostream &str, vector<T> &a) {
  for (auto &i : a) {
    str << i << " ";
  }
  return str;
}

template <class T> istream &operator>>(istream &str, vector<T> &a) {
  for (auto &i : a) {
    str >> i;
  }
  return str;
}

template <class T> ostream &operator<<(ostream &str, pair<T, T> &a) {
  str << a.first << " " << a.second;
  return str;
}

template <class T> istream &operator>>(istream &str, pair<T, T> &a) {
  str >> a.first >> a.second;
  return str;
}

void solve();
signed main() {
#ifdef LOCAL
  freopen("C:\\\\Users\\\\artma\\\\CLionProjects\\\\problems\\\\input.txt", "r", stdin);
#endif
  srand(time(0));
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cout.setf(ios::fixed);
  cout.precision(6);
  solve();
  return 0;
}

const int MOD = 1e9 + 7, MAXN = 5e5 + 10, INF = 1e18 + 10, BASE = 37;

struct bit {
  vec<int> t;
  bit() { t.resize(MAXN); }
  void upd(int i, int x) {
    while (i < MAXN) {
      t[i] += x;
      i = i | (i + 1);
    }
  }
  int sum(int i) {
    int cur = 0;
    while (i >= 0) {
      cur += t[i];
      i = (i & (i + 1)) - 1;
    }
    return cur;
  }
};

void solve() {
  int n, q;
  cin >> n >> q;
  vec<int> c(n + 1);
  for (int i = 1; i <= n; ++i) {
    cin >> c[i];
  }
  vec<pii> qs(q);
  for (int i = 0; i < q; ++i) {
    int l, r;
    cin >> l >> r;
    qs[i] = {l, r};
  }

  vec<int> nxt(n + 1);
  map<int, int> id;
  for (auto it : c) {
    id[it] = n + 1;
  }
  for (int i = n; i >= 1; --i) {
    nxt[i] = id[c[i]];
    id[c[i]] = i;
  }

  bit t;
  map<pii, int> ans;
  vec<vec<int>> R(MAXN), L(MAXN);
  set<pii> se;
  for (auto it : qs) {
    se.insert(it);
  }
  for (auto it : se) {
    R[it.y].pb(it.x);
    L[it.x].pb(it.y);
  }

  for (int i = 1; i <= n; ++i) {
    for (auto r : L[i]) {
      ans[{i, r}] -= (t.sum(MAXN) - t.sum(r));
    }
    t.upd(nxt[i], 1);
    for (auto l : R[i]) {
      ans[{l, i}] += (t.sum(MAXN) - t.sum(i));
    }
  }
  for (auto it : qs) {
    cout << ans[it] << endl;
  }
}