QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#864387 | #4802. Ternary Search | Wu_Ren | WA | 0ms | 8020kb | C++14 | 2.3kb | 2025-01-20 15:42:38 | 2025-01-20 15:42:40 |
Judging History
answer
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
int n,m,a[200010],b[200010];
ll ans[200010];
int tr[200010],mn[200010],tg[200010];
int bt[200010];
int qry(int x){
int res=0;
for(;x;x-=(x&-x)) res+=bt[x];
return res;
}
void add(int x){
for(;x<=n;x+=(x&-x)) bt[x]++;
}
void build(int l,int r,int t){
tr[t]=tg[t]=0;
if(l==r) return;
int mid=(l+r)>>1;
build(l,mid,t<<1),build(mid+1,r,t<<1|1);
}
void pushup(int t){
tr[t]=tr[t<<1]+tr[t<<1|1];
mn[t]=1e9;
if(tr[t<<1]) mn[t]=min(mn[t],mn[t<<1]);
if(tr[t<<1|1]) mn[t]=min(mn[t],mn[t<<1|1]);
}
void pushdown(int t){
if(tg[t]){
mn[t<<1]-=tg[t],tg[t<<1]+=tg[t];
mn[t<<1|1]-=tg[t],tg[t<<1|1]+=tg[t];
tg[t]=0;
}
}
void del(int ql,int qr,int l,int r,int t){
if(!tr[t]) return;
if(ql<=l&&r<=qr){
if(mn[t]>1){
mn[t]--,tg[t]++;
return;
}
if(l==r){
tr[t]=tg[t]=0;
return;
}
}
pushdown(t);
int mid=(l+r)>>1;
if(ql<=mid) del(ql,qr,l,mid,t<<1);
if(mid<qr) del(ql,qr,mid+1,qr,t<<1|1);
pushup(t);
}
int dec(int ql,int qr,int l,int r,int t){
if(ql<=l&&r<=qr) return tr[t];
pushdown(t);
int mid=(l+r)>>1,res=0;
if(ql<=mid) res+=dec(ql,qr,l,mid,t<<1);
if(mid<qr) res+=dec(ql,qr,mid+1,r,t<<1|1);
pushup(t);
return res;
}
void upd(int a,int l,int r,int t,int w){
if(l==r){
tr[t]=1;
mn[t]=w;
return;
}
int mid=(l+r)>>1;pushdown(t);
if(a<=mid) upd(a,l,mid,t<<1,w);
else upd(a,mid+1,r,t<<1|1,w);
pushup(t);
}
void work(){
build(1,n,1);
for(int i=1;i<=n;i++) bt[i]=0;
ll sum=0;
for(int i=1;i<=n;i++){
int w=qry(a[i]);
// cerr<<i<<" "<<a[i]<<" "<<w<<"\n";
sum+=dec(a[i],n,1,n,1);
del(a[i],n,1,n,1);
ans[i]=min(ans[i],sum);
if(w) upd(a[i],1,n,1,w);
add(a[i]);
}
}
int main(){
scanf("%d",&n);
for(int i=1;i<=n;i++) scanf("%d",&a[i]),b[++m]=a[i];
sort(b+1,b+m+1),m=unique(b+1,b+m+1)-b-1;
for(int i=1;i<=n;i++) a[i]=lower_bound(b+1,b+m+1,a[i])-b,ans[i]=1e18;
work();
for(int i=1;i<=n;i++) a[i]=n-a[i]+1;
work();
for(int i=1;i<=n;i++) printf("%lld\n",ans[i]);
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 8008kb
input:
9 11 4 5 14 1 9 19 8 10
output:
0 0 0 0 2 3 3 6 7
result:
ok 9 numbers
Test #2:
score: 0
Accepted
time: 0ms
memory: 8012kb
input:
1 167959139
output:
0
result:
ok 1 number(s): "0"
Test #3:
score: 0
Accepted
time: 0ms
memory: 8020kb
input:
10 802030518 983359971 96204862 640274071 796619138 71550121 799843967 598196518 402690754 446173607
output:
0 0 0 1 1 3 3 5 7 9
result:
ok 10 numbers
Test #4:
score: -100
Wrong Answer
time: 0ms
memory: 8012kb
input:
200 280383943 994080326 443906342 107926501 414713819 45732490 370693588 928126378 581740643 189811400 142817640 218386999 447997927 640003704 637428425 151698689 209462128 155599305 390080477 958251194 515653943 751042490 1186761 443171900 968975479 829084542 462837689 157692260 607049612 606116398...
output:
0 0 0 0 1 1 3 4 5 8 12 15 17 18 19 24 29 34 37 37 40 41 53 59 59 60 66 77 82 87 101 103 113 114 131 136 137 152 166 173 184 190 203 214 223 236 239 247 259 268 281 300 309 321 332 350 370 384 400 408 430 436 438 454 479 492 510 525 535 554 577 599 610 610 630 638 670 691 696 723 735 740 775 802 811 ...
result:
wrong answer 16th numbers differ - expected: '25', found: '24'