QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#338422 | #4802. Ternary Search | Jryno1 | WA | 12ms | 88924kb | C++14 | 1.5kb | 2024-02-25 21:40:00 | 2024-02-25 21:40:00 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define lowbit(x) (x&(-x))
const int maxn=2e6+10;
int a[maxn],hsh[maxn],pos[maxn],n,ans[maxn];
struct Fenwick{
int tr[maxn];
void cl(){memset(tr,0,sizeof(tr));}
void CT(int p,int x){for(;p<maxn;p+=lowbit(p))tr[p]+=x;}
int QT(int p){
int res=0;
for(;p;p-=lowbit(p))res+=tr[p];
return res;
}
}T,R;
vector<int>del[maxn];
signed main(){
cin.tie(0),cout.tie(0),ios::sync_with_stdio(false);
cin>>n;
for(int i=1;i<=n;i++)cin>>a[i],hsh[i]=a[i];
sort(hsh+1,hsh+1+n);
for(int i=1;i<=n;i++)a[i]=lower_bound(hsh+1,hsh+1+n,a[i])-hsh,pos[a[i]]=i;
T.cl();
for(int i=n;i>=1;i--){
int l=pos[i],r=n,res=0,nowval=T.QT(pos[i]);
while(l<=r){
int mid=(l+r)>>1;
if(T.QT(mid)-nowval>=nowval){
res=mid;
r=mid-1;
} else l=mid+1;
}
del[res].push_back(i);
T.CT(pos[i],1);
}
int pre=0;
R.cl();
for(int i=1;i<=n;i++){
pre+=R.QT(a[i]);
R.CT(a[i],1);
for(auto s:del[i])T.CT(s,-1);
ans[i]=pre;
}
for(int i=1;i<=n;i++)a[i]=a[n-a[i]+1],pos[a[i]]=i;
T.cl();
for(int i=n;i>=1;i--){
int l=pos[i],r=n,res=0,nowval=T.QT(pos[i]);
while(l<=r){
int mid=(l+r)>>1;
if(T.QT(mid)-nowval>=nowval){
res=mid;
r=mid-1;
} else l=mid+1;
}
del[res].push_back(i);
T.CT(pos[i],1);
}
pre=0;
R.cl();
for(int i=1;i<=n;i++){
pre+=R.QT(a[i]);
R.CT(a[i],1);
for(auto s:del[i])T.CT(s,-1);
ans[i]=min(ans[i],pre);
}
for(int i=1;i<=n;i++)cout<<ans[i]<<" ";
return 0;
}
/*
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 12ms
memory: 88924kb
input:
9 11 4 5 14 1 9 19 8 10
output:
0 0 1 4 4 7 13 16 21
result:
wrong answer 3rd numbers differ - expected: '0', found: '1'