Dmaxiya

cf 1051 Div.2 D2. Inversion Graph Coloring (Hard Version)

Sep 20th, 2025 (edited)
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.69 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. typedef long long LL;
  5. const int maxn = 2000 + 100;
  6. const LL MOD = 1000000007;
  7. int T, n, nn;
  8. LL ans;
  9. int a[maxn];
  10. LL mxsum[maxn][maxn], secsum[maxn][maxn];
  11.  
  12. int lowbit(int x) {
  13.     return x & (-x);
  14. }
  15.  
  16. void add(LL *sum, int idx, LL x) {
  17.     while (idx <= nn) {
  18.         sum[idx] = (sum[idx] + x) % MOD;
  19.         idx += lowbit(idx);
  20.     }
  21. }
  22.  
  23. void init() {
  24.     nn = n + 1;
  25.     for (int i = 1; i <= nn; ++i) {
  26.         memset(mxsum[i] + 1, 0, sizeof(LL) * nn);
  27.         memset(secsum[i] + 1, 0, sizeof(LL) * nn);
  28.     }
  29.     add(mxsum[1], 1, 1);
  30.     add(secsum[1], 1, 1);
  31. }
  32.  
  33. LL query(LL *sum, int idx) {
  34.     LL ret = 0;
  35.     while (idx > 0) {
  36.         ret += sum[idx];
  37.         idx -= lowbit(idx);
  38.     }
  39.     return ret % MOD;
  40. }
  41.  
  42. int main(){
  43. #ifdef ExRoc
  44.     freopen("test.txt", "r", stdin);
  45. #endif // ExRoc
  46.     ios::sync_with_stdio(false);
  47.  
  48.     cin >> T;
  49.     while (T--) {
  50.         cin >> n;
  51.         init();
  52.         for (int i = 1; i <= n; ++i) {
  53.             cin >> a[i];
  54.             ++a[i];
  55.         }
  56.         for (int i = 1; i <= n; ++i) {
  57.             for (int sec = 1; sec < a[i]; ++sec) {
  58.                 LL tmp = query(secsum[sec], a[i]);
  59.                 add(mxsum[a[i]], sec, tmp);
  60.                 add(secsum[sec], a[i], tmp);
  61.             }
  62.             for (int mx = a[i] + 1; mx <= nn; ++mx) {
  63.                 LL tmp = query(mxsum[mx], a[i]);
  64.                 add(mxsum[mx], a[i], tmp);
  65.                 add(secsum[a[i]], mx, tmp);
  66.             }
  67.         }
  68.         ans = 0;
  69.         for (int i = 1; i <= nn; ++i) {
  70.             ans += query(mxsum[i], nn);
  71.         }
  72.         cout << (ans % MOD) << endl;
  73.     }
  74.  
  75.     return 0;
  76. }
  77.  
Advertisement
Add Comment
Please, Sign In to add comment