• HDU2688 Rotate


      用树状数组求逆序对数。。。

    #include <cstdio>
    #include <cstdlib>
    #include <cmath>
    #include <cstring>
    #include <iostream>
    #define MAX 3000005
    #define MaxN 10005
    
    using namespace std;
    
    int ans[MAX];
    __int64 c[MaxN];
    
    inline void updata( int s )
    {
        while( s < MaxN )
        {
            c[s]++;
            s += ( s & (-s) );
        }
    }
    
    inline __int64 getsum( int s )
    {
        __int64 sum = 0;
        while( s >= 1 )
        {
            sum += c[s];
            s -= s & -s;
        }   
        return sum; 
    }
    
    inline void getint( int &t ) 
    { 
        char c; 
        while( c= getchar(), c< '0'|| c> '9' ) ; 
        t= c- '0'; 
        while( c= getchar(), c>= '0'&& c<= '9' ) 
        { 
            t= t* 10+ c- '0'; 
        } 
    }
    
    int main()
    {
        int N, M;
        char op[5];
        while( scanf( "%d", &N ) == 1 )
        {
            __int64 sum = 0;
            memset( c, 0, sizeof( c ) );
            for( int i = 0; i < N; ++i )
            {
                getint( ans[i] );
                updata( ans[i]+1 );
                sum += getsum( ans[i] );
            }
            scanf( "%d", &M );
            while( M-- )
            {
                scanf( "%s", op );
                switch( op[0] )
                {
                    case 'Q':
                    {
                        printf( "%I64d\n", sum );
                        break;        
                    }
                    case 'R':
                    {
                        int s, e, v;
                        scanf( "%d %d", &s, &e );
                        v = ans[s];
                        for( int i = s; i < e; ++i )
                        {
                            ans[i] = ans[i+1];
                            if( v < ans[i] )
                                sum--;
                            if( v > ans[i] )
                                sum++;
                        }
                        ans[e] = v;
                        break;
                    }
                }
                
            }
        }
        return 0;
    }
    
  • 相关阅读:
    vue官方实例-组件
    数据处理-js
    图片大于div时的居中显示
    angularjs select通过动态加载option有空白项的处理方法-
    背景图片自适应div
    input-text
    input-number-required
    input-number-not-required
    null与undefined的区别?
    是true还是false呢?
  • 原文地址:https://www.cnblogs.com/Lyush/p/2351809.html
Copyright © 2020-2023  润新知