QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#103563 | #4802. Ternary Search | mod998244353 | WA | 4ms | 13836kb | C++14 | 2.1kb | 2023-05-06 17:46:58 | 2023-05-06 17:47:02 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN=500005;
ll res[MAXN];
int n,a[MAXN],b[MAXN],pos[MAXN],t[MAXN];
inline void add(int x,int y) {
for(; x<=n; x+=x&-x) t[x]+=y;
}
inline int query(int x) {
int sum=0;
for(; x; x&=x-1) sum+=t[x];
return sum;
}
struct Tree {
int l,r,lt,cnt,mx;
ll sum;
} tr[MAXN<<2];
void build(int num,int l,int r) {
tr[num].l=l,tr[num].r=r,tr[num].sum=tr[num].cnt=tr[num].lt=0;
tr[num].mx=-1e9;
if(l==r)return ;
build(num<<1,l,l+r>>1),build(num<<1|1,l+r+2>>1,r);
}
inline void push_up(int num) {
tr[num].sum=tr[num<<1].sum+tr[num<<1|1].sum;
tr[num].cnt=tr[num<<1].cnt+tr[num<<1|1].cnt;
tr[num].mx=max(tr[num<<1].mx,tr[num<<1|1].mx);
}
inline void Tag(int num,ll x) {
tr[num].sum+=tr[num].cnt*x;
tr[num].lt+=x,tr[num].mx+=x;
}
inline void push_down(int num) {
if(tr[num].lt) {
Tag(num<<1,tr[num].lt),Tag(num<<1|1,tr[num].lt);
tr[num].lt=0;
}
}
void updatel(int num,int x,int l) {
if(tr[num].l==tr[num].r) {
tr[num].sum=tr[num].mx=-l;
tr[num].cnt=(l>0);
return;
}
push_down(num);
updatel(num<<1|(x>tr[num<<1].r),x,l);
push_up(num);
}
void chk(int num) {
if(tr[num].mx<0) return;
if(tr[num].l==tr[num].r) {
tr[num].cnt=0;
return;
}
push_down(num);
if(!tr[num<<1 ].mx)chk(num<<1);
if(!tr[num<<1|1].mx)chk(num<<1|1);
return push_up(num);
}
void update(int num,int l,int r,ll x) {
if(l<=tr[num].l&&tr[num].r<=r)return Tag(num,x),chk(num);
push_down(num);
if(l<=tr[num<<1].r) update(num<<1,l,r,x);
if(tr[num<<1].r<r) update(num<<1|1,l,r,x);
return push_up(num);
}
inline void solve() {
for(int i=1; i<=n; ++i) a[i]=n-a[i]+1,pos[a[i]]=i,t[i]=0;
build(1,1,n);
ll ans=0;
for(int i=1,L=0; i<=n; ++i) {
L=query(a[i]),ans+=L;
updatel(1,a[i],L);
if(a[i]<n)update(1,a[i]+1,n,1);
add(a[i],1);
res[i]=min(res[i],ans+tr[1].sum);
}
}
int main() {
scanf("%d",&n);
for(int i=1; i<=n; ++i) scanf("%d",&a[i]),b[i]=a[i],res[i]=1e18;
sort(b+1,b+n+1);
for(int i=1; i<=n; ++i) a[i]=lower_bound(b+1,b+n+1,a[i])-b;
solve(),solve();
for(int i=1; i<=n; ++i) printf("%lld\n",res[i]);
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 1ms
memory: 13824kb
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: 4ms
memory: 11924kb
input:
1 167959139
output:
0
result:
ok 1 number(s): "0"
Test #3:
score: -100
Wrong Answer
time: 4ms
memory: 13836kb
input:
10 802030518 983359971 96204862 640274071 796619138 71550121 799843967 598196518 402690754 446173607
output:
0 0 0 1 3 3 6 7 8 10
result:
wrong answer 5th numbers differ - expected: '1', found: '3'