QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#158726 | #7108. Couleur | ucup-team045# | AC ✓ | 1784ms | 28392kb | C++20 | 4.0kb | 2023-09-02 16:54:55 | 2023-09-02 16:54:56 |
Judging History
answer
#include<iostream>
#include<cstring>
#include<vector>
#include<set>
#include<array>
using namespace std;
using LL = long long;
using Info = int;
const int maxn = 1e5 + 5;
struct Node{
Info info;
int l, r;
}tr[maxn * 64];
int root[maxn];
int idx;
void modify_from(int &now, int pre, int l, int r, int x, int v){
tr[now = ++idx] = tr[pre];
if (l == r){
tr[now].info += v;
return;
}
int mid = (l + r) / 2;
if (x <= mid) modify_from(tr[now].l, tr[pre].l, l, mid, x, v);
else modify_from(tr[now].r, tr[pre].r, mid + 1, r, x, v);
tr[now].info = tr[tr[now].l].info + tr[tr[now].r].info;
}
Info query(int u, int l, int r, int L, int R){
if (r < L || l > R){
return Info();
}
if (l >= L && r <= R){
return tr[u].info;
}
int mid = (l + r) / 2;
return query(tr[u].l, l, mid, L, R) + query(tr[u].r, mid + 1, r, L, R);
}
Info query(int root1, int root2, int l, int r, int L, int R){
if (r < L || l > R){
return Info();
}
if (l >= L && r <= R){
return tr[root2].info - tr[root1].info;
}
int mid = (l + r) / 2;
return query(tr[root1].l, tr[root2].l, l, mid, L, R) + query(tr[root1].r, tr[root2].r, mid + 1, r, L, R);
}
struct Seg{
int l, r;
LL ans;
bool operator<(const Seg &t) const{
if (r != t.r) return r < t.r;
return l < t.l;
}
};
int main(){
#ifdef LOCAL
freopen("data.in", "r", stdin);
freopen("data.out", "w", stdout);
#endif
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(0);
int T;
cin >> T;
while(T--){
int n;
cin >> n;
vector<int> a(n + 1);
for(int i = 1; i <= n; i++) cin >> a[i];
for(int i = 0; i <= idx; i++)
tr[i] = {0, 0, 0};
idx = 0;
for(int i = 1; i <= n; i++) root[i] = 0;
for(int i = 1; i <= n; i++){
modify_from(root[i], root[i - 1], 1, n, a[i], 1);
}
LL last = 0;
for(int i = 1; i <= n; i++)
last += query(root[i - 1], 1, n, a[i] + 1, n);
set<Seg> s;
multiset<LL> mx;
mx.insert(last);
s.insert({1, n, last});
for(int i = 1; i <= n; i++){
cout << last << " \n"[i == n];
LL x;
cin >> x;
if (i == n) break;
x ^= last;
auto it = s.lower_bound({0, (int)x, 0});
auto [l, r, invs] = *it;
s.erase(it);
mx.erase(mx.lower_bound(invs));
invs -= query(root[l - 1], root[x - 1], 1, n, a[x] + 1, n);
invs -= query(root[x], root[r], 1, n, 1, a[x] - 1);
if (x - l <= r - x){
LL invs1 = 0, invs2 = 0;
for(int i = l; i < x; i++){
invs1 += query(root[i], root[x - 1], 1, n, 1, a[i] - 1);
invs2 += query(root[x], root[r], 1, n, 1, a[i] - 1);
}
invs2 = invs - invs2 - invs1;
if (l <= x - 1){
s.insert({l, (int)x - 1, invs1});
mx.insert(invs1);
}
if (x + 1 <= r){
s.insert({(int)x + 1, r, invs2});
mx.insert(invs2);
}
}
else{
LL invs1 = 0, invs2 = 0;
for(int i = x + 1; i <= r; i++){
invs2 += query(root[x], root[i - 1], 1, n, a[i] + 1, n);
invs1 += query(root[l - 1], root[x - 1], 1, n, a[i] + 1, n);
}
invs1 = invs - invs1 - invs2;
if (l <= x - 1){
s.insert({l, (int)x - 1, invs1});
mx.insert(invs1);
}
if (x + 1 <= r){
s.insert({(int)x + 1, r, invs2});
mx.insert(invs2);
}
}
last = *mx.rbegin();
}
}
}
这程序好像有点Bug,我给组数据试试?
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 5700kb
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: 1784ms
memory: 28392kb
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