Advertisement
yeskendir_sultanov

All Possible Increasing Subsequences

May 5th, 2025
304
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.29 KB | Source Code | 0 0
  1. #include <bits/stdc++.h>
  2. #define ll long long
  3.  
  4. using namespace std;
  5.  
  6. const ll mod = 1e9 + 7;
  7. const int N = 1e5 + 3;
  8.  
  9. ll tree[4 * N];
  10. int a[N], n;
  11.  
  12. void init(int n) {
  13.     for (int i = 0; i <= 4 * n; ++i) {
  14.         tree[i] = 0;
  15.     }
  16. }
  17.  
  18. void update(int v, int tl, int tr, int pos, ll x) {
  19.     if (tl == tr) {
  20.         tree[v] = (tree[v] + x) % mod;
  21.     } else {
  22.         int tm = (tl + tr) / 2;
  23.         if (pos <= tm) {
  24.             update(v * 2, tl, tm, pos, x);
  25.         } else {
  26.             update(v * 2 + 1, tm + 1, tr, pos, x);
  27.         }
  28.         tree[v] = (tree[v * 2] + tree[v * 2 + 1]) % mod;
  29.     }
  30. }
  31.  
  32. ll sum(int v, int tl, int tr, int l, int r) {
  33.     if (l > r) {
  34.         return 0;
  35.     }
  36.     if (tl == l && r == tr) {
  37.         return tree[v];
  38.     }
  39.     int tm = (tl + tr) / 2;
  40.     return (sum(v * 2, tl, tm, l, min(tm, r)) + sum(v * 2 + 1, tm + 1, tr, max(tm + 1, l), r)) % mod;
  41. }
  42.  
  43. void solve(int tc) {
  44.     cout << "Case " << tc << ": ";
  45.     cin >> n;
  46.     pair<int, int> b[n];
  47.     for (int i = 0; i < n; ++i) {
  48.         cin >> a[i];
  49.         b[i] = {a[i], i};
  50.     }
  51.  
  52.     sort(b + 0, b + n);
  53.     int k = 1;
  54.     a[b[0].second] = k;
  55.     for (int i = 1; i < n; ++i) {
  56.         if (b[i - 1].first != b[i].first) {
  57.             k++;
  58.         }
  59.         a[b[i].second] = k;
  60.     }
  61.    
  62.     ll ans = 0;
  63.    
  64.     init(k);
  65.    
  66.     for (int i = 0; i < n; ++i) {
  67.         ll cur = sum(1, 1, k, 1, a[i] - 1) + 1;
  68.         ans = (ans + cur) % mod;
  69.         update(1, 1, k, a[i], cur);
  70.     }
  71.    
  72.     cout << ans << endl;
  73. }
  74.  
  75. int main() {
  76.     ios_base::sync_with_stdio(0);
  77.     cin.tie(0); cout.tie(0);
  78.     int test_cnt;
  79.     cin >> test_cnt;
  80.     for (int tc = 1; tc <= test_cnt; ++tc) {
  81.         solve(tc);
  82.     }
  83.     return 0;
  84. }
  85.  
  86. /*
  87. 1    2       3       3       4
  88.    
  89.          23
  90.     3        20
  91. 1     2    8     12    
  92. 1     2    3     4      
  93.  
  94. {1}
  95. {1, 2}, {2}
  96. {1, 3}, {1, 2, 3}, {2, 3}, {3}
  97. {1, 3}, {1, 2, 3}, {2, 3}, {3}
  98.  
  99. {1,4}
  100. {1, 2, 4}, {2, 4}
  101. {1, 3, 4}, {1, 2, 3, 4}, {2, 3, 4}, {3, 4}
  102. {1, 3, 4}, {1, 2, 3, 4}, {2, 3, 4}, {3, 4}. {4}
  103.  
  104. ans += get(1, 1-1)=> +get(1,0)=0   +  1 = 1
  105. ans += get(1, 2-1)=> +get(1,1)=1   +  1 = 2
  106. ans += get(1, 3-1)=> +get(1,2)=3   +  1 = 4
  107. ans += get(1, 3-1)=> +get(1,2)=3   +  1 = 4
  108. ans += get(1, 4-1)=> +get(1,3)=11  +  1 = 12
  109. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement