QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#168471#6434. Paimon SortingzlxFTHAC ✓179ms14056kbC++141.5kb2023-09-08 15:29:582023-09-08 15:29:58

Judging History

你现在查看的是最新测评结果

  • [2023-09-08 15:29:58]
  • 评测
  • 测评结果:AC
  • 用时:179ms
  • 内存:14056kb
  • [2023-09-08 15:29:58]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;

struct BIT {
  int n;
  vector<int> a;
  BIT(int n = 0) : n(n), a(n + 1, 0) {}
  void mdf(int x, int v) {
    for (int i = x + 1; i <= n; i += i & -i) {
      a[i] += v;
    }
  }
  int qry(int x) {
    if (x < 0 || x >= n) return 0;
    int res = 0;
    for (int i = x + 1; i; i -= i & -i) {
      res += a[i];
    }
    return res; 
  }
  int sum(int l, int r) {
    int res = qry(r);
    res -= qry(l - 1);
    return res;
  }
};

using i64 = long long;

void solve() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    a[i]--;
  }
  i64 ans = 0;
  cout << 0 << " \n"[0 == n - 1];
  BIT t(n);
  vector<int> vis(n);
  auto cg = [&](int x) {
    if (!vis[x]) {
      t.mdf(x, 1);
    }
    vis[x]++;
  };
  cg(a[0]);
  vector<set<int>> b(n);
  b[a[0]].insert(0);
  for (int i = 1; i < n; i++) {
    int v = a[i];
    if (a[0] >= a[i]) {
      ans += t.sum(a[i] + 1, n - 1);
      b[a[i]].insert(i);
    } else {
      ans++;
      ans += b[a[0]].size();
      if (b[a[0]].size() > 1) {
        int u = *next(b[a[0]].begin());
        ans += (i - u - 1) - (b[a[0]].size() - 2);
      }
      b[a[0]].erase(0);
      b[a[0]].insert(i);
      b[a[i]].insert(0);
      swap(a[0], a[i]);
    }
    cg(v);
    cout << ans << " \n"[i == n - 1];
  }
}

int main() {
  cin.tie(0)->sync_with_stdio(0);
  int t;
  cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

3
5
2 3 2 1 5
3
1 2 3
1
1

output:

0 2 3 5 7
0 2 4
0

result:

ok 3 lines

Test #2:

score: 0
Accepted
time: 179ms
memory: 14056kb

input:

6107
19
10 13 8 8 11 18 12 9 15 19 6 13 11 11 17 9 14 2 18
12
1 8 10 2 10 2 6 1 5 9 5 7
16
14 4 2 15 12 14 10 3 2 9 15 4 12 9 5 15
10
3 2 5 6 7 8 6 1 6 4
18
6 5 12 12 11 2 10 10 5 10 13 15 13 10 17 7 11 2
1
1
2
1 1
3
2 1 2
17
11 15 3 10 7 15 15 10 5 17 3 3 14 13 11 11 2
3
2 2 3
7
6 1 7 5 3 5 1
7
2 1...

output:

0 2 4 6 7 9 11 16 17 19 28 31 36 41 43 51 55 67 68
0 2 4 6 6 8 10 14 17 18 22 25
0 1 3 5 7 8 11 16 22 26 26 31 33 37 42 42
0 1 3 5 7 9 11 17 19 23
0 1 3 3 4 8 10 12 16 18 27 29 30 34 36 42 46 55
0
0 0
0 1 1
0 2 4 6 9 9 9 11 15 21 27 33 35 38 42 46 55
0 0 3
0 1 3 5 8 10 14
0 1 3 4 6 9 9
0 1 1 3 7 11 ...

result:

ok 6107 lines