QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#203899 | #7108. Couleur | ucup-team484 | AC ✓ | 2464ms | 55992kb | C++17 | 2.4kb | 2023-10-06 21:46:41 | 2023-10-06 21:46:41 |
Judging History
answer
#include <bits/stdc++.h>
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define st first
#define nd second
using namespace std;
typedef long long ll;
const int mod = 1e9 + 7;
const int N = 1e6 + 5;
vector<int> seg[N];
void build(int l, int r, int k, vector<int>& a) {
if (l == r) {
seg[k] = {a[l]};
return;
}
int m = (l + r) / 2;
build(l, m, k * 2, a);
build(m + 1, r, k * 2 + 1, a);
seg[k].clear();
merge(all(seg[k * 2]), all(seg[k * 2 + 1]), back_inserter(seg[k]));
}
int count_smaller(int l, int r, int k, int x, int y, int v) {
if (r < x || y < l || x > y) return 0;
if (x <= l && r <= y)
return lower_bound(all(seg[k]), v) - seg[k].begin();
int m = (l + r) / 2;
return count_smaller(l, m, k * 2, x, y, v) + count_smaller(m + 1, r, k * 2 + 1, x, y, v);
}
void solve() {
int n; cin >> n;
vector<int> a(n);
vector<vector<int>> pos(n);
for (int i = 0; i < n; i++)
cin >> a[i], a[i]--, pos[a[i]].push_back(i);
ll last = 0;
vector<int> fen(n + 5);
auto upd = [&](int i) {
for (++i; i < n + 5; i += i & -i)
fen[i]++;
};
auto qry = [&](int i) {
ll r = 0;
for (++i; i > 0; i -= i & -i)
r += fen[i];
return r;
};
for (int i = n - 1; i >= 0; i--) {
for (int j = sz(pos[i]) - 1; j >= 0; j--) {
last += qry(pos[i][j]);
upd(pos[i][j]);
}
}
build(0, n - 1, 1, a);
set<int> bad;
bad.insert(-1);
bad.insert(n);
multiset<ll> tmp;
vector<ll> inv(n + 1);
inv[0] = last;
tmp.insert(last);
for (int i = 0; i < n; i++) {
last = *tmp.rbegin();
cout << last << " \n"[i + 1 == n];
ll p; cin >> p;
p ^= last;
p--;
auto it = bad.lower_bound(p);
int l = *prev(it) + 1, r = *it - 1;
tmp.erase(tmp.find(inv[l]));
ll tot = inv[l];
if (p - l < r - p) {
inv[l] = 0;
for (int j = l; j <= p; j++) {
tot -= count_smaller(0, n - 1, 1, p, r, a[j]);
if (j < p)
inv[l] += count_smaller(0, n - 1, 1, j + 1, p - 1, a[j]);
}
inv[p + 1] = tot - inv[l];
} else {
for (int j = p; j <= r; j++) {
tot -= p - l + 1 - count_smaller(0, n - 1, 1, l, p, a[j] + 1);
if (j > p)
inv[p + 1] += count_smaller(0, n - 1, 1, j + 1, r, a[j]);
}
inv[l] = tot - inv[p + 1];
}
tmp.insert(inv[l]);
tmp.insert(inv[p + 1]);
bad.insert(p);
}
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(0);
int t; cin >> t; while (t--) solve();
}
这程序好像有点Bug,我给组数据试试?
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 27032kb
input:
3 5 4 3 1 1 1 5 4 5 3 1 10 9 7 1 4 7 8 5 7 4 8 21 8 15 5 9 2 4 5 10 6 15 4 8 8 1 12 1 10 14 7 14 2 9 13 10 3 37 19 23 15 7 2 10 15 2 13 4 5 8 7 10
output:
7 0 0 0 0 20 11 7 2 0 0 0 0 0 0 42 31 21 14 14 4 1 1 1 0 0 0 0 0 0
result:
ok 3 lines
Test #2:
score: 0
Accepted
time: 2464ms
memory: 55992kb
input:
11116 10 10 5 10 3 6 4 8 5 9 8 31 27 24 11 12 3 0 2 3 1 10 8 2 7 2 8 10 1 10 9 10 6 5 2 13 2 1 0 1 3 1 10 7 10 7 6 1 3 10 6 7 9 21 18 10 1 6 5 4 8 9 10 10 2 10 4 8 8 5 7 2 6 7 20 10 9 1 15 0 4 2 9 7 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 10 1 2 3 4 5 6 7 8 9 10 6 3 5 2 7 10 9 1 4 8 10 1 10 1 3...
output:
21 18 16 12 10 6 4 1 1 0 12 12 10 10 4 4 4 2 1 0 20 16 9 5 3 3 3 0 0 0 22 14 8 8 5 5 2 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 19 12 7 4 4 2 2 1 0 0 20 18 8 3 1 1 0 0 0 0 45 21 21 10 3 3 3 0 0 0 17 11 8 2 1 1 1 0 0 0 13 4 1 0 0 0 0 0 0 0 29 27 22 15 9 7 4 3 1 0 26 16 9 2 1 1 1 1 1 0 0 0 0 0 0 ...
result:
ok 11116 lines
Extra Test:
score: 0
Extra Test Passed