线段树+动态开点,50 MLE on#11~20 求调

P1908 逆序对

toolong114514 @ 2023-08-25 15:50:32

#include<iostream>
using namespace std;
const int N=5e6+10;
struct node{
    int sum,lson,rson;
}tree[4*N];
int cnt,root;
inline void upd(int &pos,int dot,int c,int l,int r){
    if(pos==0) pos=++cnt;
    if(l<=dot&&dot<=r) tree[pos].sum+=c;
    if(dot<l||r<dot||l==r) return;
    int mid=(l+r)/2;
    upd(tree[pos].lson,dot,c,l,mid);
    upd(tree[pos].rson,dot,c,mid+1,r);
}
inline int ask(int &pos,int lft,int rgt,int l,int r){
    if(lft<=l&&r<=rgt) return tree[pos].sum;
    if(pos==0||rgt<l||r<lft) return 0;
    int mid=(l+r)/2;
    return ask(tree[pos].lson,lft,rgt,l,mid)+ask(tree[pos].rson,lft,rgt,mid+1,r);
}
int a[N];
int n;
long long ans;
int main(){
    ios::sync_with_stdio(false);
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>a[i];
        ans+=ask(root,a[i]+1,1e9,1,1e9);
        upd(root,a[i],1,1,1e9);
    }
    cout<<ans;
    return 0;
}

by FiraCode @ 2023-08-25 15:53:35

@toolong114514 建议写离散化


by Eleveslaine @ 2023-08-25 15:58:25

线段树跑 1e9 不 MLE/TLE 才怪了


|