Problem: 2049C - MEX Cycle
Language: C++20 (GCC 13-64)

Previous submission (verdict: Wrong answer on test 2):
// ॐ नमः शिवाय



#include<bits/stdc++.h>

using namespace std;

#define yes cout<<"YES"<<endl

#define no cout<<"NO"<<endl

#define inf 1e18

#define all(x) x.begin(), x.end()

#define rall(x) x.rbegin(), x.rend()

#define ff first

#define ss second

#define pb push_back

#define set_bits(x) __builtin_popcountll(x)

#define nline "\n"

#define PI 3.141592653589793238462

#define fastIO() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);



// #include <ext/pb_ds/assoc_container.hpp> 

// #include <ext/pb_ds/tree_policy.hpp> 

// using namespace __gnu_pbds; 

// #define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update> 



int delrow[4] = {-1, 0, 1, 0};

int delcol[4] = {0, -1, 0, 1};



const int mod = 1e9 + 7;



typedef long long ll;

typedef unsigned long long ull;

typedef long double lld;



void __print(int x) {cerr << x;}

void __print(long x) {cerr << x;}

void __print(long long x) {cerr << x;}

void __print(unsigned x) {cerr << x;}

void __print(unsigned long x) {cerr << x;}

void __print(unsigned long long x) {cerr << x;}

void __print(float x) {cerr << x;}

void __print(double x) {cerr << x;}

void __print(long double x) {cerr << x;}

void __print(char x) {cerr << '\'' << x << '\'';}

void __print(const char *x) {cerr << '\"' << x << '\"';}

void __print(const string &x) {cerr << '\"' << x << '\"';}

void __print(bool x) {cerr << (x ? "true" : "false");}



template<typename T, typename V>

void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}

template<typename T>

void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}

void _print() {cerr << "]\n";}

template <typename T, typename... V>

void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}

#ifndef ONLINE_JUDGE

#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)

#else

#define debug(x...)

#endif



void getPrime(ll n, map<ll,ll>&mp) {

    bool is_prime[n + 1];

    memset(is_prime, true, sizeof is_prime);

    is_prime[0] = is_prime[1] = false;

    for (ll p = 2; p * p <= n; p++) {

        if (is_prime[p]) {

            for (ll i = 2 * p; i<= n; i += p) {

                is_prime[i] = false;

            }

        }

    }

    for (ll i = 2; i <= n; i++) {

        if (is_prime[i]) {

            mp[i]++;

        }

    }

}

 

void getPrimeFactors(ll n, map<ll,ll>&mp) {

    mp[1]++;

    for(ll i = 2; i <= sqrt(n); i++) {

        while(n%i==0) {

            mp[i]++;

            n/=i;

        }

    }

    if(n<=1)return;

    mp[n]++;

}



long long nCr(int n, int r){

    long long p = 1, k = 1;

    if (n - r < r) r = n - r;

    if(r == 0) return 1;

    while (r) {

        p *= n; k *= r;

        long long m = __gcd(p, k);

        p /= m; k /= m;

        n--; r--;

    }

    return p;

}



ll power(ll x, ll y, ll MOD){

    if (y==0) return 1;

    ll ret = power(x, y/2, MOD);

    ret *= ret;ret%=MOD;

    if (y&1) ret *= x;

    return ret%MOD;

}

 

ll modinv(ll x, ll MOD = mod) {

    return power(x, MOD-2, MOD);

}



template<typename T> // cin >> vector<T>

istream& operator>>(istream &istream, vector<T> &v) {

  for (auto &it : v)

    cin >> it;

  return istream;

} 

 

template<typename T> // cout << vector<T>

ostream& operator<<(ostream &ostream, const vector<T> &c) { for (auto &it : c) cout << it << " "; return ostream; }



// -------------------------------------------------------------------------------------------------------------------------------->



int findMex(set<ll> &st, vector<ll> &ans){

    set<ll> fin;

    for(auto it: st) fin.insert(ans[it-1]);

    // debug(st);

    // debug(fin);

    int start = 0;

    for(auto it: fin){

        if(it == start) start++;

        else break;

    }

    return start;

}





void solve(){

    ll n, x, y;

    cin >> n >> x >> y;



    vector<ll> ans(n);

    

    for(int i=1; i<=n; i++){

        set<ll> st;

        if(i == 1) st.insert(n);

        else st.insert(i-1);

        if(i == n) st.insert(1);

        else st.insert(i+1);

        if(i == x) st.insert(y);

        else if(i == y) st.insert(x);

        ans[i-1] = findMex(st, ans);

    }

    // cout << ans << endl;



    for(int i=1; i<=n; i++){

        set<ll> st;

        if(i == 1) st.insert(n);

        else st.insert(i-1);

        if(i == n) st.insert(1);

        else st.insert(i+1);

        if(i == x) st.insert(y);

        else if(i == y) st.insert(x);

        ans[i-1] = findMex(st, ans);

    }

    // cout << ans << endl;



    for(int i=1; i<=n; i++){

        set<ll> st;

        if(i == 1) st.insert(n);

        else st.insert(i-1);

        if(i == n) st.insert(1);

        else st.insert(i+1);

        if(i == x) st.insert(y);

        else if(i == y) st.insert(x);

        ans[i-1] = findMex(st, ans);

    }

    cout << ans << endl;



}



int main() {

    fastIO();



#ifndef ONLINE_JUDGE

    freopen("input.txt", "r", stdin);

    freopen("output.txt", "w", stdout);

    freopen("debug.txt", "w", stderr);

#endif



    int t=1;

    cin>>t;

    while(t--){

        solve();

    }

}

Submission (verdict: Accepted):
// ॐ नमः शिवाय



#include<bits/stdc++.h>

using namespace std;

#define yes cout<<"YES"<<endl

#define no cout<<"NO"<<endl

#define inf 1e18

#define all(x) x.begin(), x.end()

#define rall(x) x.rbegin(), x.rend()

#define ff first

#define ss second

#define pb push_back

#define set_bits(x) __builtin_popcountll(x)

#define nline "\n"

#define PI 3.141592653589793238462

#define fastIO() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);



// #include <ext/pb_ds/assoc_container.hpp> 

// #include <ext/pb_ds/tree_policy.hpp> 

// using namespace __gnu_pbds; 

// #define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update> 



int delrow[4] = {-1, 0, 1, 0};

int delcol[4] = {0, -1, 0, 1};



const int mod = 1e9 + 7;



typedef long long ll;

typedef unsigned long long ull;

typedef long double lld;



void __print(int x) {cerr << x;}

void __print(long x) {cerr << x;}

void __print(long long x) {cerr << x;}

void __print(unsigned x) {cerr << x;}

void __print(unsigned long x) {cerr << x;}

void __print(unsigned long long x) {cerr << x;}

void __print(float x) {cerr << x;}

void __print(double x) {cerr << x;}

void __print(long double x) {cerr << x;}

void __print(char x) {cerr << '\'' << x << '\'';}

void __print(const char *x) {cerr << '\"' << x << '\"';}

void __print(const string &x) {cerr << '\"' << x << '\"';}

void __print(bool x) {cerr << (x ? "true" : "false");}



template<typename T, typename V>

void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}

template<typename T>

void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}

void _print() {cerr << "]\n";}

template <typename T, typename... V>

void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}

#ifndef ONLINE_JUDGE

#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)

#else

#define debug(x...)

#endif



void getPrime(ll n, map<ll,ll>&mp) {

    bool is_prime[n + 1];

    memset(is_prime, true, sizeof is_prime);

    is_prime[0] = is_prime[1] = false;

    for (ll p = 2; p * p <= n; p++) {

        if (is_prime[p]) {

            for (ll i = 2 * p; i<= n; i += p) {

                is_prime[i] = false;

            }

        }

    }

    for (ll i = 2; i <= n; i++) {

        if (is_prime[i]) {

            mp[i]++;

        }

    }

}

 

void getPrimeFactors(ll n, map<ll,ll>&mp) {

    mp[1]++;

    for(ll i = 2; i <= sqrt(n); i++) {

        while(n%i==0) {

            mp[i]++;

            n/=i;

        }

    }

    if(n<=1)return;

    mp[n]++;

}



long long nCr(int n, int r){

    long long p = 1, k = 1;

    if (n - r < r) r = n - r;

    if(r == 0) return 1;

    while (r) {

        p *= n; k *= r;

        long long m = __gcd(p, k);

        p /= m; k /= m;

        n--; r--;

    }

    return p;

}



ll power(ll x, ll y, ll MOD){

    if (y==0) return 1;

    ll ret = power(x, y/2, MOD);

    ret *= ret;ret%=MOD;

    if (y&1) ret *= x;

    return ret%MOD;

}

 

ll modinv(ll x, ll MOD = mod) {

    return power(x, MOD-2, MOD);

}



template<typename T> // cin >> vector<T>

istream& operator>>(istream &istream, vector<T> &v) {

  for (auto &it : v)

    cin >> it;

  return istream;

} 

 

template<typename T> // cout << vector<T>

ostream& operator<<(ostream &ostream, const vector<T> &c) { for (auto &it : c) cout << it << " "; return ostream; }



// -------------------------------------------------------------------------------------------------------------------------------->



int findMex(set<ll> &st, vector<ll> &ans){

    set<ll> fin;

    for(auto it: st) fin.insert(ans[it-1]);

    int start = 0;

    for(auto it: fin){

        if(it == start) start++;

        else break;

    }

    return start;

}





void solve(){

    ll n, x, y;

    cin >> n >> x >> y;



    vector<ll> ans(n);

    

    for(int i=1; i<=n; i++){

        set<ll> st;

        if(i == 1) st.insert(n);

        else st.insert(i-1);

        if(i == n) st.insert(1);

        else st.insert(i+1);

        if(i == x) st.insert(y);

        else if(i == y) st.insert(x);

        ans[i-1] = findMex(st, ans);

    }

    // cout << ans << endl;



    for(int i=1; i<=n; i++){

        set<ll> st;

        if(i == 1) st.insert(n);

        else st.insert(i-1);

        if(i == n) st.insert(1);

        else st.insert(i+1);

        if(i == x) st.insert(y);

        else if(i == y) st.insert(x);

        ans[i-1] = findMex(st, ans);

    }

    // cout << ans << endl;



    for(int i=1; i<=n; i++){

        set<ll> st;

        if(i == 1) st.insert(n);

        else st.insert(i-1);

        if(i == n) st.insert(1);

        else st.insert(i+1);

        if(i == x) st.insert(y);

        else if(i == y) st.insert(x);

        ans[i-1] = findMex(st, ans);

    }

    cout << ans << endl;



}



int main() {

    fastIO();



#ifndef ONLINE_JUDGE

    freopen("input.txt", "r", stdin);

    freopen("output.txt", "w", stdout);

    freopen("debug.txt", "w", stderr);

#endif



    int t=1;

    cin>>t;

    while(t--){

        solve();

    }

}