QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#273543 | #7108. Couleur | ikaurov# | RE | 1ms | 5596kb | C++17 | 3.8kb | 2023-12-03 01:07:33 | 2023-12-03 01:07:34 |
Judging History
answer
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
#define all(arr) (arr).begin(), (arr).end()
#define ll long long
#define ld long double
#define pb push_back
#define sz(x) (int)(x).size()
#define fi first
#define se second
#define endl '\n'
const int N = 1e5 + 20;
struct Node{
Node* l;
Node* r;
int sum;
Node(){
sum = 0;
l = nullptr, r = nullptr;
};
Node(int val){
sum = val;
l = nullptr, r = nullptr;
};
Node(Node* l1, Node* r1){
l = l1, r = r1;
sum = l -> sum + r -> sum;
};
};
Node* root[N];
Node* inc(Node* v, int tl, int tr, int idx, int val){
if (tl == tr){
return new Node(v -> sum + val);
}
int tm = (tl + tr) / 2;
if (idx <= tm) return new Node(inc(v -> l, tl, tm, idx, val), v -> r);
else return new Node(v -> l, inc(v -> r, tm + 1, tr, idx, val));
}
Node* build(int tl, int tr){
if (tl == tr) return new Node();
int tm = (tl + tr) / 2;
return new Node(build(tl, tm), build(tm + 1, tr));
}
int rsq(Node* v, int tl, int tr, int l, int r){
if (l > r) return 0;
if (tl == l && tr == r) return v -> sum;
int tm = (tl + tr) / 2;
return rsq(v -> l, tl, tm, l, min(r, tm)) +
rsq(v -> r, tm + 1, tr, max(l, tm + 1), r);
}
int n;
int getsmaller(int l, int r, int val){
return rsq(root[r], 0, n - 1, 0, val - 1) - rsq(root[l - 1], 0, n - 1, 0, val - 1);
}
int getlarger(int l, int r, int val){
return rsq(root[r], 0, n - 1, val + 1, n - 1) - rsq(root[l - 1], 0, n - 1, val + 1, n - 1);
}
int a[N], segend[N];
ll invcnt[N];
void solve(){
cin >> n;
for (int i = 0; i <= n + 1; i++) a[i] = segend[i] = invcnt[i] = 0;
for (int i = 1; i <= n; i++){
cin >> a[i];
a[i]--;
}
root[0] = build(0, n - 1);
for (int i = 1; i <= n; i++){
root[i] = inc(root[i - 1], 0, n - 1, a[i], 1);
}
set<int> starts;
multiset<ll> answers;
starts.insert(1);
auto calc_invs = [&] (int l, int r){
ll ret = 0;
for (int i = l; i <= r; i++){
ret += getlarger(l, i - 1, a[i]);
}
return ret;
};
invcnt[1] = calc_invs(1, n);
answers.insert(invcnt[1]);
segend[1] = n;
for (int idx = 0; idx < n; idx++){
ll z = *prev(answers.end());
cout << z << " ";
int i;
cin >> i;
i ^= z;
int start = *prev(starts.upper_bound(i));
int fin = segend[start];
ll valbef = invcnt[start] - getlarger(start, i - 1, a[i]) - getsmaller(i + 1, fin, a[i]);
int len1 = i - start, len2 = fin - i;
if (len1 < len2){
ll ourans = calc_invs(start, i - 1);
ll crossans = 0;
for (int j = start; j < i; j++){
crossans += getsmaller(i + 1, fin, a[j]);
}
answers.erase(answers.find(invcnt[start]));
// answers.erase(answers.find(invcnt[i + 1]));
invcnt[start] = ourans;
segend[start] = i - 1;
invcnt[i + 1] = valbef - ourans - crossans;
segend[i + 1] = fin;
starts.insert(i + 1);
answers.insert(invcnt[start]);
answers.insert(invcnt[i + 1]);
}
else{
ll ourans = calc_invs(i + 1, fin);
ll crossans = 0;
for (int j = i + 1; j <= fin; j++){
crossans += getlarger(start, i - 1, a[j]);
}
answers.erase(answers.find(invcnt[start]));
// answers.erase(answers.find(invcnt[i + 1]));
invcnt[start] = valbef - ourans - crossans;
segend[start] = i - 1;
invcnt[i + 1] = ourans;
segend[i + 1] = fin;
starts.insert(i + 1);
answers.insert(invcnt[start]);
answers.insert(invcnt[i + 1]);
}
}
cout << endl;
}
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
// cout.precision(20);
int t;
cin >> t;
while (t--){
solve();
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 5596kb
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
Runtime Error
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 ...