QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#524901 | #7937. Fast XORting | nathan4690 | WA | 3ms | 10768kb | C++17 | 2.1kb | 2024-08-20 10:12:45 | 2024-08-20 10:12:45 |
Judging History
answer
// Fast XORting
#pragma GCC optimize("Ofast,unroll-loops")
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define el cout << '\n'
#define f1(i,n) for(ll i=1;i<=n;i++)
#define __file_name ""
using namespace std;
const ll maxn = 1e6+5, inf=1e18;
struct FenwickTree{
ll n;
vector<ll> bit;
FenwickTree(ll n): n(n), bit(n+1, 0ll){};
void update(ll idx, ll v){for(;idx<=n;idx+=idx&-idx) bit[idx] += v;}
ll getSum(ll idx){
ll res = 0;
for(;idx>0;idx-=(idx&-idx)) res += bit[idx];
return res;
}
};
ll n,a[maxn],ans;
vector<ll> all;
bool flag;
ll solve(const vector<ll> &v, ll bit){
ll ans = 0;
ll c0 = 0, a0 = 0, c1 = 0, a1 = 0;
vector<ll> v0, v1;
for(ll i=v.size()-1;i>=0;i--){
ll item = v[i];
if(((item & (1 << bit)))) {
v1.push_back(item);
c1++;
a1 += c0;
}else {
v0.push_back(item);
c0++;
a0 += c1;
}
}
reverse(v0.begin(), v0.end());
reverse(v1.begin(), v1.end());
// cout << bit << ' ' << a0 << ' ' << a1;el;
ans += min(a0, a1);
if(bit){
ll vv0 = solve(v0, bit-1) + solve(v1, bit-1);
// ll vv1 = solve(v0, bit-1, 1) + solve(v1, bit-1, 1);
// if(vv1 > vv0) flag = false;
ans += vv0;
}
return ans;
}
FenwickTree fen(maxn);
int main(){
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
if(fopen(__file_name ".inp", "r")){
freopen(__file_name ".inp","r",stdin);
freopen(__file_name ".out","w",stdout);
}
// code here
cin >> n;
f1(i,n) {
cin >> a[i];
// cout << bitset<3>(a[i]).to_string();el;
}
ll preans = 0;
for(ll i=n;i>=1;i--){
preans +=fen.getSum(a[i]);
fen.update(a[i] + 1, 1);
}
f1(i,n) all.push_back(a[i]);
flag = true;
ll vv0 = solve(all, __lg(n) - 1);
/// ll vv1 = solve(all, __lg(n) - 1, 1);
// if(vv1 > vv0) flag = false;
cout << min(vv0 + 1, preans);
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 3ms
memory: 10768kb
input:
8 0 1 3 2 5 4 7 6
output:
1
result:
wrong answer 1st numbers differ - expected: '2', found: '1'