Collecting Numbers II

原题链接

题解

如果一个 k ,其前面没有出现过 k1 ,那么回合数+1,我们令这样的数叫做断点

因此交换两个数 l,r 不会影响 [1,l1],[r+1,n] 内的断点

code

#include<bits/stdc++.h>
#define ll long long
#define lowbit(x) ((x)&(-x))
using namespace std;
const ll inf=1e18;
const ll mod=1e9+7;

ll a[200005];
ll pos[200005];

void solve()
{
    ll n,m;
    cin>>n>>m;

    for(ll i=1;i<=n;i++)
    {
        ll x;
        cin>>x;
        pos[x]=i;
        a[i]=x;
    }

    ll ans=1;
    for(ll i=2;i<=n;i++) ans+=pos[i-1]>pos[i];

    while(m--)
    {
        ll x,y;
        cin>>x>>y;
        if(x>y) swap(x,y);
        ll vx=a[x],vy=a[y];
        if(vx>1&&pos[vx-1]>x&&pos[vx-1]<y) ans--;
        if(vx<n&&pos[vx+1]>x&&pos[vx+1]<y) ans++;
        if(vy>1&&pos[vy-1]>x&&pos[vy-1]<y) ans++;
        if(vy<n&&pos[vy+1]>x&&pos[vy+1]<y) ans--;
        if(abs(vx-vy)==1)
        {
            if(vx>vy) ans--;
            else ans++;
        }
        swap(a[x],a[y]);
        swap(pos[vx],pos[vy]);

        cout<<ans<<'\n';
    }
}
int main()
{
    ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    int TT=1;
    //cin>>TT;
    while(TT--) solve();
    return 0;
}

posted @   纯粹的  阅读(20)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~
点击右上角即可分享
微信分享提示