zoukankan      html  css  js  c++  java
  • 线段树求逆序对

    前言

     一直对线段树没有感觉,只会打板子,今天测试彻底被虐惨了,决定好好学习线段树,求逆序对个数是线段树的一个经典问题,那么从它开始吧。

    题目描述

    猫猫TOM和小老鼠JERRY最近又较量上了,但是毕竟都是成年人,他们已经不喜欢再玩那种你追我赶的游戏,现在他们喜欢玩统计。最近,TOM老猫查阅到一个人类称之为“逆序对”的东西,这东西是这样定义的:对于给定的一段正整数序列,逆序对就是序列中ai>aj且i<j的有序对。知道这概念后,他们就比赛谁先算出给定的一段正整数序列中逆序对的数目。
    Update:数据已加强。

    输入输出格式

    输入格式:

    第一行,一个数n,表示序列中有n个数。

    第二行n个数,表示给定的序列。

    输出格式:

    给定序列中逆序对的数目。

    solution

    朴素算法求逆序对需要o(n^2),无法通过此题

    可以使用归并排序、树状数组、线段树

    线段树的思路是使用权值线段树,逐个插入数字,设插入数字为i,是原数列中的第k个,可以使用线段树查询出 [ 1 , n ]内数字的个数,设个数为count,再用k-count就是比插入数字大的数的个数,

    且因为数列未改动,同时也是位置靠前的数,就是逆序对,计数即可

    tip

    注意可能数字过大,但权值线段树的下标代表的是数字,会开不下线段树,这时候需要离散化处理

        for(int i=1;i<=n;++i) b[i]=a[i];//b[i]是离散化后的数组 
        sort(b+1,b+n+1);//很关键 
        int len=unique(b+1,b+n+1)-b-1;//统计不同地数的个数 
        for(int i=1;i<=n;i++)
        {
            int place=lower_bound(b+1,b+len+1,a[i])-b;
            //printf("%d ",place);
        }

     code

    因为我一直调不出来懒得写,咕了好久

    #include<cstdio>
    #include<iostream>
    #include<algorithm>
    #define re register
    #define maxn 500010
    #define lson x<<1
    #define rson x<<1|1
    using namespace std;
    struct T{
        int v,l,r;
    }t[maxn<<2];
    int a[maxn],b[maxn],n;
    long long ans;
    inline int read()
    {
        int x=0,f=1; char ch=getchar();
        while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
        while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
        return x*f;
    }
    inline void push_up(int x)
    {
        t[x].v=t[lson].v+t[rson].v;
    }
    void build(int x,int l,int r)
    {
        t[x].l=l,t[x].r=r,t[x].v=0;
        if(l==r) return;
        int mid=(l+r)>>1; 
        build(lson,l,mid);
        build(rson,mid+1,r);
    }
    void update(int x,int pos)
    {
        if(t[x].l==t[x].r)
        {
            ++t[x].v;
            return;
        }
        int mid=(t[x].l+t[x].r)>>1;
        if(pos<=mid) update(lson,pos);
        else update(rson,pos);
        push_up(x);
    }
    long long query(int x,int ql,int qr)
    {
        if(ql<=t[x].l&&t[x].r<=qr) return t[x].v;
        int mid=(t[x].l+t[x].r)>>1;
        long long tmp=0;
        if(ql<=mid) tmp+=query(lson,ql,qr);
        if(mid<qr)  tmp+=query(rson,ql,qr);
        return tmp;
    }
    int main()
    {
        n=read();
        for(re int i=1;i<=n;++i) a[i]=read(),b[i]=a[i];
        build(1,1,n);
        sort(b+1,b+n+1);
        int len=unique(b+1,b+n+1)-b-1;
        for(re int i=1;i<=n;++i)
        {
            int tmp=lower_bound(b+1,b+len+1,a[i])-b;
            if(tmp!=n)ans+=query(1,tmp+1,n); //注意特判,最大的数没有贡献,不用询问,否则会导致数组越界
            update(1,tmp);//先询问再加入
        }
        printf("%lld
    ",ans);//不开long long 见祖宗
        return 0;
    }
  • 相关阅读:
    Discuz!X/数据库操作方法
    使用 HTML5, javascript, webrtc, websockets, Jetty 和 OpenCV 实现基于 Web 的人脸识别
    ECShop模板原理
    ecshop中smarty最常用的6大基本语法
    Laravel学习笔记
    Laravel的目录结构分析
    Intellij Idea 常用快捷键
    Code optimization and organization in Javascript / jQuery
    Bossies 2015: The Best of Open Source Software Awards
    解决jetty runner锁定js
  • 原文地址:https://www.cnblogs.com/Liuz8848/p/10371938.html
Copyright © 2011-2022 走看看