qiutiao
查看原帖
qiutiao
246331
mystic_qwq楼主2024/10/27 19:49
#include<bits/stdc++.h>
using namespace std;
#define int long long
using PII=pair<int,int>;
#define File(str) \
    freopen(str".in","r",stdin);\
    freopen(str".out","w",stdout)
#define rep(i,x,y) for(int i=x;i<=y;++i)
#define irep(i,x,y) for(int i=x;i>=y;--i)
#define Set(x,y) memset(x,y,sizeof(x))
#define fi first
#define se second
#define pb push_back
#define PQ priority_queue
#define TLC (double)clock()/CLOCKS_PER_SEC
//#define add(u,v) G[u].pb(v)
#define W while
#define Dbg(fmt,...) \
    fprintf(stderr,"%s %d %s: " fmt "\n",\
    __FILE__,__LINE__,__FUNCTION__,##__VA_ARGS__)
const int N=2e5+10,P=1e9+7;
// there're y nums greater than x, before x, then x will get rid in round y+1
// b[1...n]: how many nums before index i is greater than num at index i(online)
// a[x]<a[x+1] -> initial dif +1,b[x+1]+1, in BIT modify before b[x+1]+1 all del 1
// a[x]>a[x+1] -> initial dif-1,b[x]-1, in BIT modify after b[x]-1 all +1
int n,m,a[N],b[N],c[N],ans,tmp;
struct BIT{
    int t[N];
    void add(int p,int v){
        W(p)t[p]+=v,p&=p-1;
    }
    void add(int l,int r,int v){add(r,v),add(l-1,-v);}
    int ask(int p){
        int v=0;
        W(p<=n)v+=t[p],p+=p&-p;
        return v;
    }
    int ask(int l,int r){return ask(l)-ask(r+1);}
    void clear(){
        Set(t,0);
    }
}Bit;
main(){
    //File("");
    //cin.tie(0)->sync_with_stdio(0);
    cin>>n>>m;
    rep(i,1,n)cin>>a[i];
    rep(i,1,n){
        b[i]=Bit.ask(a[i]+1);
        Dbg("b_i:%d",b[i]);
        ans+=b[i],++c[b[i]];
        Bit.add(a[i],1);
    }
    Bit.clear();
    Bit.add(1,n,ans);
    Dbg("ans:%d",ans);
    rep(i,1,n){
        Dbg("c_{i-1}:%d",c[i-1]);
        tmp+=c[i-1];
        if(tmp==n)break;
        Bit.add(1+i,n,tmp-n);
    }
    rep(i,1,m){
        int op,x;
        cin>>op>>x;
        if(op==1){
            swap(a[x],a[x+1]);
            swap(b[x],b[x+1]);
            if(a[x]>a[x+1])
                Bit.add(1+b[x+1],1),++b[x+1];
            else
                --b[x],Bit.add(1+b[x],-1);
        }
        else
            cout<<Bit.ask(1,1+min(x,n-1))<<'\n';
    }
}
/*
3 6
1 2 3
2 0
1 1
1 2
2 0
2 1
2 2
*/

2024/10/27 19:49
加载中...