Advertisement
Galebickosikasa

Untitled

Nov 22nd, 2020
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.40 KB | None | 0 0
  1. // #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
  2. // #pragma GCC target("sse,sse2,sse3,ssse3,sse4,sse4.1,sse4.2,popcnt,abm,mmx,avx")
  3. // #pragma comment(linker, "/stack:200000000"]
  4.  
  5. #include <iostream>
  6. #include <vector>
  7. #include <cmath>
  8. #include <algorithm>
  9. #include <unordered_set>
  10. #include <unordered_map>
  11. #include <set>
  12. #include <map>
  13. #include <queue>
  14. #include <deque>
  15. #include <bitset>
  16. #include <stack>
  17. #include <random>
  18. #include <fstream>
  19. #include <sstream>
  20. #include <chrono>
  21.  
  22. #define fi first
  23. #define se second
  24. #define pb push_back
  25. #define ll long long
  26. #define ld long double
  27. #define hm unordered_map
  28. #define pii pair<int, int>
  29. #define sz(a) (int)a.size()
  30. #define all(a) a.begin(), a.end()
  31. #define cinv(v) for (auto& x: v) cin >> x
  32. #define fr(i, n) for (int i = 0; i < n; ++i)
  33. #define fl(i, l, n) for (int i = l; i < n; ++i)
  34.  
  35. #define int ll
  36.  
  37. template <typename T1, typename T2> inline bool chkmin(T1 &x, const T2 &y) {if (x > y) {x = y; return 1;} return 0;}
  38. template <typename T1, typename T2> inline bool chkmax(T1 &x, const T2 &y) {if (x < y) {x = y; return 1;} return 0;}
  39.  
  40. using namespace std;
  41.  
  42. #ifdef LOCAL
  43. #define dbg(x) cerr << #x << " : " << x << '\n'
  44. const int maxn = 20;
  45. #else
  46. #define dbg(x)
  47. const int maxn = 2e5 + 20;
  48. #endif
  49.  
  50. //tg: @galebickosikasa
  51.  
  52. ostream& operator << (ostream& out, vector<int>& v) {
  53. for (auto& x: v) out << x << ' ';
  54. return out;
  55. }
  56.  
  57. ostream& operator << (ostream& out, pii& v) {
  58. out << v.fi << ", " << v.se;
  59. return out;
  60. }
  61.  
  62. istream& operator >> (istream& in, pii& a) {
  63. in >> a.fi >> a.se;
  64. return in;
  65. }
  66.  
  67. const ll inf = (ll) 2e9;
  68. const ld pi = asin (1) * 2;
  69. const ld eps = 1e-8;
  70. const ll mod = (ll)1e9 + 7;
  71. const ll ns = 97;
  72.  
  73. mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
  74.  
  75. struct tree {
  76. vector<int> assignmx, assignmn, t;
  77. int size = 0, capacity;
  78.  
  79. tree (int n) {
  80. while ((1<<size) < n) ++size;
  81. ++size;
  82. size = (1<<size);
  83. capacity = size>>1;
  84. t = vector<int> (size);
  85. assignmx = assignmn = vector<int> (size, -1);
  86. }
  87.  
  88. void push (int v) {
  89. if (assignmx[v] != -1) {
  90. assignmx[v * 2] = assignmx[v * 2 + 1] = assignmx[v];
  91. assignmx[v] = -1;
  92. }
  93. if (assignmn[v] != -1) {
  94. assignmn[v * 2] = assignmn[v * 2 + 1] = assignmn[v];
  95. assignmn[v] = -1;
  96. }
  97. }
  98.  
  99. void render (int v) {
  100. t[v] = 0;
  101. if (assignmn[v * 2] != -1) {
  102. chkmax (t[v], assignmx[v * 2] - assignmn[v * 2]);
  103. } else {
  104. chkmax (t[v], t[v * 2]);
  105. }
  106. if (assignmn[v * 2 + 1] != -1) {
  107. chkmax (t[v], assignmx[v * 2 + 1] - assignmn[v * 2 + 1]);
  108. } else {
  109. chkmax (t[v], t[v * 2 + 1]);
  110. }
  111. }
  112.  
  113. void set (int cur, int left, int right, int l, int r, int a, int b) {
  114. if (cur >= size) return;
  115. if (left > r || right < l) return;
  116. if (l <= left && r >= right) {
  117. assignmn[cur] = a;
  118. assignmx[cur] = b;
  119. } else {
  120. push (cur);
  121. set (cur * 2, left, (left + right) / 2, l, r, a, b);
  122. set (cur * 2 + 1, (left + right) / 2 + 1, right, l, r, a, b);
  123. render (cur);
  124. }
  125. }
  126.  
  127. void set (int l, int r, int a, int b) {
  128. set (1, 0, capacity - 1, l, r, a, b);
  129. }
  130.  
  131. pii get (int cur, int left, int right, int x) {
  132. int a = -1, b = -1;
  133. if (left > x || right < x) return {a, b};
  134. a = assignmn[cur];
  135. b = assignmx[cur];
  136. if (cur * 2 >= size) return {a, b};
  137. pii ptt = {-1, -1};
  138. push (cur);
  139. chkmax (ptt, get (cur * 2, left, (left + right) / 2, x));
  140. chkmax (ptt, get (cur * 2 + 1, (left + right) / 2 + 1, right, x));
  141. render (cur);
  142. return ptt;
  143. }
  144.  
  145. pii get (int x) {
  146. return get (1, 0, capacity - 1, x);
  147. }
  148.  
  149. int get () {
  150. push (1);
  151. render (1);
  152. return t[1];
  153. }
  154. };
  155.  
  156. signed main () {
  157. ios_base::sync_with_stdio(false);
  158. cin.tie(nullptr);
  159. cout.tie(nullptr);
  160. int n, q;
  161. cin >> n >> q;
  162. vector<int> goo (n);
  163. cinv (goo);
  164. for (auto& x: goo) --x;
  165. tree t (n);
  166. fr (i, n) t.set (i, i, i, i);
  167. vector<int> pos (n, -1);
  168. fr (i, n) {
  169. pos[goo[i]] = i;
  170. if (goo[i] > 0 && pos[goo[i] - 1] != -1) {
  171. auto ptt = t.get (goo[i] - 1);
  172. ptt.se = goo[i];
  173. t.set (ptt.fi, ptt.se, ptt.fi, ptt.se);
  174. }
  175. }
  176. cout << n - t.get () - 1 << '\n';
  177. fr (i, q) {
  178. int a, b;
  179. cin >> a >> b;
  180. --a, --b;
  181. {
  182. int x = goo[a];
  183. auto ptt = t.get (x);
  184. if (x > 0 && ptt.fi <= x - 1) {
  185. t.set (ptt.fi, x - 1, ptt.fi, x - 1);
  186. }
  187. if (x + 1 < n && x + 1 <= ptt.se) {
  188. t.set (x + 1, ptt.se, x + 1, ptt.se);
  189. }
  190. t.set (x, x, x, x);
  191. pos[x] = b;
  192. if (x > 0 && pos[x] > pos[x - 1]) {
  193. ptt = t.get (x - 1);
  194. t.set (ptt.fi, x, ptt.fi, x);
  195. }
  196. if (x + 1 < n && pos[x + 1] > pos[x]) {
  197. auto ptt1 = t.get (x);
  198. ptt = t.get (x + 1);
  199. chkmin (ptt.fi, ptt1.fi);
  200. chkmax (ptt.se, ptt1.se);
  201. t.set (ptt.fi, ptt.se, ptt.fi, ptt.se);
  202. }
  203. }
  204. {
  205. int x = goo[b];
  206. auto ptt = t.get (x);
  207. if (x > 0 && ptt.fi <= x - 1) {
  208. t.set (ptt.fi, x - 1, ptt.fi, x - 1);
  209. }
  210. if (x + 1 < n && x + 1 <= ptt.se) {
  211. t.set (x + 1, ptt.se, x + 1, ptt.se);
  212. }
  213. t.set (x, x, x, x);
  214. pos[x] = a;
  215. if (x > 0 && pos[x] > pos[x - 1]) {
  216. ptt = t.get (x - 1);
  217. t.set (ptt.fi, x, ptt.fi, x);
  218. }
  219. if (x + 1 < n && pos[x + 1] > pos[x]) {
  220. auto ptt1 = t.get (x);
  221. ptt = t.get (x + 1);
  222. chkmin (ptt.fi, ptt1.fi);
  223. chkmax (ptt.se, ptt1.se);
  224. t.set (ptt.fi, ptt.se, ptt.fi, ptt.se);
  225. }
  226. }
  227. swap (goo[a], goo[b]);
  228. cout << n - t.get () - 1 << '\n';
  229. }
  230.  
  231.  
  232.  
  233.  
  234.  
  235.  
  236. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement