【牛牛的猜数游戏】前缀和的置换性

https://ac.nowcoder.com/acm/contest/7605/B

题解:https://ac.nowcoder.com/discuss/542124?type=101&order=0&pos=2&page=1&source_id=discuss_tag_nctrack&channel=-1)

个人实现:

#pragma GCC optimize(2)
#include<bits/stdc++.h>
#define ll long long
#define INF 0x3f3f3f3f
#define rep(i, a, b) for(int i=a;i<=b;i++)
#define per(i, a, b) for(int i=a;i>=b;i--)
using namespace std;
const int maxn=1e5+5;
const ll mod=1e9+7;
inline int read(){ int f = 1; int x = 0;char ch = getchar();while(ch>'9'||ch<'0') {if(ch=='-') f=-1; ch = getchar();}while(ch>='0'&&ch<='9') x = (x<<3) + (x<<1) + ch - '0',  ch = getchar();return x*f; }
inline ll qpow(ll a,ll b,ll MOD=mod){ll res=1;a%=MOD;while(b>0){if(b&1)res=res*a%MOD;a=a*a%MOD;b>>=1;}return res;}
struct balllist{ int pos[10];}pre[maxn];

balllist solve(int r,int l)//先把数组初始化为 1 ~ (l-1) 的逆操作后的结果,再做一次 1 ~ r 的操作就行了
{
    balllist tmp,res;
    rep(i,0,9) tmp.pos[pre[l].pos[i]]=i;//求出可以消除前l-1(非函数里的l,是传参的l)次影响的序列,其实是在做逆操作。 得到的序列经过l-1次操作得到的序列就是0到i,换句话说,我要是改成 rep(i,0,9) tmp.pos[pre[l].pos[i]]=pre[l].pos[i],操作后得到的就是原序列,等于号后面的决定了最终状态
    //或者我们可以换个角度来理解,我们把现在pre[l]的每个数字当作是有序的,不要管他本身数字是什么,就假设它是0-9排列的数字,那我们对其进行逆操作之后,再正操作,得到的就是0-9的有序序列

    rep(i,0,9) res.pos[i]=tmp.pos[pre[r].pos[i]];//在l-r上操作的影响等同于在tmp序列上做一次1-r的操作,代码实现原因同上

    return res;
}

int main()
{
    int n=read(),m=read();
    rep(i,0,9) pre->pos[i]=i;
    rep(i,1,n)
    {
        int l=read(),r=read();
        pre[i]=pre[i-1];
        swap(pre[i].pos[l],pre[i].pos[r]);
    }
    rep(i,1,m)
    {
        int l=read(),r=read();
        balllist ans=solve(r,l-1);
        rep(i,0,9) printf("%d%c",ans.pos[i],i==9?'\n':' ');
    }
}
全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务