树状数组+离散化(一半AC,一半WA)

P1908 逆序对

A_chicken_boy @ 2024-02-20 15:54:27

#include <bits/stdc++.h>
using namespace std ;
long long sum ;
long long n ;
long long t[500005] ;
long long a[500005] ;
long long b[500005] ;
long long c[500005] ;
long long d[500005] ;
long long m ;
void dis ( ){

    sort ( d+1 , d + n + 1 ) ;
    for ( int i = 1 ; i <= n ;++i ){
        if ( i == 1 || d[i] != d[i-1] ){
            b[++m] = d[i] ;
        }
    }
}
int qes ( int x ){
    return lower_bound (b + 1 , b + m + 1 , x ) - b ;
}
void add ( int x , int k ){
    for ( ; x <= n ; x += ( x & ( -x ) ) ) c[x] += k ;
    return ;
}
long long ask ( int x ){
    long long ans = 0 ;
    for ( ; x ; x -= ( x & ( -x ) ) ) ans += c[x] ;
    return ans ;
}
int main ( ){
    scanf  ( "%d" , &n ) ;
    for ( int i = 1 ; i <= n ; ++i ){
        scanf  ( "%d" , &a[i] ) ;
        d[i] = a[i] ;
    }
    dis ( ) ;
    for ( int i = 1 ; i <= n ; ++i ){
        a[i] = qes ( a[i] ) ;
    }
    for ( int i = n ; i >= 1 ; --i ){

        sum += ask ( a[i] - 1);
        add ( a[i] , 1 ) ;
    }
    printf ( "%d" , sum ) ;
    return 0 ;
}

by xiaoshumiao @ 2024-02-20 15:55:53

sum += ask ( a[i] - 1); 改成 sum += ask ( a[i]);


by __Rickysun__ @ 2024-02-20 16:18:30

@A_chicken_boy 楼上错了,我试过了,只要把所有的%d%lld(因为long long类型就得这么读入,不然还是int类型)就能AC(求关注)


by A_chicken_boy @ 2024-02-21 09:48:08

Thank you very much , 已关


|