【luogu1966】 【noip2013】火柴排队[逆序对 树状数组]

P1966 火柴排队

可以发现要想高度最小 我们就要使a的第k大和b的第k大对应 

然后我们就用逆序对来求 按照之前的模板打出来答案不对 然后我就福至心灵地倒着建 结果对了!

暂时没想通为什么 明天再来想QAQ

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
#define ll long long
#define rg register
const int N=100000+5,M=200000+5,inf=0x3f3f3f3f,P=99999997;
int n,sum=0,tree[N<<2],c[N];
template <class t>void rd(t &x){
    x=0;int w=0;char ch=0;
    while(!isdigit(ch)) w|=ch=='-',ch=getchar();
    while(isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
    x=w?-x:x;
}

#define Max(x,y) (x)>(y)?(x):(y)
#define Min(x,y) (x)>(y)?(y):(x)
int lowbit(int x){return x&(-x);}

void update(int pos,ll ad){
    while(pos<=n) tree[pos]+=ad,pos+=lowbit(pos);
}

struct node{int x,p;}a[N],b[N];
bool cmp(const node x,const node y){return x.x>y.x;}

int query(ll pos){
    int ans=0;
    while(pos>0) ans=(ans+tree[pos])%P,pos-=lowbit(pos);
    return ans;
}


int main(){
//    freopen("in.txt","r",stdin);
    rd(n);
    for(int i=1;i<=n;++i) rd(a[i].x),a[i].p=i;
    for(int i=1;i<=n;++i) rd(b[i].x),b[i].p=i;
    stable_sort(a+1,a+1+n,cmp);
    stable_sort(b+1,b+1+n,cmp);
    for(int i=1;i<=n;++i) c[a[i].p]=b[i].p;
    for(int i=n;i>0;--i)
    update(c[i],1),sum=(sum+query(c[i]-1))%P;
    printf("%d",sum);
    return 0;
}
 
原文地址:https://www.cnblogs.com/lxyyyy/p/11178669.html