数据不够强,线段树仍可AC

P1908 逆序对

寒冰大大 @ 2019-09-12 20:13:11

#include<iostream>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<queue>
#include<stack>
#include<vector>
#include<map>
#include<set>
#include<algorithm>

#define lson i<<1,l,mid
#define rson i<<1|1,mid+1,r
#define ls i<<1
#define rs i<<1|1

using namespace std;

int n,a[500500],b[500500];
long long s;

struct tree{
    int sum;
}t[2002000];

struct orztree{

    inline void pushup(int i)
    {
        t[i].sum=t[ls].sum+t[rs].sum;
    }

    void change_tree(int i,int l,int r,int x)
    {
        if(l==r&&l==x)
        {
            t[i].sum++;
            return ;
        }
        int mid=(l+r)>>1;
        if(x<=mid) change_tree(lson,x);
        else change_tree(rson,x);
        pushup(i);
    }

    int ask_sum_tree(int i,int l,int r,int x,int y)
    {
        if(x>y) return 0;
        if(l>=x&&r<=y) return t[i].sum;
        int mid=(l+r)>>1;
        int ans=0;
        if(x<=mid) ans+=ask_sum_tree(lson,x,y);
        if(y>mid) ans+=ask_sum_tree(rson,x,y);
        return ans;
    }

}st;

inline int getplace(int tar)
{
    register int mid,l=1,r=n;
    while(l<=r)
    {
        mid=(l+r)>>1;
        if(a[mid]<tar) l=mid+1;
        if(a[mid]>tar) r=mid-1;
        if(a[mid]==tar) return mid;
    }
}

int main()
{
    register int i,j;
    ios::sync_with_stdio(false);
    cin>>n;
    for(i=1;i<=n;i++) cin>>a[i],b[i]=a[i];
    sort(a+1,a+n+1);
    for(i=1;i<=n;i++) b[i]=getplace(b[i]);
    for(i=n;i>=1;i--) s+=st.ask_sum_tree(1,1,n,1,b[i]-1),st.change_tree(1,1,n,b[i]);
    cout<<s;
    return 0;
}

https://www.luogu.org/record/23881334


by mzgwty @ 2019-09-12 20:45:00

众所周知,5e5这个数据连splay都能过


by 142857cs @ 2019-09-12 20:51:26

@skydogli 3e5是分块极限?你看P5048


by ix35 @ 2019-09-12 21:06:44

这个数据不是线段树躺着过吗


by 反比例函数 @ 2019-09-12 21:08:57

此题用线段树作甚?归并即可


by Thaumaturge @ 2019-09-12 21:48:20

线 段 树 常 数 大 ??? 哪 方 面 ???


by skydogli @ 2019-09-12 22:03:05

@142857cs 对不起大佬我错了


by konglk @ 2019-10-11 21:03:12

orz orz


上一页 |