Dang_Quan_10_Tin

SALES

Aug 27th, 2020
308
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.00 KB | None | 0 0
  1. #pragma GCC target("avx2")
  2. #pragma GCC optimization("O3")
  3. #pragma GCC optimization("unroll-loops")
  4.  
  5. #include <iostream>
  6. #include <cstdio>
  7. #include <algorithm>
  8. #include <queue>
  9. #include <cmath>
  10. #define task "SALES"
  11. using namespace std;
  12. using ll = long long;
  13. using ld = long double;
  14.  
  15. const int N = 5e5 + 2;
  16. int n;
  17. ll u, d, s, sum[N];
  18. ll dp[N], f[N];
  19. int maxn(0);
  20. const ll Inf = 1e16;
  21. struct fes
  22. {
  23.     ll t, m, l;
  24.     bool operator<(const fes &a) const
  25.     {
  26.         return t < a.t || (t == a.t && l < a.l);
  27.     }
  28. } a[N];
  29.  
  30. #define BIT(i, x) (((x) >> (i)) & 1)
  31.  
  32. struct Trie
  33. {
  34.     Trie *child[2];
  35.     ll v;
  36.     Trie()
  37.     {
  38.         child[0] = child[1] = NULL;
  39.         v = -Inf;
  40.     }
  41. } lower, upper;
  42.  
  43. void Add(Trie *a, int x, ll v)
  44. {
  45.     int id = maxn;
  46.     while (1)
  47.     {
  48.         if (id == -1)
  49.         {
  50.             a->v = max(a->v, v);
  51.             return;
  52.         }
  53.         if (!(a->child[BIT(id, x)]))
  54.             a->child[BIT(id, x)] = new Trie;
  55.         a->v = max(a->v, v);
  56.         a = a->child[BIT(id, x)];
  57.         --id;
  58.     }
  59. }
  60.  
  61. ll getup(Trie *a, int x)
  62. {
  63.     int id = maxn;
  64.     ll ans = -Inf;
  65.     while (1)
  66.     {
  67.         if (id == -1)
  68.         {
  69.             ans = max(ans, a->v);
  70.             break;
  71.         }
  72.         if (BIT(id, x))
  73.         {
  74.             if (a->child[1])
  75.                 a = a->child[1];
  76.             else
  77.                 break;
  78.         }
  79.         else
  80.         {
  81.             if (a->child[1])
  82.                 ans = max(ans, a->child[1]->v);
  83.             if (a->child[0])
  84.                 a = a->child[0];
  85.             else
  86.                 break;
  87.         }
  88.         --id;
  89.     }
  90.     return ans;
  91. }
  92.  
  93. ll getlow(Trie *a, int x)
  94. {
  95.     int id = maxn;
  96.     ll ans = -Inf;
  97.     while (1)
  98.     {
  99.         if (id == -1)
  100.         {
  101.             ans = max(ans, a->v);
  102.             break;
  103.         }
  104.         if (BIT(id, x))
  105.         {
  106.             if (a->child[0])
  107.                 ans = max(ans, a->child[0]->v);
  108.             if (a->child[1])
  109.                 a = a->child[1];
  110.             else
  111.                 break;
  112.         }
  113.         else
  114.         {
  115.             if (a->child[0])
  116.                 a = a->child[0];
  117.             else
  118.                 break;
  119.         }
  120.         --id;
  121.     }
  122.     return ans;
  123. }
  124.  
  125. void Read()
  126. {
  127.     cin >> n >> u >> d >> s;
  128.     for (int i = 1; i <= n; ++i)
  129.         cin >> a[i].t >> a[i].l >> a[i].m;
  130.     sort(a + 1, a + n + 1);
  131.     maxn = max(maxn, (int)log2(s));
  132.     for (int i = 1; i <= n; ++i)
  133.         sum[i] = sum[i - 1] + a[i].m,
  134.         maxn = max(maxn, (int)log2(a[i].l));
  135. }
  136.  
  137. void Solve()
  138. {
  139.     fill_n(dp, N, -Inf);
  140.     /// BASE CASE
  141.     Add(&lower, s, s * d);
  142.     Add(&upper, s, -u * s);
  143.     /// END BASE CASE
  144.     int j;
  145.     for (int i = 1; i <= n; ++i)
  146.     {
  147.         j = i;
  148.         while (j <= n && a[i].t == a[j].t)
  149.             ++j;
  150.         ll ilr(-Inf), irl(-Inf), pilr(-Inf), pirl(-Inf);
  151.         for (int t = i; t < j; ++t)
  152.         {
  153.             /// Calculate f[]
  154.             f[t] = max(getlow(&lower, a[t].l) - d * a[t].l,
  155.                        getup(&upper, a[t].l) + u * a[t].l);
  156.             /// Calculate dp[]
  157.             pilr = max(pilr, -(u + d) * a[t].l - sum[t - 1]);
  158.             ilr = max(ilr, u * a[t].l + f[t] + pilr);
  159.             dp[t] = d * a[t].l + ilr + sum[t];
  160.         }
  161.         for (int t = j - 1; t >= i; --t)
  162.         {
  163.             pirl = max(pirl, sum[t] + (u + d) * a[t].l);
  164.             irl = max(irl, pirl + f[t] - d * a[t].l);
  165.             dp[t] = max(dp[t], irl - u * a[t].l - sum[t - 1]);
  166.             /// Update lower, upper
  167.             Add(&lower, a[t].l, dp[t] + d * a[t].l);
  168.             Add(&upper, a[t].l, dp[t] - u * a[t].l);
  169.         }
  170.         i = j - 1;
  171.     }
  172.     cout << max(getlow(&lower, s) - d * s, getup(&upper, s) + u * s);
  173. }
  174.  
  175. int32_t main()
  176. {
  177.     ios::sync_with_stdio(0);
  178.     cin.tie(0);
  179.     cout.tie(0);
  180.     if (fopen(task ".INP", "r"))
  181.         freopen(task ".INP", "r", stdin),
  182.             freopen(task ".OUT", "w", stdout);
  183.     Read();
  184.     Solve();
  185. }
Advertisement
Add Comment
Please, Sign In to add comment