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



#include <bits/stdc++.h>

using namespace std;

/*helpfull*/
using vl = vector<long long>;
using vpl = vector<pair<long long, long long>>;
using vs = vector<string>;
using pl = pair<long long, long long>;
using ll = long long int;
using vb = vector<bool>;

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;
  }
};
// #ifndef ONLINE_JUDGE
// 	freopen("input.txt", "r", stdin);
// 	freopen("output.txt", "w", stdout);
// #endif

// shortcut macros
#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define mp make_pair
#define fi first
#define se second
// #define mt              make_tuple
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define ini(a, v) memset(a, v, sizeof(a))
#define rep(i, a, n) for (ll i = a; i < n; ++i)
#define For(i, n) rep(i, 0, n)
#define sz(x) (int((x).size()))
#define ump(x, y) unordered_map<x, y>
#define umpr(x, y, z) unordered_map<x, y, z>
void printcase(ll &x) { cout << "Case #" << x << ": "; }
void read(vector<ll> &v, ll &n) { For(i, n) cin >> v[i]; }
void read(ll &x) { cin >> x; }
void read(string &s) { cin >> s; }
void read(int &x) { cin >> x; }
void read(ll a[], ll &n) { For(i, n) cin >> a[i]; }
void print() { cout << endl; }
void write(vector<ll> &v, ll &n) {
  For(i, n) cout << v[i] << " ";
  cout << endl;
}
void write(vector<ll> &v) {
  ll n = v.size();
  For(i, n) cout << v[i] << " ";
  cout << endl;
}
void write(ll x) { cout << x << endl; }
void write(string &s) { cout << s << endl; }
void write(int &x) { cout << x << endl; }
void write(ll a[], ll &n) {
  For(i, n) cout << a[i] << " ";
  cout << endl;
}

const ll inf = 1000001;

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

bool cmp(Query x, Query y) { return x.r < y.r; }

void update(ll idx, ll val, ll bit[], ll n) {
  for (; idx <= n; idx += idx & -idx)
    bit[idx] += val;
}

ll query(ll idx, ll bit[], ll n) {
  ll sum = 0;
  for (; idx > 0; idx -= idx & -idx)
    sum += bit[idx];
  return sum;
}

void printall(vl arr, ll n, Query queries[], ll q) {
  ll bit[n + 1];
  memset(bit, 0, sizeof(bit));

  ll last_visit[inf];
  memset(last_visit, -1, sizeof(last_visit));

  ll ans[q];
  ll query_counter = 0;
  for (ll i = 0; i < n; i++) {
    if (last_visit[arr[i]] != -1)
      update(last_visit[arr[i]] + 1, -1, bit, n);

    last_visit[arr[i]] = i;
    update(i + 1, 1, bit, n);

    while (query_counter < q && queries[query_counter].r == i) {
      ans[queries[query_counter].idx] =
          query(queries[query_counter].r + 1, bit, n) -
          query(queries[query_counter].l, bit, n);
      query_counter++;
    }
  }

  for (ll i = 0; i < q; i++)
    cout << ans[i] << endl;
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  IOS;
  ll n, q;
  cin >> n >> q;
  vl v(n);
  read(v, n);
  Query qu[q];
  For(i, q) {
    cin >> qu[i].l >> qu[i].r;
    qu[i].idx = i;
    --qu[i].l;
    --qu[i].r;
  }
  sort(qu, qu + q, cmp);
  printall(v, n, qu, q);
  return 0;
}


Error: TLE

Fixed submission:


#include <bits/stdc++.h>

using namespace std;

/*helpfull*/
using vl = vector<long long>;
using vpl = vector<pair<long long, long long>>;
using vs = vector<string>;
using pl = pair<long long, long long>;
using ll = long long int;
using vb = vector<bool>;

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;
  }
};
// #ifndef ONLINE_JUDGE
// 	freopen("input.txt", "r", stdin);
// 	freopen("output.txt", "w", stdout);
// #endif

// shortcut macros
#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define mp make_pair
#define fi first
#define se second
// #define mt              make_tuple
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define ini(a, v) memset(a, v, sizeof(a))
#define rep(i, a, n) for (ll i = a; i < n; ++i)
#define For(i, n) rep(i, 0, n)
#define sz(x) (int((x).size()))
#define ump(x, y) unordered_map<x, y>
#define umpr(x, y, z) unordered_map<x, y, z>
void printcase(ll &x) { cout << "Case #" << x << ": "; }
void read(vector<ll> &v, ll &n) { For(i, n) cin >> v[i]; }
void read(ll &x) { cin >> x; }
void read(string &s) { cin >> s; }
void read(int &x) { cin >> x; }
void read(ll a[], ll &n) { For(i, n) cin >> a[i]; }
void print() { cout << endl; }
void write(vector<ll> &v, ll &n) {
  For(i, n) cout << v[i] << " ";
  cout << endl;
}
void write(vector<ll> &v) {
  ll n = v.size();
  For(i, n) cout << v[i] << " ";
  cout << endl;
}
void write(ll x) { cout << x << endl; }
void write(string &s) { cout << s << endl; }
void write(int &x) { cout << x << endl; }
void write(ll a[], ll &n) {
  For(i, n) cout << a[i] << " ";
  cout << endl;
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  IOS;
  ll n, q;
  cin >> n >> q;
  vl v(n);
  read(v, n);
  ll l, r;
  while (q--) {
    cin >> l >> r;

    unordered_set<int> s(v.begin() + l - 1, v.begin() + r);
    cout << s.size() << endl;
  }
  return 0;
}