QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#785804#9663. Reverse Pairs Coloringrotcar07WA 1ms5736kbC++23653b2024-11-26 19:15:542024-11-26 19:16:16

Judging History

This is the latest submission verdict.

  • [2024-11-26 19:16:16]
  • Judged
  • Verdict: WA
  • Time: 1ms
  • Memory: 5736kb
  • [2024-11-26 19:15:54]
  • Submitted

answer

#include<bits/stdc++.h>
using namespace std;
constexpr int maxn=3e5+5;
int n,t[maxn],a[maxn],pos[maxn];
inline void add(int x,int y){while(x<=n)t[x]+=y,x+=x&-x;}
inline int query(int x){int res=0;while(x)res+=t[x],x-=x&-x;return res;}
int main(){
    std::ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    cin>>n;
    for(int i=1;i<=n;i++)cin>>a[i],pos[a[i]]=i;
    long long ans=0;
    for(int i=n;i>=1;i--){
        if(pos[a[i]-1]<pos[a[i]]) ans-=query(a[i]);
        add(a[i],1);add(max(a[i],a[i+1]),-1);
        if(pos[a[i]]<pos[a[i]+1]) ans+=query(min(n,a[i]+1));
        // cout<<i<<' '<<ans<<'\n';
    }
    cout<<ans<<'\n';
}

詳細信息

Test #1:

score: 100
Accepted
time: 1ms
memory: 5736kb

input:

9
5 9 1 8 2 6 4 7 3

output:

5

result:

ok single line: '5'

Test #2:

score: 0
Accepted
time: 1ms
memory: 5612kb

input:

2
1 2

output:

0

result:

ok single line: '0'

Test #3:

score: -100
Wrong Answer
time: 1ms
memory: 5688kb

input:

2
2 1

output:

0

result:

wrong answer 1st lines differ - expected: '1', found: '0'