Problem p02599 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <stdio.h>
using namespace std;

#define mx 300008

#define pii pair<int, int>
#define pi acos(-1.0)
#define pb push_back
#define ss second
#define ff first
typedef long long int ll;
/// adjacent move
int fx[4] = {0, +1, 0, -1};
int fy[4] = {+1, 0, -1, 0};
/// knight
ll prime[mx];

ll a[mx], tree[4 * mx];
ll n, m;
void build(ll node, ll s, ll e) {
  if (s == e) {
    tree[node] = a[s];
    return;
  }
  ll mid = (s + e) / 2;
  ll left = node * 2;
  ll right = node * 2 + 1;
  build(left, s, mid);
  build(right, mid + 1, e);
  tree[node] = (tree[left] + tree[right]);
}
void update(ll node, ll s, ll e, ll idx, ll val) {
  if (s > idx || e < idx)
    return;
  if (s == e && s == idx) {

    tree[node] = val;
    return;
  }
  ll mid = (s + e) / 2;
  ll left = 2 * node;
  ll right = left + 1;
  update(left, s, mid, idx, val);
  update(right, mid + 1, e, idx, val);
  tree[node] = tree[left] + tree[right];
}
ll query(ll node, ll s, ll e, ll i, ll j) {
  if (i > e || j < s)
    return 0;
  if (s >= i && e <= j)
    return tree[node];
  ll mid = (s + e) / 2;
  ll left = 2 * node;
  ll right = left + 1;
  ll x = query(left, s, mid, i, j);
  ll y = query(right, mid + 1, e, i, j);
  return (x + y);
}
int last[mx];
int tot[mx];
int ans[mx];
vector<pii> v[mx];
int main() {
  int t = 1;
  // cin>>t;
  int c = 0;
  while (t--) {
    ll n, m;
    memset(ans, 0, sizeof(ans));
    memset(last, 0, sizeof(last));
    memset(v, 0, sizeof(v));
    memset(tree, 0, sizeof(tree));
    cin >> n >> m;
    for (int i = 1; i <= n; i++)
      cin >> a[i];
    for (int i = 1; i <= m; i++) {
      int l, r;
      cin >> l >> r;
      v[r].push_back({l, i});
    }

    for (int i = 1; i <= n; i++) {
      update(1, 1, n, i, 1);
      if (last[a[i]] != 0)
        update(1, 1, n, last[a[i]], 0);
      last[a[i]] = i;
      for (int j = 0; j < v[i].size(); j++) {
        int idx = v[i][j].ss;
        int val = v[i][j].ff;
        ans[idx] = query(1, 1, n, val, i);
      }
    }

    for (int i = 1; i <= m; i++)
      cout << ans[i] << "\n";
  }
}


Fixed submission:
#include <bits/stdc++.h>
#include <stdio.h>
using namespace std;

#define mx 500008

#define pii pair<int, int>
#define pi acos(-1.0)
#define pb push_back
#define ss second
#define ff first
typedef long long int ll;
/// adjacent move
int fx[4] = {0, +1, 0, -1};
int fy[4] = {+1, 0, -1, 0};
/// knight
ll prime[mx];

ll a[mx], tree[4 * mx];
ll n, m;
void build(ll node, ll s, ll e) {
  if (s == e) {
    tree[node] = a[s];
    return;
  }
  ll mid = (s + e) / 2;
  ll left = node * 2;
  ll right = node * 2 + 1;
  build(left, s, mid);
  build(right, mid + 1, e);
  tree[node] = (tree[left] + tree[right]);
}
void update(ll node, ll s, ll e, ll idx, ll val) {
  if (s > idx || e < idx)
    return;
  if (s == e && s == idx) {

    tree[node] = val;
    return;
  }
  ll mid = (s + e) / 2;
  ll left = 2 * node;
  ll right = left + 1;
  update(left, s, mid, idx, val);
  update(right, mid + 1, e, idx, val);
  tree[node] = tree[left] + tree[right];
}
ll query(ll node, ll s, ll e, ll i, ll j) {
  if (i > e || j < s)
    return 0;
  if (s >= i && e <= j)
    return tree[node];
  ll mid = (s + e) / 2;
  ll left = 2 * node;
  ll right = left + 1;
  ll x = query(left, s, mid, i, j);
  ll y = query(right, mid + 1, e, i, j);
  return (x + y);
}
int last[mx];
int tot[mx];
int ans[mx];
vector<pii> v[mx];
int main() {
  int t = 1;
  // cin>>t;
  int c = 0;
  while (t--) {
    ll n, m;
    memset(ans, 0, sizeof(ans));
    memset(last, 0, sizeof(last));
    memset(v, 0, sizeof(v));
    memset(tree, 0, sizeof(tree));
    cin >> n >> m;
    for (int i = 1; i <= n; i++)
      cin >> a[i];
    for (int i = 1; i <= m; i++) {
      int l, r;
      cin >> l >> r;
      v[r].push_back({l, i});
    }

    for (int i = 1; i <= n; i++) {
      update(1, 1, n, i, 1);
      if (last[a[i]] != 0)
        update(1, 1, n, last[a[i]], 0);
      last[a[i]] = i;
      for (int j = 0; j < v[i].size(); j++) {
        int idx = v[i][j].ss;
        int val = v[i][j].ff;
        ans[idx] = query(1, 1, n, val, i);
      }
    }

    for (int i = 1; i <= m; i++)
      cout << ans[i] << "\n";
  }
}