QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#714619 | #7108. Couleur | IUT_Bhalochelera | TL | 0ms | 3544kb | C++14 | 4.0kb | 2024-11-06 01:07:43 | 2024-11-06 01:07:43 |
Judging History
answer
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define all(V) V.begin (), V.end ()
using LL = long long;
using ordered_multiset = tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update>;
void solve (int tc) {
int n; cin>>n;
vector<int> a(n);
ordered_multiset os[n + 1];
multiset<LL> allInv;
set<pair<int, int>> ranges; ranges.insert({0, n - 1});
LL ans = 0;
for(int i = 0; i < n; i++){
cin>>a[i];
int large = i - os[0].order_of_key(a[i] + 1);
ans += large;
os[0].insert(a[i]);
}
// cout<<ans<<'\n';
allInv.insert(ans);
allInv.insert(0);
LL ansInRange[n] = {}; ansInRange[0] = ans;
for(int i = 0; i < n; i++){
LL p; cin>>p;
p ^= ans;
// cerr<<p<<" "<<ans<<'\n';
cout<<ans<<' ';
p--;
// for(auto [L, R]: ranges) cerr<<"Time "<<i<<" Set e ase --> "<<L<<" "<<R<<'\n';
auto [l, r] = *prev (ranges.upper_bound ({p, n}));
// cerr<<"LR--> "<<l<<" "<<r<<" "<<p<<'\n';
allInv.erase(allInv.find(ansInRange[l]));
ranges.erase({l, r});
if(l == r){
// an
continue;
}
if(p == r){
ansInRange[l] -= (r - l + 1 - os[l].order_of_key(a[p] + 1));
os[l].erase(os[l].find_by_order(os[l].order_of_key(a[p])));
allInv.insert(ansInRange[l]); //cout<<ansInRange[l]<<'\n';
if(p > l) ranges.insert({l, p - 1});
auto it = allInv.end(); it--;
ans = *it;
continue;
}
if(r - p > p - l){
swap(os[l], os[p + 1]);
LL tmp = 0;
for(int j = l; j < p; j++){
os[p + 1].erase(os[p + 1].find_by_order(os[p + 1].order_of_key(a[j])));
LL large = j - l - os[l].order_of_key(a[j] + 1);
tmp += large;
os[l].insert(a[j]);
}
for(int j = l; j < p; j++){
LL small = os[p + 1].order_of_key(a[j]);
ansInRange[l] -= small;
}
ansInRange[l] -= os[p + 1].order_of_key(a[p]);
os[p + 1].erase(os[p + 1].find_by_order(os[p + 1].order_of_key(a[p])));
ansInRange[p + 1] = ansInRange[l];
ansInRange[l] = tmp;
ansInRange[p + 1] -= ansInRange[l];
allInv.insert(ansInRange[l]);
allInv.insert(ansInRange[p + 1]);
}else{
LL tmp = 0;
// cout<<(r - l + 1)<<" "<<os[l].size()<<'\n';
for(int j = p + 1; j <= r; j++){
os[l].erase(os[l].find_by_order(os[l].order_of_key(a[j])));
LL large = j - p - 1 - os[p + 1].order_of_key(a[j] + 1);
tmp += large;
os[p + 1].insert(a[j]);
}
// cout<<ansInRange[l]<<'\n';
// cout<<os[l].size() <<" "<<p - l + 1<<'\n';
for(int j = p + 1; j <= r; j++){
LL large = (os[l].size()) - os[l].order_of_key(a[j] + 1);
ansInRange[l] -= large;
}
ansInRange[l] -= (os[l].size()) - os[l].order_of_key(a[p] + 1);
os[l].erase(os[l].find_by_order(os[l].order_of_key(a[p])));
ansInRange[p + 1] = tmp;
ansInRange[l] -= ansInRange[p + 1];
allInv.insert(ansInRange[l]); //cout<<ansInRange[l]<<'\n';
allInv.insert(ansInRange[p + 1]);
}
if(p > l)
ranges.insert({l, p - 1});
if(r > p)
ranges.insert({p + 1, r});
// for(auto ij: allInv) cerr<<ij<<" "; cerr<<'\n';
auto it = allInv.end(); it--;
ans = *it;
}
cout<<'\n';
}
int main () {
cin.tie (nullptr) -> ios_base :: sync_with_stdio (false);
int tests = 1;
cin >> tests;
for (int tc = 1; tc <= tests; tc++) {
solve (tc);
}
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 3544kb
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: -100
Time Limit Exceeded
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 ...