QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#828459#9663. Reverse Pairs ColoringwcyQwQML 7ms14952kbC++141.7kb2024-12-23 16:49:432024-12-23 16:49:57

Judging History

This is the latest submission verdict.

  • [2024-12-23 16:49:57]
  • Judged
  • Verdict: ML
  • Time: 7ms
  • Memory: 14952kb
  • [2024-12-23 16:49:43]
  • Submitted

answer

#include <bits/stdc++.h>
#define all(x) x.begin(), x.end()
#define sz(x) int(x.size())
#define eb emplace_back

using namespace std;
using LL = long long;

struct Fenwick {
  vector<int> tr;
  Fenwick(int n = 0) {
    tr.resize(n + 1);
  }
  void add(int x, int v) {
    for (; x < tr.size(); x += x & -x) tr[x] += v;
  }
  int ask(int x) {
    int ans = 0;
    for (; x; x -= x & -x) ans += tr[x];
    return ans;
  }
  int ask(int l, int r) { return ask(r) - ask(l - 1); }
};

namespace FloatingMoon {
  void main() {
    int n;
    cin >> n;
    vector<int> a(n + 1), b(n + 1, 1);
    for (int i = 1; i <= n; i++) cin >> a[i], a[i] = n - a[i] + 1;
    vector<vector<int>> mp(n + 1, vector<int>(n + 1));
    for (int i = 1; i <= n; i++) {
      for (int j = i + 1; j <= n; j++) {
        if (a[i] < a[j]) mp[i][a[j]] = 1;
      }
    }
    // for (int i = 1; i <= n; i++) {
    //   for (int j = 1; j <= n; j++) {
    //     clog << mp[i][j];
    //   }
    //   clog << '\n';
    // }
    // for (int i = 1; i <= n; i++) clog << a[i] << " \n"[i == n];
    Fenwick bit(n);
    LL ans = a[1] != n;
    if (a[1] < n && b[a[1] + 1] == 1) bit.add(a[1] + 1, 1);
    if (a[1] > 1 && b[a[1] - 1] == 0) bit.add(a[1], -1);
    b[a[1]] = 0;
    for (int i = 2; i <= n; i++) {
      int sum = a[i] > a[i - 1] ? 0 : bit.ask(a[i] + 1, a[i - 1]) + (b[a[i] + 1] == 1 && b[a[i]] == 1);
      if (a[i] < n && b[a[i] + 1] == 1) bit.add(a[i] + 1, 1);
      if (a[i] > 1 && b[a[i] - 1] == 0) bit.add(a[i], -1);
      b[a[i]] = 0;
      ans += sum;
    }
    cout << ans << '\n';
  }
}

int main() {
  // freopen("input.in", "r", stdin);
  cin.tie(0)->sync_with_stdio(0);
  FloatingMoon::main();
  return 0;
}

详细

Test #1:

score: 100
Accepted
time: 1ms
memory: 3804kb

input:

9
5 9 1 8 2 6 4 7 3

output:

5

result:

ok single line: '5'

Test #2:

score: 0
Accepted
time: 0ms
memory: 3748kb

input:

2
1 2

output:

0

result:

ok single line: '0'

Test #3:

score: 0
Accepted
time: 1ms
memory: 3780kb

input:

2
2 1

output:

1

result:

ok single line: '1'

Test #4:

score: 0
Accepted
time: 0ms
memory: 3520kb

input:

12
12 11 10 9 8 7 6 5 4 3 2 1

output:

1

result:

ok single line: '1'

Test #5:

score: 0
Accepted
time: 0ms
memory: 3636kb

input:

12
2 5 3 7 1 12 11 9 8 4 10 6

output:

8

result:

ok single line: '8'

Test #6:

score: 0
Accepted
time: 0ms
memory: 3812kb

input:

12
1 2 3 4 5 6 7 8 9 10 11 12

output:

0

result:

ok single line: '0'

Test #7:

score: 0
Accepted
time: 0ms
memory: 3692kb

input:

144
144 143 142 141 140 139 138 137 136 135 134 133 132 131 130 129 128 127 126 125 124 123 122 121 120 119 118 117 116 115 114 113 112 111 110 109 108 107 106 105 104 103 102 101 100 99 98 97 96 95 94 93 92 91 90 89 88 87 86 85 84 83 82 81 80 79 78 77 76 75 74 73 72 71 70 69 68 67 66 65 64 63 62 61...

output:

1

result:

ok single line: '1'

Test #8:

score: 0
Accepted
time: 1ms
memory: 3832kb

input:

144
78 106 141 21 115 2 14 64 124 91 117 121 30 99 144 55 94 137 38 27 40 72 59 46 50 109 84 15 87 108 52 23 74 4 140 70 63 58 105 113 77 10 35 66 130 34 96 86 16 65 12 92 83 5 95 36 45 8 79 132 75 57 98 93 119 125 26 18 136 127 73 90 3 123 53 139 142 37 103 24 133 111 143 67 76 43 114 42 44 135 22 ...

output:

699

result:

ok single line: '699'

Test #9:

score: 0
Accepted
time: 0ms
memory: 3700kb

input:

144
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 ...

output:

0

result:

ok single line: '0'

Test #10:

score: 0
Accepted
time: 3ms
memory: 14920kb

input:

1728
1728 1727 1726 1725 1724 1723 1722 1721 1720 1719 1718 1717 1716 1715 1714 1713 1712 1711 1710 1709 1708 1707 1706 1705 1704 1703 1702 1701 1700 1699 1698 1697 1696 1695 1694 1693 1692 1691 1690 1689 1688 1687 1686 1685 1684 1683 1682 1681 1680 1679 1678 1677 1676 1675 1674 1673 1672 1671 1670 ...

output:

1

result:

ok single line: '1'

Test #11:

score: 0
Accepted
time: 7ms
memory: 14900kb

input:

1728
802 233 421 541 844 722 70 119 697 499 1699 1323 1537 1053 551 1125 736 686 1378 630 1070 192 1650 652 291 1182 1405 848 684 1557 100 1704 329 504 611 795 1242 1548 680 1355 1693 66 479 1194 607 316 913 1343 276 701 78 737 279 804 1438 30 754 1115 745 1017 767 1046 920 683 988 1494 501 669 1658...

output:

83738

result:

ok single line: '83738'

Test #12:

score: 0
Accepted
time: 3ms
memory: 14952kb

input:

1728
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101...

output:

0

result:

ok single line: '0'

Test #13:

score: -100
Memory Limit Exceeded

input:

20736
20736 20735 20734 20733 20732 20731 20730 20729 20728 20727 20726 20725 20724 20723 20722 20721 20720 20719 20718 20717 20716 20715 20714 20713 20712 20711 20710 20709 20708 20707 20706 20705 20704 20703 20702 20701 20700 20699 20698 20697 20696 20695 20694 20693 20692 20691 20690 20689 20688 ...

output:

1

result: