prog3r

hld/parts worst case

Jun 28th, 2025
1,078
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.92 KB | None | 0 0
  1. //#include "testlib.h"
  2. #include <bits/extc++.h>
  3. using namespace std;
  4. using namespace __gnu_pbds;
  5. template <typename T> using ordered_set =  tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
  6. #define int long long
  7. #define YES(x) cout << (x?"YES\n":"NO\n")
  8. #define NO(x) cout << (x?"NO\n":"YES\n")
  9. #ifdef LO
  10. #pragma GCC optimize("trapv")
  11. #endif
  12. #ifndef LO
  13. #pragma GCC optimize("Ofast,unroll-loops")
  14. #endif
  15. //constexpr int MOD = (119<<23)+1;
  16. //constexpr int MOD = 967276608647009887ll;
  17. //constexpr int MOD = 1e9+7;
  18. constexpr int INF = 1e18;
  19. signed main() {
  20.     ofstream cout("hh.txt");
  21. #ifndef LO
  22.     clog << "FIO\n";
  23.     ios::sync_with_stdio(0);
  24.     cin.tie(0);
  25. #endif
  26. #ifdef LO
  27.     cout << unitbuf;
  28. #endif
  29.     auto fenw_ops = [&] (const int x) -> int {
  30.         if (x <= 1) {
  31.             assert(false);
  32.         }
  33.         return __lg(x-1)+2;
  34.     };
  35.     int N = 5e5;
  36.     vector<int> dp(N+1);
  37.     vector<pair<int,int>> ch(N+1);
  38.     vector<int> palka(N+1);
  39.     dp[1] = 1;
  40.     for (int i = 2; i <= N; i += 1) {
  41.         for (int use = 1; use <= i; use += 1) {
  42.             if ((use&(use-1)) && fenw_ops(use+1) < (1<<__lg(use))) {
  43.                 continue;
  44.             }
  45.             for (int one = 0; i-use-one >= one; one += 1) {
  46.                 int another = i-use-one;
  47.                 if (!one) {
  48.                     int x = dp[another];
  49.                     if (dp[i] < x) {
  50.                         ch[i] = {one, another};
  51.                         palka[i] = use;
  52.                     }
  53.                     dp[i] = max(dp[i], x);
  54.                 } else {
  55.                     int x = min({max(dp[one]+1, dp[another]+1),
  56.                                  max(dp[one]+min(use, fenw_ops(use+1)), dp[another]+0),
  57.                                  max(dp[one]+0, dp[another]+min(use, fenw_ops(use+1)))});
  58.                     if (dp[i] < x) {
  59.                         ch[i] = {one, another};
  60.                         palka[i] = use;
  61.                     }
  62.                     dp[i] = max(dp[i], x);
  63.                 }
  64.             }
  65.         }
  66. //        cout << "(" << i << ", " << dp[i] << ")\n";
  67.         cout << "(" << i << ", " << dp[i] << " " << ch[i].first << "|" << dp[ch[i].first] << " " << ch[i].second << "|" << dp[ch[i].second] << "), " << palka[i] << "\n";
  68.     }
  69.     int curr = 1;
  70.     auto print = [&] (auto f, const int x) -> int {
  71.         if (!x) {
  72.             return -1;
  73.         }
  74.         int U = curr++;
  75.         int u = U;
  76.         for (int i = 0; i < palka[x]-1; i += 1) {
  77.             int v = curr++;
  78.             cout << u << " " << v << "\n";
  79.             u = v;
  80.         }
  81.         int v1 = f(f, ch[x].first), v2 = f(f, ch[x].second);
  82.         if (v1 != -1) {
  83.             cout << u << " " << v1 << "\n";
  84.         }
  85.         if (v2 != -1) {
  86.             cout << u << " " << v2 << "\n";
  87.         }
  88.         return U;
  89.     };
  90.     int n;
  91.     while (cin >> n) {
  92.         print(print, n);
  93.     }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment