Advertisement
ec1117

Untitled

Jan 29th, 2021
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.37 KB | None | 0 0
  1. We will solve the problem for each bit separately, and then multiply the results.
  2.  
  3. Obviously, if the position is covered by a segment with the value 1, then we have no choice, and we must put 1 there. For segments with the value 0, there must be at least one position that they cover and its value is 0.
  4.  
  5. So we can write the following dynamic programming: dpi — the number of arrays such that the last 0 was exactly at the position i, and all 0-segments to the left of it contain at least one zero.
  6.  
  7. It remains to determine which states j we can update from. The only restriction we have is that there should not be any segment (l,r) with the value 0, such that j<l and r<i. Since in this case, this segment will not contain any zero values. For each position i, we may precalculate the rightmost position fi where some segment ending before i begins, and while calculating dpi, we should sum up only the values starting from position fi. This can be done with prefix sums.
  8.  
  9. #include<bits/stdc++.h>
  10. using namespace std;
  11.  
  12. const int MOD = 998244353;
  13.  
  14. int main() {
  15.     ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  16.     int N, K, M; cin >> N >> K >> M;
  17.     vector<vector<pair<int, int>>> constraints(N);
  18.     for (int i = 0; i < M; i++) {
  19.         int l, r, x; cin >> l >> r >> x; l--, r--;
  20.         constraints[r].emplace_back(l, x);
  21.     }
  22.     int64_t totAns = 1;
  23.     vector<pair<int, int>> dp(N+1);
  24.     for (int k = 0; k < K; k++) {
  25.         int lo,hi;
  26.         int totDp = (dp[lo = hi = 0] = {-1,1}).second;
  27.         for (int i = 0; i < N; i++) {
  28.             totDp += (dp[++hi] = {i,totDp}).second;
  29.             if (totDp >= MOD) totDp -= MOD;
  30.             for (auto it : constraints[i]) {
  31.                 if (it.second & (1<<k)) {
  32.                     while (lo <= hi && dp[hi].first >= it.first) {
  33.                         totDp -= dp[hi--].second;
  34.                         if (totDp < 0) totDp += MOD;
  35.                     }
  36.                 } else {
  37.                     while (lo <= hi && dp[lo].first < it.first) {
  38.                         totDp -= dp[lo++].second;
  39.                         if (totDp < 0) totDp += MOD;
  40.                     }
  41.                 }
  42.             }
  43.         }
  44.         totAns *= totDp;
  45.         totAns %= MOD;
  46.     }
  47.     cout << totAns << '\n';
  48.  
  49.     return 0;
  50. }
  51.  
  52.  
  53. int one[MX], zero[MX];
  54. int bit[MX];
  55. ll dp[MX];
  56.  
  57.  
  58.  
  59. int main() {
  60.     ios_base::sync_with_stdio(0); cin.tie(0);    
  61.     cin >> N;
  62.     int K; cin >> K;
  63.     cin >> M;
  64.  
  65.     ll ans = 1;
  66.  
  67.     vector<vi> conds; F0R(i, M) {
  68.         int A, B, C; cin >> A >> B >> C;
  69.         A--; B--; C = (1 << K) - 1 - C;
  70.         conds.pb({A, B, C});
  71.     }
  72.  
  73.  
  74.     F0R(x, K) {
  75.         F0R(i, N) {
  76.             one[i] = -1, zero[i] = -1;
  77.         }
  78.         F0R(i, M) {
  79.             if (conds[i][2] & (1 << x)) {
  80.                 one[conds[i][1]] = max(one[conds[i][1]], conds[i][0]);
  81.  
  82.             } else {
  83.                 zero[conds[i][0]] = max(zero[conds[i][0]], conds[i][1]);
  84.             }
  85.         }
  86.  
  87.         ll sum = 1;
  88.         int p = 0;
  89.         dp[0] = 1;
  90.         int zed = 0;
  91.         FOR(i, 1, N+1) {
  92.             dp[i] = sum;
  93.             zed = max(zed, zero[i-1] + 1);
  94.             if (zed >= i) {
  95.                 dp[i] = 0;
  96.             }
  97.             sum += dp[i]; sum %= MOD;
  98.             while (p <= one[i-1]) {
  99.                 sum += MOD - dp[p];
  100.                 sum %= MOD;
  101.                 p++;
  102.             }
  103.         }
  104.  
  105.         ans *= sum; ans %= MOD;
  106.         F0R(i, N) {
  107.             //cout << x << " " << i << " " << one[i] << " " << zero[i] << endl;
  108.         }
  109.         //cout << sum << endl;
  110.  
  111.  
  112.     }
  113.  
  114.     cout << ans << endl;
  115.  
  116.     return 0;
  117. }
  118.  
  119.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement