Advertisement
skimono

Untitled

Aug 13th, 2023
1,264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.61 KB | None | 0 0
  1. #pragma GCC optimize("Ofast") // ������������ ���������, �� ��� ������
  2. #pragma GCC optimize("no-stack-protector") //�����
  3. #pragma GCC optimize("unroll-loops") // � ���� ���� ��� �� 100 �� ������ ����� � ��������� �� ��� � 100 ��� �� ������ ��������
  4. #pragma GCC target("sse,sse2,sse3,ssse3,popcnt,abm,mmx,tune=native") // ��� ����� ����� �� ��� 03 02 ��������� � ������ ������� �������� ��� ���� ������������
  5. #pragma GCC optimize("fast-math")
  6. #define _CRT_SECURE_NO_WARNINGS
  7.  
  8. #include <iostream>
  9. #include <vector>
  10. #include <string>
  11. #include <algorithm>
  12. #include <cmath>
  13. #include <stack>
  14. #include <iomanip>
  15. #include <fstream>
  16. #include <string>
  17. #include <set>
  18. #include <deque>
  19. #include <queue>
  20. #include <map>
  21. #include <bitset>
  22. #include <random>
  23. #include <list>
  24. #include <unordered_map>
  25. #include <unordered_set>
  26. #include <cassert>
  27.  
  28. using namespace std;
  29.  
  30. typedef long long ll;
  31. typedef short sh;
  32. typedef unsigned long long ull;
  33. typedef long double ld;
  34. typedef string str;
  35. //typedef __int128 ultraint;
  36. #define sqrt sqrtl
  37. #define F first
  38. #define S second
  39. #define endl '\n'
  40. #define all(vc666) vc666.begin(), vc666.end()
  41. #define allr(vc666) vc666.rbegin(), vc666.rend()
  42. #define int long long
  43. #define degug(x) cerr (#x) << " " << (x) << endl;
  44.  
  45. const ll INF = 3e12;
  46. const ll inf = 1e12 + 7;
  47. const ll ONE = 1;
  48. const ll mod = 1e9 + 7;
  49. const ll m1 = 1e9 + 575179;
  50. const ll m2 = 1e9 + 87;
  51. const ll LG = 19;
  52. const ll k = 347;
  53. ld EPS = 1e-7;
  54. ld PI = 3.1415926535897932384;
  55. ld phi = (sqrt(5) + 1.0) / 2.0;
  56. mt19937_64 gen(rand());
  57.  
  58. struct Node {
  59.     int mn = INF, push = 0, id = -1;
  60. };
  61. struct SegTree {
  62.     vector <Node> t;
  63.     int n;
  64.     SegTree(int _n) {
  65.         n = 4 * _n;
  66.         t.resize(n);
  67.     }
  68.     void unite(const Node& l, const Node& r, Node& m) {
  69.         if (l.mn < r.mn) {
  70.             m.mn = l.mn;
  71.             m.id = l.id;
  72.         }
  73.         else {
  74.             m.mn = r.mn;
  75.             m.id = r.id;
  76.         }
  77.         m.push = 0;
  78.     }
  79.     void pushUP(int v) {
  80.         t[v].mn += t[v].push;
  81.         if (2 * v + 1 < n) {
  82.             t[2 * v].push += t[v].push;
  83.             t[2 * v + 1].push += t[v].push;
  84.         }
  85.         t[v].push = 0;
  86.     }
  87.     Node query(int v, int tl, int tr, int l, int r) {
  88.         pushUP(v);
  89.         if (tr < l || tl > r) {
  90.             return { INF, 0, -1 };
  91.         }
  92.         else if (l <= tl && r >= tr) {
  93.             return t[v];
  94.         }
  95.         else {
  96.             int m = (tl + tr) / 2;
  97.             Node L = query(2 * v, tl, m, l, r), R = query(2 * v + 1, m + 1, tr, l, r), M;
  98.             unite(L, R, M);
  99.             unite(t[v * 2], t[v * 2 + 1], t[v]);
  100.             return M;
  101.         }
  102.     }
  103.     void update(int v, int tl, int tr, int l, int r, int boost) {
  104.         pushUP(v);
  105.         if (tl > r || tr < l) {
  106.             return;
  107.         }
  108.         else {
  109.             if (tl >= l && tr <= r) {
  110.                 t[v].push += boost;
  111.                 pushUP(v);
  112.             }
  113.             else {
  114.                 int tm = (tl + tr) / 2;
  115.                 update(v * 2, tl, tm, l, r, boost);
  116.                 update(v * 2 + 1, tm + 1, tr, l, r, boost);
  117.                 unite(t[v * 2], t[v * 2 + 1], t[v]);
  118.             }
  119.         }
  120.     }
  121.     void build(int v, int tl, int tr, vector <int>& w) {
  122.         if (tl == tr) {
  123.             t[v].mn = w[tl];
  124.             t[v].id = tl;
  125.         }
  126.         else {
  127.             int tm = (tl + tr) / 2;
  128.             build(2 * v, tl, tm, w);
  129.             build(2 * v + 1, tm + 1, tr, w);
  130.             unite(t[v * 2], t[v * 2 + 1], t[v]);
  131.         }
  132.     }
  133. };
  134.  
  135. void solve() {
  136.     int n, q, i, j, s, t;
  137.     cin >> n >> q >> s >> t;
  138.     s--, t--;
  139.     vector <int> p(q);
  140.     vector <int> w(n);
  141.     for (i = 0; i < q; i++) {
  142.         cin >> p[i];
  143.         p[i]--;
  144.     }
  145.     for (i = 0; i < n; i++) {
  146.         w[i] = inf + i;
  147.     }
  148.     SegTree t1(n), t2(n);
  149.     t1.build(1, 0, n - 1, w);
  150.     reverse(all(w));
  151.     t2.build(1, 0, n - 1, w);
  152.     t1.update(1, 0, n - 1, s, s, abs(p[0] - t) - inf);
  153.     if (s != t) {
  154.         t1.update(1, 0, n - 1, t, t, abs(p[0] - s) - inf);
  155.     }
  156.     t2.update(1, 0, n - 1, s, s, abs(p[0] - t) - inf);
  157.     if (s != t) {
  158.         t2.update(1, 0, n - 1, t, t, abs(p[0] - s) - inf);
  159.     }
  160.     Node res1, res2, el1, el2;
  161.     int u1, u2, ans = INF;
  162.     for (i = 0; i < q - 1; i++) {
  163.         if (p[i + 1] == p[i]) continue;
  164.         res1 = t2.query(1, 0, n - 1, 0, p[i + 1]);
  165.         res2 = t1.query(1, 0, n - 1, p[i + 1], n - 1);
  166.         el1 = t1.query(1, 0, n - 1, p[i], p[i]);
  167.         el2 = t2.query(1, 0, n - 1, p[i], p[i]);
  168.         u1 = res1.mn + (p[i + 1] - res1.id) - (n - res1.id - 1);
  169.         u2 = res2.mn + (res2.id - p[i + 1]) - res2.id;
  170.         t1.update(1, 0, n - 1, p[i], p[i], min(u1, u2) - el1.mn + p[i]);
  171.         t2.update(1, 0, n - 1, p[i], p[i], min(u1, u2) - el2.mn + (n - p[i] - 1));
  172.         t1.update(1, 0, n - 1, 0, p[i] - 1, abs(p[i + 1] - p[i]));
  173.         t1.update(1, 0, n - 1, p[i] + 1, n - 1, abs(p[i + 1] - p[i]));
  174.         t2.update(1, 0, n - 1, 0, p[i] - 1, abs(p[i + 1] - p[i]));
  175.         t2.update(1, 0, n - 1, p[i] + 1, n - 1, abs(p[i + 1] - p[i]));
  176.         if (i == 1) {
  177.             /*for (j = 0; j < n; j++) {
  178.                 el1 = t1.query(1, 0, n - 1, j, j);
  179.                 cout << el1.mn << " " << el1.id << endl;
  180.             }
  181.             for (j = 0; j < n; j++) {
  182.                 el2 = t2.query(1, 0, n - 1, j, j);
  183.                 cout << el2.mn << " " << el2.id << endl;
  184.             }
  185.             for (j = 0; j < n; j++) {
  186.                 el1 = t1.query(1, 0, n - 1, j, j);
  187.                 el2 = t2.query(1, 0, n - 1, j, j);
  188.                 assert(el1.mn - el1.id == el2.mn - (n - el2.id - 1));
  189.                 cout << el1.mn - el1.id << " " << el2.mn - (n - el2.id - 1) << endl;
  190.             }
  191.             */
  192.         }
  193.     }
  194.     for (i = 0; i < n; i++) {
  195.         el1 = t1.query(1, 0, n - 1, i, i);
  196.         el2 = t2.query(1, 0, n - 1, i, i);
  197.         ans = min(ans, el1.mn - el1.id);
  198.         ans = min(ans, el2.mn - (n - el2.id - 1));
  199.     }
  200.     cout << ans << endl;
  201. }
  202.  
  203. signed main() {
  204. #ifdef _DEBUG
  205.     freopen("input.txt", "r ", stdin);
  206.     freopen("output.txt", "w", stdout);
  207. #endif
  208.     ios_base::sync_with_stdio(0);
  209.     cin.tie(NULL);
  210.     cout.tie(NULL);
  211.     int t = 1;
  212.     //cin >> t;
  213.     while (t--) solve();
  214. }
  215. //Deisgned by skimono
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement