zoukankan      html  css  js  c++  java
  • AC日记——逆序对 洛谷 P1908

    逆序对

    思路:

      线段树水过;

    代码:

    #include <cstdio>
    #include <cstring>
    #include <iostream>
    #include <algorithm>
    
    using namespace std;
    
    #define maxn 40005
    #define ll long long
    
    inline void in(int &now)
    {
        char Cget=getchar();now=0;
        while(Cget>'9'||Cget<'0') Cget=getchar();
        while(Cget>='0'&&Cget<='9')
        {
            now=now*10+Cget-'0';
            Cget=getchar();
        }
    }
    
    inline void in(ll &now)
    {
        char Cget=getchar();now=0;
        while(Cget>'9'&&Cget<'0') Cget=getchar();
        while(Cget>='0'&&Cget<='9')
        {
            now=now*10+Cget-'0';
            Cget=getchar();
        }
    }
    
    struct TreeNodeType {
        int l,r,mid,dis;
        
        TreeNodeType *lc,*rc;
        
        TreeNodeType() {lc=NULL,rc=NULL,dis=0;}
    };
    
    class NiXuDuiWorkPType {
        private:
            int ai[maxn],bi[maxn],n,size;
            
            ll ans;
            
            TreeNodeType *root;
            
            void build(TreeNodeType *&now,int l,int r)
            {
                now=new TreeNodeType,now->l=l,now->r=r;
                if(l==r) return ;now->mid=l+r>>1;
                build(now->lc,l,now->mid),build(now->rc,now->mid+1,r);
            }
            
            void updata(TreeNodeType *now,int to)
            {
                if(now->l==now->r) {now->dis++;return;}
                if(to<=now->mid) updata(now->lc,to);else updata(now->rc,to);
                now->dis=now->lc->dis+now->rc->dis;
            }
            
            void query(TreeNodeType *now,int l,int r)
            {
                if(now->l>=l&&now->r<=r) {ans+=now->dis;return;}
                if(l<=now->mid) query(now->lc,l,min(now->mid,r));
                if(r>now->mid) query(now->rc,max(now->mid+1,l),r);
            }
            
        public:
            NiXuDuiWorkPType()
            {
                in(n);for(int i=1;i<=n;i++) in(ai[i]),bi[i]=ai[i];
                sort(bi+1,bi+n+1),size=unique(bi+1,bi+n+1)-bi-1;build(root,1,size);
                for(int i=1;i<=n;i++)
                {
                    ai[i]=lower_bound(bi+1,bi+size+1,ai[i])-bi;
                    if(ai[i]<size) query(root,ai[i]+1,size);updata(root,ai[i]);
                }
                cout<<ans;
            }
    };
    class NiXuDuiWorkPType pos;
    
    int main()
    {
        return 0;
    }
  • 相关阅读:
    POJ3984-迷宫问题【BFS】
    BFS与DFS模板
    nyoj27-水池数目【DFS】
    C++ STL-stack使用详解
    C++ STL
    HDU1058
    HDU1114
    HDU1867
    Codeforces Round #461 (Div. 2) D. Robot Vacuum Cleaner
    Codeforces Round #461 (Div. 2) C. Cave Painting
  • 原文地址:https://www.cnblogs.com/IUUUUUUUskyyy/p/6934966.html
Copyright © 2011-2022 走看看