QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#158713 | #7108. Couleur | ucup-team228# | AC ✓ | 4093ms | 241044kb | C++20 | 7.7kb | 2023-09-02 16:53:31 | 2023-09-02 16:53:32 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5 + 10;
const int B = 700;
const int M = N / B + 10;
struct fenwick1d {
int t[N];
void update(int pos, int val) {
for (int i = pos; i < N; i = i | (i + 1)) {
t[i] += val;
}
}
int get_pref_sum(int pref) {
int res = 0;
for (int i = pref; i >= 0; i = (i & (i + 1)) - 1) {
res += t[i];
}
return res;
}
int get(int l, int r) {
if (l > r) return 0;
else if (l == 0) return get_pref_sum(r);
else return get_pref_sum(r) - get_pref_sum(l - 1);
}
};
struct fenwick1d_small {
static const int N = B + 10;
int t[N];
void update(int pos, int val) {
for (int i = pos; i < N; i = i | (i + 1)) {
t[i] += val;
}
}
int get_pref_sum(int pref) {
int res = 0;
for (int i = pref; i >= 0; i = (i & (i + 1)) - 1) {
res += t[i];
}
return res;
}
int get(int l, int r) {
if (l > r) return 0;
else if (l == 0) return get_pref_sum(r);
else return get_pref_sum(r) - get_pref_sum(l - 1);
}
};
fenwick1d tree;
fenwick1d_small tree_small;
int a[N];
ll p[N];
ll ans[N];
int l_id[N], r_id[N];
int lef[N], rig[N];
ll mem_lef[M][N], mem_rig[M][N];
pair<int, int> b[N];
int pos[N];
int flag[N];
ll get(int l, int r) {
if (l == lef[l]) {
return mem_lef[l_id[l]][r];
} else if (r == rig[r]) {
return mem_rig[r_id[r]][l];
} else if (lef[l] == lef[r]) {
ll res = 0;
int L = lef[l];
for (int i = l; i <= r; i++) {
res += tree_small.get(pos[i] - L + 1, rig[l] - lef[l] + 1);
tree_small.update(pos[i] - L + 1, 1);
}
for (int i = l; i <= r; i++) {
tree_small.update(pos[i] - L + 1, -1);
}
return res;
} else {
int L = l == lef[l] ? l : rig[l] + 1;
int R = r == rig[r] ? r : lef[r] - 1;
ll res = 0;
if (L <= R) {
res += mem_lef[l_id[L]][r] + mem_rig[r_id[R]][l] - mem_lef[l_id[L]][R];
} else {
res += mem_rig[r_id[rig[l]]][l] + mem_lef[l_id[lef[r]]][r];
}
// cout << l << " " << L << " " << R << " " << r << " " << res << "\n";
for (int i = l; i <= L - 1; i++) {
flag[pos[i]]++;
}
for (int i = R + 1; i <= r; i++) {
flag[pos[i]]++;
}
int x = lef[l], y = lef[r];
int cnt_lef = 0, cnt_rig = 0;
while (x <= rig[l] && y <= rig[r]) {
if (b[x].first <= b[y].first) {
res += 1ll * flag[x] * cnt_rig;
cnt_lef += flag[x];
x++;
} else {
cnt_rig += flag[y];
y++;
}
}
while (x <= rig[l]) {
res += 1ll * flag[x] * cnt_rig;
cnt_lef += flag[x];
x++;
}
while (y <= rig[r]) {
cnt_rig += flag[y];
y++;
}
for (int i = l; i <= L - 1; i++) {
flag[pos[i]]--;
}
for (int i = R + 1; i <= r; i++) {
flag[pos[i]]--;
}
return res;
}
}
ll slow_get(int l, int r) {
ll res = 0;
for (int i = l; i <= r; i++) {
for (int j = i + 1; j <= r; j++) {
res += a[i] > a[j];
}
}
return res;
}
void precalc(int n) {
for (int i = 1; i <= n; i++) {
l_id[i] = r_id[i] = 0;
}
for (int l = 1; l <= n; l += B) {
int r = min(n, l + B - 1);
l_id[l] = r_id[r] = (l + B - 1) / B;
for (int i = l; i <= r; i++) {
lef[i] = l;
rig[i] = r;
}
mem_lef[l_id[l]][l - 1] = 0;
for (int i = l; i <= n; i++) {
mem_lef[l_id[l]][i] = mem_lef[l_id[l]][i - 1] + tree.get(a[i] + 1, n);
tree.update(a[i], 1);
}
for (int i = l; i <= n; i++) {
tree.update(a[i], -1);
}
mem_rig[r_id[r]][r + 1] = 0;
for (int i = r; i >= 1; i--) {
mem_rig[r_id[r]][i] = mem_rig[r_id[r]][i + 1] + tree.get(1, a[i] - 1);
tree.update(a[i], 1);
}
for (int i = r; i >= 1; i--) {
tree.update(a[i], -1);
}
for (int i = l; i <= r; i++) {
b[i] = {a[i], i};
}
sort(b + l, b + r + 1);
for (int i = l; i <= r; i++) {
pos[b[i].second] = i;
}
}
}
void solve(int n) {
precalc(n);
// for (int len = 1; len <= n; len++) {
// for (int l = 1; l + len - 1 <= n; l++) {
// int r = l + len - 1;
// if (get(l, r) != slow_get(l, r)) {
// cout << l << " " << r << " " << get(l, r) << " " << slow_get(l, r) << "\n";
// return;
// }
// }
// }
multiset<ll> ms;
ms.insert(get(1, n));
set<int> dead;
dead.insert(0);
dead.insert(n + 1);
for (int i = 1; i <= n; i++) {
ans[i] = *ms.rbegin();
p[i] ^= ans[i];
// cout << i << " " << ans[i] << " " << p[i] << "\n";
if (1 > p[i] || p[i] > n || dead.count(p[i])) {
cout << "DEAD\n";
break;
}
// assert(1 <= p[i] && p[i] <= n && !dead.count(p[i]));
int l, r;
auto it = dead.lower_bound(p[i]);
r = *it - 1;
it--;
l = *it + 1;
ms.erase(ms.find(get(l, r)));
if (l < p[i]) {
// cout << "? " << l << " " << p[i] - 1 << " " << get(l, p[i] - 1) << "\n";
ms.insert(get(l, p[i] - 1));
}
if (p[i] < r) {
// cout << "? " << p[i] + 1 << " " << r << " " << get(p[i] + 1, r) << "\n";
ms.insert(get(p[i] + 1, r));
}
dead.insert(p[i]);
}
}
void stress() {
mt19937 rnd;
while (true) {
int n = rnd() % 10 + 1;
for (int i = 1; i <= n; i++) {
a[i] = rnd() % n + 1;
}
precalc(n);
int p = 0, q = 0;
for (int len = 1; len <= n; len++) {
for (int l = 1; l + len - 1 <= n; l++) {
int r = l + len - 1;
if (get(l, r) != slow_get(l, r)) {
p = l;
q = r;
}
}
}
if (p == 0) {
cout << "OK" << endl;
} else {
cout << "WA\n";
cout << "1\n";
cout << n << "\n";
for (int i = 1; i <= n; i++) {
cout << a[i] << " ";
}
cout << "\n";
for (int i = 1; i <= n; i++) {
cout << i << " ";
}
cout << "\n\n";
cout << p << " " << q << "\n";
cout << get(p, q) << " " << slow_get(p, q) << "\n";
break;
}
}
exit(0);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
#ifdef LOCAL
freopen("input.txt", "r", stdin);
#endif
// stress();
// cout << sizeof(mem_lef) / (1 << 20) << "\n";
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
for (int i = 1; i <= n; i++) {
cin >> p[i];
}
solve(n);
for (int i = 1; i <= n; i++) {
cout << ans[i] << " ";
}
cout << "\n";
}
#ifdef LOCAL
cout << "\nTime elapsed: " << double(clock()) / CLOCKS_PER_SEC << " s.\n";
#endif
}
这程序好像有点Bug,我给组数据试试?
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 11892kb
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: 4093ms
memory: 241044kb
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