QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#162601 | #7108. Couleur | ucup-team1209# | AC ✓ | 950ms | 29152kb | C++20 | 3.3kb | 2023-09-03 14:52:55 | 2023-09-03 14:52:56 |
Judging History
answer
#include<bits/stdc++.h>
using std::cin;
using std::cout;
using ll = long long;
const int N = 100005;
const int M = N * 20;
int ls[M], rs[M], size[M], cnt;
int n;
int add(int rt, int p, int l = 1, int r = n) {
int x = ++ cnt;
size[x] = size[rt] + 1;
if(l == r) return x;
int mid = (l + r) >> 1;
if(p <= mid) {
ls[x] = add(ls[rt], p, l, mid);
rs[x] = rs[rt];
} else {
ls[x] = ls[rt];
rs[x] = add(rs[rt], p, mid + 1, r);
}
return x;
}
int a[N];
int L[N], R[N];
int root[N];
std::multiset<ll> answers;
int bit[N];
ll ans[N];
using ll = long long;
void clear() {
answers.clear();
memset(L + 1, 0, n << 2);
memset(R + 1, 0, n << 2);
memset(a + 1, 0, n << 2);
memset(root + 1, 0, n << 2);
memset(bit + 1, 0, n << 2);
memset(ans + 1, 0, n << 3);
cnt = 0;
}
ll calc(int l, int r) {
ll ans = 0;
for(int i = l;i <= r;++i) {
for(int x = a[i];x;x &= x - 1) {
bit[x] += 1;
}
for(int x = a[i] + 1;x <= n;x += x & -x) {
ans += bit[x];
}
}
for(int i = l;i <= r;++i)
for(int x = a[i];x;x &= x - 1)
bit[x] = 0;
return ans;
}
using IT = std::vector<int>::iterator;
ll calc(int rt0, int rt1, IT ql, IT qr, int l = 1, int r = n) {
if(ql == qr) return 0;
if(size[rt0] == size[rt1]) return 0;
if(l == r) return (qr - lower_bound(ql, qr, l)) * (size[rt1] - size[rt0]);
int mid = (l + r) >> 1;
auto qm = upper_bound(ql, qr, mid);
ll ret = 0;
ret += calc(ls[rt0], ls[rt1], ql, qm, l, mid);
ret += calc(rs[rt0], rs[rt1], qm, qr, mid + 1, r);
ret += (size[ls[rt1]] - size[ls[rt0]]) * (qr - qm);
return ret;
}
void set(int l, int r, ll v) {
if(l > r) return ;
R[l] = r, L[r] = l, ans[l] = v;
answers.emplace(v);
}
void split(int l, int mid, int r) {
ll all = ans[l];
answers.erase(answers.find(all));
int Llen = mid - l;
int Rlen = r - mid;
if(Llen > Rlen) {
ll ra = calc(mid + 1, r);
std::vector<int> x(a + mid + 1, a + r + 1);
sort(x.begin(), x.end());
auto pos = lower_bound(x.begin(), x.end(), a[mid]);
all -= ra;
all -= pos - x.begin();
x.insert(pos, a[mid]);
ll val = calc(root[l - 1], root[mid - 1], x.begin(), x.end());
all -= ll(mid - l) * x.size() - val;
set(l, mid - 1, all);
set(mid + 1, r, ra);
} else {
ll la = calc(l, mid - 1);
std::vector<int> x(a + l, a + mid);
sort(x.begin(), x.end());
auto pos = upper_bound(x.begin(), x.end(), a[mid]);
all -= la;
all -= x.end() - pos;
x.insert(pos, a[mid]);
for(int & p : x) -- p;
ll val = calc(root[mid], root[r], x.begin(), x.end());
all -= val;
set(l, mid - 1, la);
set(mid + 1, r, all);
}
}
int main() {
std::ios::sync_with_stdio(false), cin.tie(0);
int T;
#ifdef zqj
freopen("$.in", "r", stdin);
#endif
cin >> T;
for(int i = 0;i < T;++i) {
cin >> n;
for(int i = 1;i <= n;++i) {
cin >> a[i];
root[i] = add(root[i - 1], a[i]);
}
set(1, n, calc(1, n));
for(int i = 1;i <= n;++i) {
cout << *answers.rbegin() << " \n"[i == n];
ll p; cin >> p;
p ^= *answers.rbegin();
int le = -1, ri = -1;
for(int x = 0;;++x) {
if(p > x && R[p - x]) {
le = p - x, ri = R[p - x];
break;
}
if(p + x <= n && L[p + x]) {
le = L[p + x], ri = p + x;
break;
}
}
split(le, p, ri);
}
clear();
}
}
这程序好像有点Bug,我给组数据试试?
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 6004kb
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: 950ms
memory: 29152kb
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