Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
- // #pragma GCC target("sse,sse2,sse3,ssse3,sse4,sse4.1,sse4.2,popcnt,abm,mmx,avx")
- // #pragma comment(linker, "/stack:200000000"]
- #include <iostream>
- #include <vector>
- #include <cmath>
- #include <algorithm>
- #include <unordered_set>
- #include <unordered_map>
- #include <set>
- #include <map>
- #include <queue>
- #include <deque>
- #include <bitset>
- #include <stack>
- #include <random>
- #include <fstream>
- #include <sstream>
- #include <chrono>
- #define fi first
- #define se second
- #define pb push_back
- #define ll long long
- #define ld long double
- #define hm unordered_map
- #define pii pair<int, int>
- #define sz(a) (int)a.size()
- #define all(a) a.begin(), a.end()
- #define cinv(v) for (auto& x: v) cin >> x
- #define fr(i, n) for (int i = 0; i < n; ++i)
- #define fl(i, l, n) for (int i = l; i < n; ++i)
- #define int ll
- template <typename T1, typename T2> inline bool chkmin(T1 &x, const T2 &y) {if (x > y) {x = y; return 1;} return 0;}
- template <typename T1, typename T2> inline bool chkmax(T1 &x, const T2 &y) {if (x < y) {x = y; return 1;} return 0;}
- using namespace std;
- #ifdef LOCAL
- #define dbg(x) cerr << #x << " : " << x << '\n'
- const int maxn = 20;
- #else
- #define dbg(x)
- const int maxn = 2e5 + 20;
- #endif
- //tg: @galebickosikasa
- ostream& operator << (ostream& out, vector<int>& v) {
- for (auto& x: v) out << x << ' ';
- return out;
- }
- ostream& operator << (ostream& out, pii& v) {
- out << v.fi << ", " << v.se;
- return out;
- }
- istream& operator >> (istream& in, pii& a) {
- in >> a.fi >> a.se;
- return in;
- }
- const ll inf = (ll) 2e9;
- const ld pi = asin (1) * 2;
- const ld eps = 1e-8;
- const ll mod = (ll)1e9 + 7;
- const ll ns = 97;
- mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
- struct tree {
- vector<int> assignmx, assignmn, t;
- int size = 0, capacity;
- tree (int n) {
- while ((1<<size) < n) ++size;
- ++size;
- size = (1<<size);
- capacity = size>>1;
- t = vector<int> (size);
- assignmx = assignmn = vector<int> (size, -1);
- }
- void push (int v) {
- if (assignmx[v] != -1) {
- assignmx[v * 2] = assignmx[v * 2 + 1] = assignmx[v];
- assignmx[v] = -1;
- }
- if (assignmn[v] != -1) {
- assignmn[v * 2] = assignmn[v * 2 + 1] = assignmn[v];
- assignmn[v] = -1;
- }
- }
- void render (int v) {
- t[v] = 0;
- if (assignmn[v * 2] != -1) {
- chkmax (t[v], assignmx[v * 2] - assignmn[v * 2]);
- } else {
- chkmax (t[v], t[v * 2]);
- }
- if (assignmn[v * 2 + 1] != -1) {
- chkmax (t[v], assignmx[v * 2 + 1] - assignmn[v * 2 + 1]);
- } else {
- chkmax (t[v], t[v * 2 + 1]);
- }
- }
- void set (int cur, int left, int right, int l, int r, int a, int b) {
- if (cur >= size) return;
- if (left > r || right < l) return;
- if (l <= left && r >= right) {
- assignmn[cur] = a;
- assignmx[cur] = b;
- } else {
- push (cur);
- set (cur * 2, left, (left + right) / 2, l, r, a, b);
- set (cur * 2 + 1, (left + right) / 2 + 1, right, l, r, a, b);
- render (cur);
- }
- }
- void set (int l, int r, int a, int b) {
- set (1, 0, capacity - 1, l, r, a, b);
- }
- pii get (int cur, int left, int right, int x) {
- int a = -1, b = -1;
- if (left > x || right < x) return {a, b};
- a = assignmn[cur];
- b = assignmx[cur];
- if (cur * 2 >= size) return {a, b};
- pii ptt = {-1, -1};
- push (cur);
- chkmax (ptt, get (cur * 2, left, (left + right) / 2, x));
- chkmax (ptt, get (cur * 2 + 1, (left + right) / 2 + 1, right, x));
- render (cur);
- return ptt;
- }
- pii get (int x) {
- return get (1, 0, capacity - 1, x);
- }
- int get () {
- push (1);
- render (1);
- return t[1];
- }
- };
- signed main () {
- ios_base::sync_with_stdio(false);
- cin.tie(nullptr);
- cout.tie(nullptr);
- int n, q;
- cin >> n >> q;
- vector<int> goo (n);
- cinv (goo);
- for (auto& x: goo) --x;
- tree t (n);
- fr (i, n) t.set (i, i, i, i);
- vector<int> pos (n, -1);
- fr (i, n) {
- pos[goo[i]] = i;
- if (goo[i] > 0 && pos[goo[i] - 1] != -1) {
- auto ptt = t.get (goo[i] - 1);
- ptt.se = goo[i];
- t.set (ptt.fi, ptt.se, ptt.fi, ptt.se);
- }
- }
- cout << n - t.get () - 1 << '\n';
- fr (i, q) {
- int a, b;
- cin >> a >> b;
- --a, --b;
- {
- int x = goo[a];
- auto ptt = t.get (x);
- if (x > 0 && ptt.fi <= x - 1) {
- t.set (ptt.fi, x - 1, ptt.fi, x - 1);
- }
- if (x + 1 < n && x + 1 <= ptt.se) {
- t.set (x + 1, ptt.se, x + 1, ptt.se);
- }
- t.set (x, x, x, x);
- pos[x] = b;
- if (x > 0 && pos[x] > pos[x - 1]) {
- ptt = t.get (x - 1);
- t.set (ptt.fi, x, ptt.fi, x);
- }
- if (x + 1 < n && pos[x + 1] > pos[x]) {
- auto ptt1 = t.get (x);
- ptt = t.get (x + 1);
- chkmin (ptt.fi, ptt1.fi);
- chkmax (ptt.se, ptt1.se);
- t.set (ptt.fi, ptt.se, ptt.fi, ptt.se);
- }
- }
- {
- int x = goo[b];
- auto ptt = t.get (x);
- if (x > 0 && ptt.fi <= x - 1) {
- t.set (ptt.fi, x - 1, ptt.fi, x - 1);
- }
- if (x + 1 < n && x + 1 <= ptt.se) {
- t.set (x + 1, ptt.se, x + 1, ptt.se);
- }
- t.set (x, x, x, x);
- pos[x] = a;
- if (x > 0 && pos[x] > pos[x - 1]) {
- ptt = t.get (x - 1);
- t.set (ptt.fi, x, ptt.fi, x);
- }
- if (x + 1 < n && pos[x + 1] > pos[x]) {
- auto ptt1 = t.get (x);
- ptt = t.get (x + 1);
- chkmin (ptt.fi, ptt1.fi);
- chkmax (ptt.se, ptt1.se);
- t.set (ptt.fi, ptt.se, ptt.fi, ptt.se);
- }
- }
- swap (goo[a], goo[b]);
- cout << n - t.get () - 1 << '\n';
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement