Advertisement
Dang_Quan_10_Tin

Growing Up is Hard to Do

Dec 4th, 2020
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.70 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <algorithm>
  4. #include <vector>
  5. #include <map>
  6. #define task "Growing Up is Hard to Do"
  7. #define bit(i, x) (((x) >> (i)) & 1)
  8.  
  9. using namespace std;
  10. using ll = long long;
  11.  
  12. const int N = 3e5 + 2;
  13. const int mod = 1e9 + 7;
  14. struct Trie
  15. {
  16.     Trie *child[2];
  17.     ll v;
  18.     Trie()
  19.     {
  20.         child[0] = child[1] = NULL;
  21.         v = 0;
  22.     }
  23. };
  24.  
  25. void Add(Trie *a, int v, ll amount)
  26. {
  27.     int id = 18;
  28.     while (1)
  29.     {
  30.         if (id == -1)
  31.         {
  32.             (a->v += amount) %= mod;
  33.             return;
  34.         }
  35.         if (!(a->child[bit(id, v)]))
  36.             a->child[bit(id, v)] = new Trie;
  37.         (a->v += amount) %= mod;
  38.         a = a->child[bit(id, v)];
  39.         --id;
  40.     }
  41. }
  42.  
  43. ll Get(Trie *a, int x)
  44. {
  45.     int id = 18;
  46.     ll ans(0);
  47.     while (1)
  48.     {
  49.         if (id == -1)
  50.         {
  51.             (ans += a->v) %= mod;
  52.             break;
  53.         }
  54.         if (bit(id, x))
  55.         {
  56.             if (a->child[0])
  57.                 (ans += a->child[0]->v) %= mod;
  58.             if (a->child[1])
  59.                 a = a->child[1];
  60.             else
  61.                 break;
  62.         }
  63.         else
  64.         {
  65.             if (a->child[0])
  66.                 a = a->child[0];
  67.             else
  68.                 break;
  69.         }
  70.         --id;
  71.     }
  72.     return ans;
  73. }
  74.  
  75. int n, id[N], m;
  76. int a[N];
  77. map<int, int> ck[N];
  78. vector<int> endof(N);
  79. vector<Trie> d(N);
  80. ll dp[N];
  81.  
  82. void Read()
  83. {
  84.     cin >> n;
  85.     for (int i = 1; i <= n; ++i)
  86.     {
  87.         cin >> a[i];
  88.         id[i] = i;
  89.     }
  90. }
  91.  
  92. void Compress()
  93. {
  94.     sort(id + 1, id + n + 1, [&](const int &x, const int &y) {
  95.         return a[x] < a[y];
  96.     });
  97.     int l(0);
  98.     for (int i = 1; i <= n; ++i)
  99.     {
  100.         ++l;
  101.         int j = i;
  102.         while (j <= n && a[id[i]] == a[id[j]])
  103.             ++j;
  104.         while (i < j)
  105.             a[id[i++]] = l;
  106.         --i;
  107.     }
  108. }
  109.  
  110. void Solve()
  111. {
  112.     fill(endof.begin(), endof.end(), mod);
  113.     endof[0] = 0;
  114.     Add(&d[0], 0, 1);
  115.     int ans = 0;
  116.     for (int i = 1; i <= n; ++i)
  117.     {
  118.         int j = lower_bound(endof.begin(), endof.end(), a[i]) - endof.begin();
  119.         if (ck[a[i]][j])
  120.             continue;
  121.         ck[a[i]][j] = 1;
  122.         ll now = Get(&d[j - 1], a[i] - 1);
  123.         dp[j] = (dp[j] + now) % mod;
  124.         Add(&d[j], a[i], now);
  125.         endof[j] = min(endof[j], a[i]);
  126.         ans = max(ans, j);
  127.     }
  128.     cout << dp[ans];
  129. }
  130.  
  131. int32_t main()
  132. {
  133.     if (fopen(task ".INP", "r"))
  134.     {
  135.         freopen(task ".INP", "r", stdin);
  136.         freopen(task ".OUT", "w", stdout);
  137.     }
  138.     ios_base::sync_with_stdio(0);
  139.     cin.tie(0);
  140.     cout.tie(0);
  141.     Read();
  142.     Compress();
  143.     Solve();
  144. }
  145.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement