QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#356926 | #7108. Couleur | karuna | AC ✓ | 2197ms | 38796kb | C++20 | 2.9kb | 2024-03-18 16:09:15 | 2024-03-18 16:09:15 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 101010;
typedef long long ll;
struct pst {
struct node {
int v, l, r;
};
vector<node> T;
int new_node() {
T.push_back({0, 0, 0});
return (int)T.size() - 1;
}
void init(int l, int r, int x) {
if (l == r) {
return;
}
int m = (l + r) / 2;
init(l, m, T[x].l = new_node());
init(m + 1, r, T[x].r = new_node());
}
int init(int l, int r) {
T.clear();
int x = new_node();
init(l, r, x);
return x;
}
void update(int a, int v, int l, int r, int x, int y) {
if (l == r) {
T[y].v = T[x].v + v;
return;
}
int m = (l + r) / 2;
if (a <= m) {
T[y].l = new_node();
T[y].r = T[x].r;
update(a, v, l, m, T[x].l, T[y].l);
}
else {
T[y].l = T[x].l;
T[y].r = new_node();
update(a, v, m + 1, r, T[x].r, T[y].r);
}
T[y].v = T[T[y].l].v + T[T[y].r].v;
}
int update(int a, int v, int l, int r, int x) {
int y = new_node();
update(a, v, l, r, x, y);
return y;
}
int query(int a, int b, int l, int r, int x) {
if (b < l || a > r) return 0;
if (a <= l && r <= b) {
return T[x].v;
}
int m = (l + r) / 2;
return query(a, b, l, m, T[x].l) + query(a, b, m + 1, r, T[x].r);
}
} t1;
int n, a[MAXN]; ll p[MAXN];
int main() {
cin.tie(0); ios_base::sync_with_stdio(0);
int T; cin >> T;
while (T--) {
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
for (int i = 1; i <= n; i++) {
cin >> p[i];
}
int idx[n + 1], ord[n + 1];
iota(idx + 1, idx + 1 + n, 1);
sort(idx + 1, idx + 1 + n, [&](int i, int j) {
return (a[i] == a[j]) ? i < j : a[i] < a[j];
});
for (int i = 1; i <= n; i++) {
ord[idx[i]] = i;
}
int R[n + 1] = {};
R[0] = t1.init(1, n);
ll tot = 0;
for (int i = 1; i <= n; i++) {
tot += t1.query(idx[i] + 1, n, 1, n, R[i - 1]);
R[i] = t1.update(idx[i], 1, 1, n, R[i - 1]);
}
ll inv[n + 1];
inv[1] = tot;
multiset<ll> st = {tot};
set<int> obs = {0, n + 1};
for (int i = 1; i <= n; i++) {
ll ans = *prev(st.end());
cout << ans << ((i == n) ? '\n' : ' ');
p[i] ^= ans;
int m = p[i];
int s = *prev(obs.lower_bound(m)) + 1;
int e = *obs.lower_bound(m) - 1;
st.erase(st.find(inv[s]));
ll tot = inv[s];
tot -= (m - s) - t1.query(s, m - 1, 1, n, R[ord[m]]);
tot -= t1.query(m + 1, e, 1, n, R[ord[m]]);
ll lft = 0, rgh = 0;
if (m - s < e - m) {
for (int x = s; x < m; x++) {
lft += t1.query(x + 1, m - 1, 1, n, R[ord[x]]);
rgh -= t1.query(m + 1, e, 1, n, R[ord[x]]);
}
rgh += tot - lft;
}
else {
for (int x = e; x > m; x--) {
rgh += t1.query(x + 1, e, 1, n, R[ord[x]]);
lft -= (m - s) - t1.query(s, m - 1, 1, n, R[ord[x]]);
}
lft += tot - rgh;
}
st.insert(lft);
st.insert(rgh);
obs.insert(m);
inv[s] = lft;
inv[m + 1] = rgh;
}
}
}
这程序好像有点Bug,我给组数据试试?
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3876kb
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: 2197ms
memory: 38796kb
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