Guest User

D innopolis

a guest
Nov 22nd, 2020
517
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.64 KB | None | 0 0
  1. //나는 가상 소녀들에게 큰 호감이 있습니다.
  2.  
  3. #include <iostream>
  4. #include <cmath>
  5. #include <algorithm>
  6. #include <stdio.h>
  7. #include <cstring>
  8. #include <string>
  9. #include <cstdlib>
  10. #include <vector>
  11. #include <bitset>
  12. #include <map>
  13. #include <chrono>
  14. #include <functional>
  15. #include <unordered_set>
  16. #include <unordered_map>
  17. #include <numeric>
  18. #include <queue>
  19. #include <ctime>
  20. #include <stack>
  21. #include <set>
  22. #include <list>
  23. #include <deque>
  24. #include <iomanip>
  25. #include <sstream>
  26. #include <fstream>
  27. #include <stdarg.h>
  28. #include <utility>
  29.  
  30. using namespace std;
  31.  
  32. #define pb push_back
  33. #define mp make_pair
  34. #define ll long long
  35. #define ull unisgned long long
  36. #define ld long double
  37. #define all(a) a.begin(), a.end()
  38. #define SORT(a) sort(all(a))
  39. #define pii pair<int, int>
  40. #define tii triple<int, int, int>
  41. #define e 1e-7
  42. #define PI acos(-1)
  43. #define sz(a) (int)(a.size())
  44. #define inf 1e17
  45. #define vi vector<int>
  46. #define F first
  47. #define S second
  48. #define rng(x) for(int _ = 0; _ < (x); _++)
  49. #define rngi(i, x) for(int i = 0; i < (x); i++)
  50. #define rngsi(s, i, x) for(int i = (s); i <(x); i++)
  51. #define int long long
  52. #define problem "a"
  53.  
  54. template<typename A, typename B, typename C>
  55. struct triple {
  56. A X;
  57. B Y;
  58. C Z;
  59. triple(A a = 0, B b = 0, C c = 0) :X(a), Y(b), Z(c) {}
  60. };
  61. template<typename A, typename B, typename C>
  62. triple<A, B, C> make_triple(A a = 0, B b = 0, C c = 0) {
  63. return triple<A, B, C>(a, b, c);
  64. }
  65. template<typename A, typename B, typename C>
  66. bool operator<(const triple<A, B, C>& a, const triple<A, B, C>& b) {
  67. if (a.X != b.X)
  68. return a.X < b.X;
  69. if (a.Y != b.Y)
  70. return a.Y < b.Y;
  71. return a.Z < b.Z;
  72. }
  73. template<typename T, typename SS>
  74. ostream& operator<<(ostream& ofs, const pair<T, SS>& p) {
  75. ofs << "( " << p.F << " , " << p.S << " )";
  76. return ofs;
  77. }
  78. template<typename T>
  79. void print(T a) {
  80. for (auto i : a)
  81. cout << i << ' ';
  82. cout << '\n';
  83. }
  84. template<typename T>
  85. T max(T a, T b, T c) {
  86. return max(a, max(b, c));
  87. }
  88. template<typename T>
  89. T min(T a, T b, T c) {
  90. return min(a, min(b, c));
  91. }
  92. template<typename T, typename D>
  93. D min(T a) {
  94. return *min_element(all(a));
  95. }
  96. template<typename T, typename D>
  97. D max(T a) {
  98. return *max_element(all(a));
  99. }
  100. struct custom_hash {
  101. static uint64_t splitmix64(uint64_t x) {
  102. x += 0x9e3779b97f4a7c15;
  103. x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
  104. x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
  105. return x ^ (x >> 31);
  106. }
  107.  
  108. size_t operator()(uint64_t x) const {
  109. static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
  110. return splitmix64(x + FIXED_RANDOM);
  111. }
  112. };
  113. struct node {
  114. int ans;
  115. int s;
  116. int le, re;
  117. int sl, pl;
  118. node() { ans = s = le = re = sl = pl = 0; }
  119. };
  120. node mrg(const node& l, const node& r) {
  121. node res;
  122. res.le = l.le; res.re = r.re;
  123. res.ans = max(res.ans, l.ans, r.ans);
  124. res.s = l.s + r.s;
  125. if (l.re < r.le) {
  126. res.ans = max(res.ans, l.sl + r.pl);
  127. if (r.pl == r.s) res.sl = l.sl + r.s;
  128. else res.sl = r.sl;
  129. if (l.pl == l.s) res.pl = r.pl + l.s;
  130. else res.pl = l.pl;
  131. }
  132. else {
  133. res.sl = r.sl;
  134. res.pl = l.pl;
  135. }
  136. return res;
  137. };
  138. vector<node> tree;
  139. void build(int v, int l, int r, vi& c) {
  140. if (l > r)return;
  141. if (l == r) {
  142. tree[v].le = tree[v].re = c[l];
  143. tree[v].ans = tree[v].sl = tree[v].pl = 1;
  144. tree[v].s = 1;
  145. return;
  146. }
  147. int m = l + r >> 1;
  148. build(v << 1, l, m, c); build(v << 1 | 1, m + 1, r, c);
  149. tree[v] = mrg(tree[v << 1], tree[v << 1 | 1]);
  150. }
  151. void ch(int v, int l, int r, int pos, int x) {
  152. if (l > r)return;
  153. if (l == r) {
  154. tree[v].le = tree[v].re = x;
  155. return;
  156. }
  157. int m = l + r >> 1;
  158. if (pos <= m)ch(v << 1, l, m, pos, x);
  159. else ch(v << 1 | 1, m + 1, r, pos, x);
  160. tree[v] = mrg(tree[v << 1], tree[v << 1 | 1]);
  161. }
  162. void solve() {
  163. int n, m; cin >> n >> m;
  164. vi a(n); rngi(i, n) cin >> a[i], a[i]--;
  165. vi p(n); rngi(i, n)p[a[i]] = i;
  166. tree.resize(4 * n);
  167. build(1, 0, n - 1, p);
  168. auto calc = [&]() {
  169. //build(1, 0, n - 1, p);
  170. return n - tree[1].ans;
  171. int z = 1;
  172. int i = 0;
  173. while (i < n) {
  174. int d = 1;
  175. while (i + d < n && p[i + d - 1] < p[i + d])d++;
  176. z = max(z, d);
  177. i = i + d;
  178. }
  179. return n - z;
  180. };
  181. cout << calc() << '\n';
  182. rng(m) {
  183. int x, y; cin >> x >> y;
  184. int u = p[a[x - 1]], v = p[a[y - 1]];
  185. ch(1, 0, n - 1, a[x - 1], v);
  186. ch(1, 0, n - 1, a[y - 1], u);
  187. swap(p[a[x - 1]], p[a[y - 1]]);
  188. swap(a[x - 1], a[y - 1]);
  189. cout << calc() << '\n';
  190. }
  191. };
  192. signed main()
  193. {
  194. if (0) {
  195. freopen(problem".in", "r", stdin);
  196. freopen(problem".out", "w", stdout);
  197. }
  198. srand(time(NULL));
  199. cin.tie(0);
  200. cout.tie(0);
  201. ios_base::sync_with_stdio(false);
  202. int t = 1;// cin >> t;
  203. rng(t) solve();
  204. }
  205.  
Advertisement
Add Comment
Please, Sign In to add comment