首页 > 其他分享 >P2448 无尽的生命

P2448 无尽的生命

时间:2022-11-21 10:14:45浏览次数:55  
标签:生命 P2448 lb int long return include True 无尽

可以把连续不改变的一条线段看做一个点,重新赋值,求逆序对即可。

注意开 \(long long\) !

#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<unordered_map>
using namespace std;
const int N = 5e5 + 1 ;
#define int long long
struct node {
    int c [ N ] ;
    int lb ( int x ){ return x & ( - x ) ; }
    int update  ( int x , int y ) {
        for ( ; x < N ; x += lb ( x ) )
            c [ x ] += y ;
    }
    int query ( int x ) {
        int rhs = 0 ;
        for ( ; x ;  x -= lb ( x ) )
            rhs += c [ x ] ;
        return rhs ;
    }
}tree;
int n , ls [ N ] , m , now ;
pair < int , int > a [ N ] ;
pair < int , int > True [ N ] ;
unordered_map < int , int > map;
signed main ( ) {
    cin >> n ;
    for ( int i = 1 ; i <= n ; i ++ ) {
        scanf ( "%d %d" , & a [ i ] . first , & a [ i ] . second ) ;
        ls [ ++ m ] = a [ i ] . first;
        ls [ ++ m ] = a [ i ] . second;
    }
    std :: sort ( ls + 1 , ls + m + 1 ) ;
    m = unique ( ls + 1 , ls + m + 1 ) - ls - 1 ;
    for ( int i = 1 ; i <= m ; i ++ ) {
        if ( ls [ i ] == ls [ i - 1 ] + 1 ) ;
        else {
            now ++ ;
            True [ now ] = { now , ls [ i ] - ls [ i - 1 ] - 1 } ;
        }
        now ++ ;
        True [ now ] = { now , 1 } ;
        map [ ls [ i ] ] = now;
    }
    for ( int i = 1 ; i <= n ; i ++ ) {
        swap ( True [ map [ a [ i ] . first ] ] , True [ map [ a [ i ] . second ] ] ) ;
    }
    int ans = 0 ;
    for (int i = now ; i >= 1 ; i -- ) {
        ans += tree . query ( True [ i ] . first - 1 ) * True [ i ] . second ;
        tree . update ( True [ i ] . first , True [ i ] . second ) ;
    }
    printf ( "%lld\n" , ans ) ;
    return 0 ;
}

标签:生命,P2448,lb,int,long,return,include,True,无尽
From: https://www.cnblogs.com/dadidididi/p/16910449.html

相关文章