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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
// #include <boost/multiprecision/cpp_ll.hpp>
// typedef boost::multiprecision::cpp_ll ll;
typedef long double dd;
#define i_7 (ll)(1E9 + 7)
// #define i_7 998244353
#define i_5 i_7 - 2
ll mod(ll a) {
  ll c = a % i_7;
  if (c >= 0)
    return c;
  return c + i_7;
}
typedef pair<ll, ll> l_l;
ll inf = (ll)1E18;
#define rep(i, l, r) for (ll i = l; i <= r; i++)
#define pb push_back
ll max(ll a, ll b) {
  if (a < b)
    return b;
  else
    return a;
}
ll min(ll a, ll b) {
  if (a > b)
    return b;
  else
    return a;
}
void Max(ll &pos, ll val) { pos = max(pos, val); } // Max(dp[n],dp[n-1]);
void Min(ll &pos, ll val) { pos = min(pos, val); }
void Add(ll &pos, ll val) { pos = mod(pos + val); }
dd EPS = 1E-9;
#define endl "\\n"
#define fastio                                                                 \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);                                                                  \\
  cout.tie(0);

struct que {
  ll l, r, flag;
};
bool comp(que a, que b) { return a.r < b.r; }

// セグ木

ll segnodenum;
vector<ll> segnode;

ll init_num = 0;
ll func(ll x, ll y) { return x + y; }

void init(vector<ll> v) {
  segnodenum = 1;
  while (v.size() > segnodenum)
    segnodenum *= 2;
  segnode.resize(2 * segnodenum - 1, init_num);
  rep(i, segnodenum - 1, segnodenum * 2 - 2) {
    if (i - (segnodenum - 1) < v.size()) {
      segnode[i] = segnode[i - (segnodenum - 1)];
    } else {
      segnode[i] = init_num;
    }
  }
  for (ll i = segnodenum - 2; i >= 0; i--) {
    segnode[i] = func(segnode[2 * i + 1], segnode[2 * i + 2]);
  }
}

void update(ll x, ll y) { // v[x]をyに変更する
  segnode[x + segnodenum - 1] = y;
  ll next = x + segnodenum - 1;
  while (next > 0) {
    next = (next - 1) / 2;
    segnode[next] = func(segnode[2 * next + 1], segnode[2 * next + 2]);
  }
}

ll find(ll i, ll j, ll x, ll y, ll z) { // v[[i,j)]でのfuncの結果を出力する
                                        // //segnode[x]に着目しているsegnode[x]が担当する範囲は[y,z);
                                        // //最初はx=-1で
  if (x == -1)
    return find(i, j, 0, 0, segnodenum);
  if (j <= y || z <= i)
    return init_num;
  if (i <= y && z <= j)
    return segnode[x];
  return func(find(i, j, 2 * x + 1, y, (y + z) / 2),
              find(i, j, 2 * x + 2, (y + z) / 2, z));
}

int main() {
  fastio ll n, q;
  cin >> n >> q;
  ll c[n + 1];
  rep(i, 1, n) cin >> c[i];
  vector<ll> V;
  rep(i, 0, n) V.pb(0);
  init(V);
  ll de[n + 1];
  rep(i, 1, n) de[i] = -1;
  vector<que> v;
  rep(i, 0, q - 1) {
    ll l, r;
    cin >> l >> r;
    que q;
    q.l = l;
    q.r = r;
    q.flag = i;
    v.pb(q);
  }
  sort(v.begin(), v.end(), comp);
  ll nowr = 0;
  ll ans[q];
  rep(i, 0, q - 1) {
    ll l, r;
    l = v[i].l;
    r = v[i].r;
    while (nowr < r) {
      nowr++;
      if (de[c[nowr]] == -1) {
        de[c[nowr]] = nowr;
        update(nowr, 1);
      } else {
        update(de[c[nowr]], 0);
        de[c[nowr]] = nowr;
        update(nowr, 1);
      }
    }
    ans[v[i].flag] = find(l, r + 1, -1, 0, 0);
  }
  rep(i, 0, q - 1) { cout << ans[i] << endl; }

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
// #include <boost/multiprecision/cpp_ll.hpp>
// typedef boost::multiprecision::cpp_ll ll;
typedef long double dd;
#define i_7 (ll)(1E9 + 7)
// #define i_7 998244353
#define i_5 i_7 - 2
ll mod(ll a) {
  ll c = a % i_7;
  if (c >= 0)
    return c;
  return c + i_7;
}
typedef pair<ll, ll> l_l;
ll inf = (ll)1E18;
#define rep(i, l, r) for (ll i = l; i <= r; i++)
#define pb push_back
ll max(ll a, ll b) {
  if (a < b)
    return b;
  else
    return a;
}
ll min(ll a, ll b) {
  if (a > b)
    return b;
  else
    return a;
}
void Max(ll &pos, ll val) { pos = max(pos, val); } // Max(dp[n],dp[n-1]);
void Min(ll &pos, ll val) { pos = min(pos, val); }
void Add(ll &pos, ll val) { pos = mod(pos + val); }
dd EPS = 1E-9;
#define endl "\\n"
#define fastio                                                                 \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);                                                                  \\
  cout.tie(0);

struct que {
  ll l, r, flag;
};
bool comp(que a, que b) { return a.r < b.r; }
int main() {
  fastio ll n, q;
  cin >> n >> q;
  ll c[n + 1];
  rep(i, 1, n) cin >> c[i];
  map<ll, ll> mp;
  ll de[n + 1];
  rep(i, 1, n) de[i] = -1;
  vector<que> v;
  rep(i, 0, q - 1) {
    ll l, r;
    cin >> l >> r;
    que q;
    q.l = l;
    q.r = r;
    q.flag = i;
    v.pb(q);
  }
  sort(v.begin(), v.end(), comp);
  ll nowr = 0;
  ll ans[q];
  rep(i, 0, q - 1) {
    ll l, r;
    l = v[i].l;
    r = v[i].r;
    while (nowr < r) {
      nowr++;
      if (de[c[nowr]] == -1) {
        de[c[nowr]] = nowr;
        mp[nowr] = c[nowr];
      } else {
        mp.erase(de[c[nowr]]);
        de[c[nowr]] = nowr;
        mp[nowr] = c[nowr];
      }
    }
    auto itr = mp.lower_bound(l);
    ans[v[i].flag] = distance(itr, mp.end());
  }
  rep(i, 0, q - 1) { cout << ans[i] << endl; }

  return 0;
}