Dang_Quan_10_Tin

INCSEQ

Aug 7th, 2020
285
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.29 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <vector>
  4. #include <algorithm>
  5. #define task "INCSEQ"
  6. using namespace std;
  7. using ll = long long;
  8. using ld = long double;
  9.  
  10. template<class T>
  11. void read(T &x){
  12.     register int c;
  13.     x = 0;
  14.     while((c = getchar()) && c < '0' || c > '9');
  15.     for(; c >= '0' && c <= '9'; c = getchar())
  16.         x = x * 10 + c - '0';
  17. }
  18.  
  19. const int N = 1e4 + 2;
  20. int n, k, a[N];
  21. const int mod = 5e6;
  22. int dp[50 + 2][N];
  23.  
  24. void add(int &x, int y){
  25.     x += y;
  26.     x %= mod;
  27. }
  28.  
  29. struct Trie{
  30.     Trie *child[2];
  31.     Trie() {child[0] = child[1] = NULL;}
  32.     int v = 0;
  33.     void Add(int id, int lim, int x, int val){
  34.         if(id == lim){
  35.             add(v, val);
  36.             return;
  37.         }
  38.         if(!child[(x >> id) & 1])
  39.             child[(x >> id) & 1] = new Trie;
  40.         child[(x >> id) & 1] -> Add(id - 1, lim, x, val);
  41.         add(v, val);
  42.     }
  43.     int count(int id, int lim, int x){
  44.         if(id == lim)
  45.             return v;
  46.         int ans = 0;
  47.         if((x >> id) & 1){
  48.             if(child[!((x >> id) & 1)])
  49.                 add(ans, child[!((x >> id) & 1)] -> v);
  50.             if(child[(x >> id) & 1])
  51.                 add(ans, child[(x >> id) & 1] -> count(id - 1, lim, x));
  52.             return ans;
  53.         }
  54.         else{
  55.             if(child[(x >> id) & 1])
  56.                 add(ans, child[(x >> id) & 1] -> count(id - 1, lim, x));
  57.             return ans;
  58.         }
  59.     }
  60. };
  61. vector<Trie> s(N);
  62. vector<int> b(N);
  63. const int Inf = 1e9 + 7;
  64. int id[N];
  65.  
  66. void Read(){
  67.     //cin >> n >> k;
  68.     read(n); read(k);
  69.     for(int i = 1; i <= n; ++i)
  70.         //cin >> a[i];
  71.         read(a[i]),
  72.         id[i] = i;
  73. }
  74.  
  75. void Compress(){
  76.     sort(id + 1, id + n + 1, [&](const int &x, const int &y){return a[x] < a[y];});
  77.     int l = 0;
  78.     for(int i = 1; i <= n; ++i){
  79.         int j = i;
  80.         int cur = a[id[i]];
  81.         ++l;
  82.         while(j <= n && a[id[j]] == cur)
  83.             a[id[j++]] = l;
  84.         i = j - 1;
  85.     }
  86. }
  87.  
  88. void Solve(){
  89.     Compress();
  90.     fill(b.begin(), b.end(), Inf);
  91.     int ans = 0;
  92.     b[0] = 0;
  93.     s[0].Add(13, -1, 0, 1);
  94.     for(int i = 1; i <= n; ++i){
  95.         int j = lower_bound(b.begin(), b.end(), a[i]) - b.begin();
  96.         b[j] = min(b[j], a[i]);
  97.         if(j >= k)
  98.             add(ans, s[k - 1].count(13, -1, a[i] - 1));
  99.         for(int t = 1; t <= min(j, k); ++t)
  100.             s[t].Add(13, -1, a[i], s[t - 1].count(13, -1, a[i] - 1));
  101.     }
  102.     cout << ans;
  103. }
  104.  
  105. int32_t main(){
  106.     ios::sync_with_stdio(0);
  107.     cin.tie(0);
  108.     cout.tie(0); if(fopen(task".INP", "r"))
  109.     freopen(task".INP", "r", stdin),
  110.     freopen(task".OUT", "w", stdout);
  111.     Read();
  112.     Solve();
  113. }
  114.  
Advertisement
Add Comment
Please, Sign In to add comment