#include <bits/stdc++.h>
using namespace std;
int const INF = (int)1e9 + 1e3;
long long const INFL = (long long)1e18 + 1e6;
mt19937 tw(chrono::high_resolution_clock::now().time_since_epoch().count());
uniform_int_distribution<long long> ll_distr;
uniform_real_distribution<double> d_distr;
long long rnd(long long a, long long b) {
  return ll_distr(tw) % (b - a + 1) + a;
}
double rnd() { return d_distr(tw); }
struct node {
  double y;
  int l, r;
  int x, val, sum, best;
  node() : y(rnd()), l(0), r(0), x(0), val(0), sum(0), best(0) {}
  node(int _x, int _val)
      : y(rnd()), l(0), r(0), x(_x), val(_val), sum(_val), best(_val) {}
};
vector<node> treap;
void t_upd(int v) {
  int vl = treap[v].l;
  int vr = treap[v].r;
  treap[v].sum = treap[vl].sum + treap[v].val + treap[vr].sum;
  treap[v].best = max(max(treap[vl].best, treap[vr].best), treap[v].val);
}
void t_lower_bound(int v, int& vl, int& vr, int val) {
  if (v == 0) {
    vl = vr = 0;
    return;
  }
  if (treap[v].x < val) {
    t_lower_bound(treap[v].r, treap[v].r, vr, val);
    vl = v;
    t_upd(vl);
  } else {
    t_lower_bound(treap[v].l, vl, treap[v].l, val);
    vr = v;
    t_upd(vr);
  }
}
void t_merge(int vl, int vr, int& v) {
  if (vl == 0) {
    v = vr;
    return;
  }
  if (vr == 0) {
    v = vl;
    return;
  }
  if (treap[vl].y > treap[vr].y) {
    t_merge(treap[vl].r, vr, treap[vl].r);
    v = vl;
    t_upd(v);
  } else {
    t_merge(vl, treap[vr].l, treap[vr].l);
    v = vr;
    t_upd(v);
  }
}
int get_right(int v) {
  while (treap[v].r != 0) {
    v = treap[v].r;
  }
  return v;
}
void solve() {
  int n, q;
  cin >> n >> q;
  vector<int> arr;
  map<int, set<int>> where;
  for (int i = 0; i < n; ++i) {
    int num;
    cin >> num;
    arr.push_back(num);
    where[num].insert(i);
  }
  map<int, int> last_change;
  vector<pair<pair<int, int>, pair<pair<int, int>, int>>> edges;
  for (int i = 0; i < q; ++i) {
    int pos, val;
    cin >> pos >> val;
    --pos;
    int old_val = arr[pos];
    if (old_val == val) {
      continue;
    }
    edges.push_back({{last_change[old_val], i + 1},
                     {{*where[old_val].begin(), *--where[old_val].end()},
                      ((int)where[old_val].size())}});
    if (((int)where[val].size())) {
      edges.push_back({{last_change[val], i + 1},
                       {{*where[val].begin(), *--where[val].end()},
                        ((int)where[val].size())}});
    }
    last_change[old_val] = i + 1;
    last_change[val] = i + 1;
    where[old_val].erase(pos);
    where[val].insert(pos);
    arr[pos] = val;
  }
  for (auto& p : where) {
    if (((int)p.second.size())) {
      edges.push_back(
          {{last_change[p.first], q + 1},
           {{*p.second.begin(), *--p.second.end()}, ((int)p.second.size())}});
    }
  }
  int bpv = 1;
  while (bpv < q + 1) {
    bpv *= 2;
  }
  vector<vector<pair<pair<int, int>, int>>> here(bpv * 2);
  for (auto e : edges) {
    int l = e.first.first + bpv;
    int r = e.first.second + bpv;
    while (l < r) {
      if (l & 1) {
        here[l++].push_back(e.second);
      }
      if (r & 1) {
        here[--r].push_back(e.second);
      }
      l /= 2;
      r /= 2;
    }
  }
  vector<int> ans(q + 1);
  treap.push_back(node());
  int root = 0;
  for (int i = 0; i <= n; ++i) {
    treap.push_back(node(i, 0));
    t_merge(root, ((int)treap.size()) - 1, root);
  }
  vector<int> order;
  function<void(int, int, int)> dfs = [&](int v, int l, int r) {
    if (l >= q + 1) {
      return;
    }
    vector<tuple<int, int, int>> changes;
    for (auto e : here[v]) {
      int val = e.second;
      int lg = e.first.first;
      int rg = e.first.second;
      int vl, vm, vr;
      t_lower_bound(root, vl, vr, rg + 1);
      t_lower_bound(vl, vl, vm, lg + 1);
      int nv = get_right(vl);
      lg = treap[nv].x;
      changes.push_back({lg, vm, treap[nv].val});
      val = max(val, max(treap[vm].best, treap[nv].val));
      order.clear();
      int cur = vl;
      while (treap[cur].r) {
        order.push_back(cur);
        cur = treap[cur].r;
      }
      order.push_back(cur);
      treap[nv].val = val;
      reverse(order.begin(), order.end());
      for (int u : order) {
        t_upd(u);
      }
      t_merge(vl, vr, root);
    }
    if (l + 1 == r) {
      ans[l] = treap[root].sum;
    } else {
      int m = (l + r) / 2;
      dfs(v * 2, l, m);
      dfs(v * 2 + 1, m, r);
    }
    while (((int)changes.size())) {
      int vl, vr;
      auto [lg, vm, val] = changes.back();
      t_lower_bound(root, vl, vr, lg + 1);
      order.clear();
      int cur = vl;
      while (treap[cur].r) {
        order.push_back(cur);
        cur = treap[cur].r;
      }
      order.push_back(cur);
      treap[cur].val = val;
      reverse(order.begin(), order.end());
      for (int u : order) {
        t_upd(u);
      }
      t_merge(vl, vm, vl);
      t_merge(vl, vr, root);
      changes.pop_back();
    }
  };
  dfs(1, 0, bpv);
  for (int i = 0; i <= q; ++i) {
    cout << n - ans[i] << "\n";
  }
}
int main() {
  cout << setprecision(15) << fixed;
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int test_count = 1;
  for (int test = 1; test <= test_count; ++test) {
    solve();
  }
}

### Prompt

Please create a solution in Cpp to the following problem:

This is a harder version of the problem. In this version q ≤ 200 000.

A sequence of integers is called nice if its elements are arranged in blocks like in [3, 3, 3, 4, 1, 1]. Formally, if two elements are equal, everything in between must also be equal.

Let's define difficulty of a sequence as a minimum possible number of elements to change to get a nice sequence. However, if you change at least one element of value x to value y, you must also change all other elements of value x into y as well. For example, for [3, 3, 1, 3, 2, 1, 2] it isn't allowed to change first 1 to 3 and second 1 to 2. You need to leave 1's untouched or change them to the same value.

You are given a sequence of integers a_1, a_2, …, a_n and q updates.

Each update is of form "i x" — change a_i to x. Updates are not independent (the change stays for the future).

Print the difficulty of the initial sequence and of the sequence after every update.

Input

The first line contains integers n and q (1 ≤ n ≤ 200 000, 0 ≤ q ≤ 200 000), the length of the sequence and the number of the updates.

The second line contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 200 000), the initial sequence.

Each of the following q lines contains integers i_t and x_t (1 ≤ i_t ≤ n, 1 ≤ x_t ≤ 200 000), the position and the new value for this position.

Output

Print q+1 integers, the answer for the initial sequence and the answer after every update.

Example

Input


5 6
1 2 1 2 1
2 1
4 1
5 3
2 3
4 2
2 1


Output


2
1
0
0
2
3
0

### Response

```cpp
#include <bits/stdc++.h>
using namespace std;
int const INF = (int)1e9 + 1e3;
long long const INFL = (long long)1e18 + 1e6;
mt19937 tw(chrono::high_resolution_clock::now().time_since_epoch().count());
uniform_int_distribution<long long> ll_distr;
uniform_real_distribution<double> d_distr;
long long rnd(long long a, long long b) {
  return ll_distr(tw) % (b - a + 1) + a;
}
double rnd() { return d_distr(tw); }
struct node {
  double y;
  int l, r;
  int x, val, sum, best;
  node() : y(rnd()), l(0), r(0), x(0), val(0), sum(0), best(0) {}
  node(int _x, int _val)
      : y(rnd()), l(0), r(0), x(_x), val(_val), sum(_val), best(_val) {}
};
vector<node> treap;
void t_upd(int v) {
  int vl = treap[v].l;
  int vr = treap[v].r;
  treap[v].sum = treap[vl].sum + treap[v].val + treap[vr].sum;
  treap[v].best = max(max(treap[vl].best, treap[vr].best), treap[v].val);
}
void t_lower_bound(int v, int& vl, int& vr, int val) {
  if (v == 0) {
    vl = vr = 0;
    return;
  }
  if (treap[v].x < val) {
    t_lower_bound(treap[v].r, treap[v].r, vr, val);
    vl = v;
    t_upd(vl);
  } else {
    t_lower_bound(treap[v].l, vl, treap[v].l, val);
    vr = v;
    t_upd(vr);
  }
}
void t_merge(int vl, int vr, int& v) {
  if (vl == 0) {
    v = vr;
    return;
  }
  if (vr == 0) {
    v = vl;
    return;
  }
  if (treap[vl].y > treap[vr].y) {
    t_merge(treap[vl].r, vr, treap[vl].r);
    v = vl;
    t_upd(v);
  } else {
    t_merge(vl, treap[vr].l, treap[vr].l);
    v = vr;
    t_upd(v);
  }
}
int get_right(int v) {
  while (treap[v].r != 0) {
    v = treap[v].r;
  }
  return v;
}
void solve() {
  int n, q;
  cin >> n >> q;
  vector<int> arr;
  map<int, set<int>> where;
  for (int i = 0; i < n; ++i) {
    int num;
    cin >> num;
    arr.push_back(num);
    where[num].insert(i);
  }
  map<int, int> last_change;
  vector<pair<pair<int, int>, pair<pair<int, int>, int>>> edges;
  for (int i = 0; i < q; ++i) {
    int pos, val;
    cin >> pos >> val;
    --pos;
    int old_val = arr[pos];
    if (old_val == val) {
      continue;
    }
    edges.push_back({{last_change[old_val], i + 1},
                     {{*where[old_val].begin(), *--where[old_val].end()},
                      ((int)where[old_val].size())}});
    if (((int)where[val].size())) {
      edges.push_back({{last_change[val], i + 1},
                       {{*where[val].begin(), *--where[val].end()},
                        ((int)where[val].size())}});
    }
    last_change[old_val] = i + 1;
    last_change[val] = i + 1;
    where[old_val].erase(pos);
    where[val].insert(pos);
    arr[pos] = val;
  }
  for (auto& p : where) {
    if (((int)p.second.size())) {
      edges.push_back(
          {{last_change[p.first], q + 1},
           {{*p.second.begin(), *--p.second.end()}, ((int)p.second.size())}});
    }
  }
  int bpv = 1;
  while (bpv < q + 1) {
    bpv *= 2;
  }
  vector<vector<pair<pair<int, int>, int>>> here(bpv * 2);
  for (auto e : edges) {
    int l = e.first.first + bpv;
    int r = e.first.second + bpv;
    while (l < r) {
      if (l & 1) {
        here[l++].push_back(e.second);
      }
      if (r & 1) {
        here[--r].push_back(e.second);
      }
      l /= 2;
      r /= 2;
    }
  }
  vector<int> ans(q + 1);
  treap.push_back(node());
  int root = 0;
  for (int i = 0; i <= n; ++i) {
    treap.push_back(node(i, 0));
    t_merge(root, ((int)treap.size()) - 1, root);
  }
  vector<int> order;
  function<void(int, int, int)> dfs = [&](int v, int l, int r) {
    if (l >= q + 1) {
      return;
    }
    vector<tuple<int, int, int>> changes;
    for (auto e : here[v]) {
      int val = e.second;
      int lg = e.first.first;
      int rg = e.first.second;
      int vl, vm, vr;
      t_lower_bound(root, vl, vr, rg + 1);
      t_lower_bound(vl, vl, vm, lg + 1);
      int nv = get_right(vl);
      lg = treap[nv].x;
      changes.push_back({lg, vm, treap[nv].val});
      val = max(val, max(treap[vm].best, treap[nv].val));
      order.clear();
      int cur = vl;
      while (treap[cur].r) {
        order.push_back(cur);
        cur = treap[cur].r;
      }
      order.push_back(cur);
      treap[nv].val = val;
      reverse(order.begin(), order.end());
      for (int u : order) {
        t_upd(u);
      }
      t_merge(vl, vr, root);
    }
    if (l + 1 == r) {
      ans[l] = treap[root].sum;
    } else {
      int m = (l + r) / 2;
      dfs(v * 2, l, m);
      dfs(v * 2 + 1, m, r);
    }
    while (((int)changes.size())) {
      int vl, vr;
      auto [lg, vm, val] = changes.back();
      t_lower_bound(root, vl, vr, lg + 1);
      order.clear();
      int cur = vl;
      while (treap[cur].r) {
        order.push_back(cur);
        cur = treap[cur].r;
      }
      order.push_back(cur);
      treap[cur].val = val;
      reverse(order.begin(), order.end());
      for (int u : order) {
        t_upd(u);
      }
      t_merge(vl, vm, vl);
      t_merge(vl, vr, root);
      changes.pop_back();
    }
  };
  dfs(1, 0, bpv);
  for (int i = 0; i <= q; ++i) {
    cout << n - ans[i] << "\n";
  }
}
int main() {
  cout << setprecision(15) << fixed;
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int test_count = 1;
  for (int test = 1; test <= test_count; ++test) {
    solve();
  }
}

```