QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#390872 | #7108. Couleur | KKT89 | AC ✓ | 1021ms | 21764kb | C++17 | 4.2kb | 2024-04-16 00:29:44 | 2024-04-16 00:29:45 |
Judging History
answer
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll myRand(ll B) { return (ull)rng() % B; }
// 0-indexed
template <typename T> struct BIT {
int n;
vector<T> bit, ary;
BIT(int n = 0) : n(n), bit(n + 1), ary(n) {}
T operator[](int k) { return ary[k]; }
// [0, i)
T sum(int i) {
T res = 0;
for (; i > 0; i -= (i & -i)) {
res += bit[i];
}
return res;
}
// [l, r)
T sum(int l, int r) { return sum(r) - sum(l); }
void add(int i, T a) {
ary[i] += a;
i++;
for (; i <= n; i += (i & -i)) {
bit[i] += a;
}
}
};
void slv() {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
vector<BIT<int>> bit_memo(n);
// [l, r) の転倒数と左端を始点としたBITを返す
auto calc = [&](int l, int r) -> pair<ll, BIT<int>> {
// 座圧
vector<int> z(r - l);
for (int i = l; i < r; ++i) {
z[i - l] = a[i];
}
sort(z.begin(), z.end());
z.erase(unique(z.begin(), z.end()), z.end());
for (int i = l; i < r; ++i) {
a[i] = lower_bound(z.begin(), z.end(), a[i]) - z.begin();
}
ll inv = 0;
BIT<int> bit(z.size());
for (int i = l; i < r; ++i) {
inv += (i - l - bit.sum(a[i] + 1));
bit.add(a[i], 1);
}
return {inv, bit};
};
// setで区間を管理 [l, r)
set<pair<int, int>> st;
st.insert({0, n});
// 転倒数の値を管理
multiset<ll> mst;
vector<ll> inv_memo(n);
// 初期化
auto init = calc(0, n);
mst.insert(init.first);
inv_memo[0] = init.first;
bit_memo[0] = init.second;
for (int i = 0; i < n; ++i) {
ll val = *mst.rbegin();
cout << val << " ";
ll mid;
cin >> mid;
mid = (mid ^ val) - 1;
auto it = st.lower_bound({mid, 1e9});
auto [l, r] = *(--it);
st.erase(it);
val = inv_memo[l];
mst.erase(mst.find(val));
// 左端を消す場合
if (l == mid) {
if (mid + 1 != r) {
val -= bit_memo[l].sum(a[l]);
bit_memo[l].add(a[l], -1);
swap(bit_memo[l], bit_memo[l + 1]);
st.insert({l + 1, r});
mst.insert(val);
inv_memo[l + 1] = val;
}
continue;
}
// 右端を消す場合
if (mid + 1 == r) {
val -= (r - l) - bit_memo[l].sum(a[r - 1] + 1);
bit_memo[l].add(a[r - 1], -1);
st.insert({l, r - 1});
mst.insert(val);
inv_memo[l] = val;
continue;
}
// 区間が2つに分かれる場合
if (mid - l <= r - mid) {
for (int j = l; j <= mid; ++j) {
val -= bit_memo[l].sum(a[j]);
bit_memo[l].add(a[j], -1);
}
swap(bit_memo[l], bit_memo[mid + 1]);
st.insert({mid + 1, r});
mst.insert(val);
inv_memo[mid + 1] = val;
auto nxt = calc(l, mid);
bit_memo[l] = nxt.second;
st.insert({l, mid});
mst.insert(nxt.first);
inv_memo[l] = nxt.first;
} else {
for (int j = r - 1; j >= mid; --j) {
val -= (j + 1 - l) - bit_memo[l].sum(a[j] + 1);
bit_memo[l].add(a[j], -1);
}
st.insert({l, mid});
mst.insert(val);
inv_memo[l] = val;
auto nxt = calc(mid + 1, r);
bit_memo[mid + 1] = nxt.second;
st.insert({mid + 1, r});
mst.insert(nxt.first);
inv_memo[mid+1] = nxt.first;
}
}
cout << "\n";
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int q;
cin >> q;
while (q--) {
slv();
}
}
这程序好像有点Bug,我给组数据试试?
详细
Test #1:
score: 100
Accepted
time: 1ms
memory: 3572kb
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: 1021ms
memory: 21764kb
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 ...
result:
ok 11116 lines
Extra Test:
score: 0
Extra Test Passed