Problem: 2049C - MEX Cycle
Language: c++17 

Solution 1 (runtime: 139 ms):
#pragma GCC optimize("Ofast")

 

#include <bits/stdc++.h>

#include <ext/pb_ds/assoc_container.hpp>

#include <ext/pb_ds/assoc_container.hpp>

#include <chrono>

#define pb push_back

#define INF 0x3f3f3f3f

#define LINF 0x3f3f3f3f3f3f3f3f

#define endl '\n'

#define ll long long

#define f first

#define fin cin

#define fout cout

#define s second

#define FAST cin.tie(0), cout.tie(0), ios::sync_with_stdio(0)

#define debug(x) cout << "DEBUG " << x << endl

#define debug2(x, y) cout << "DEBUG " << x << " " << y << endl

#define debug3(x, y, z) cout << "DEBUG " << x << " " << y << " " << z<< endl

#define debug4(x, y, z, o) cout << "DEBUG " << x << " " << y << " " << z<< " " << o << endl

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

#define left esquerda

#define lb lower_bound

#define right direita

using namespace std;

using namespace __gnu_pbds;

 

void setIO(string s) {

ios_base::sync_with_stdio(0); cin.tie(0);

freopen((s+".in").c_str(),"r",stdin);

freopen((s+".out").c_str( ),"w",stdout);

}

typedef pair<int, ll> pii;

typedef vector<vector<char>> mat;

typedef pair<int, string> pis;

const ll mod = 1e9 + 7, MAXN = 2e5 + 5;

typedef vector<int> vi;

typedef pair<int, pair<int, int>> piii;

const int RANDOM = chrono::high_resolution_clock::now().time_since_epoch().count();

struct chash {

    int operator()(int x) const { return x ^ RANDOM; }

};



 

void solve() {

    int n, x, y;

    cin >> n >> x >> y;



    x--, y--;



    vector<int> v(n);



    priority_queue<pii, vector<pii>, greater<pii>> fila;

    for(int i = 0; i < n; i++)

        fila.push({0, i});

    

    while(!fila.empty()) {

        pii aux = fila.top();

        fila.pop();



        if(v[aux.s] != aux.f)

            continue; 

        

        int viz = (aux.s ? aux.s - 1 : n - 1);

        int viz2 = (aux.s + 1 < n ? aux.s + 1 : 0);

        

        vector<int> vl = {viz, viz2};

        if(aux.s == x)

            vl.pb(y);

        if(aux.s == y)

            vl.pb(x);



        for(int &i : vl) 

            if(aux.f == v[i]) {

                fila.push({aux.f + 1, i});

                v[i] = aux.f + 1;

            }

        

    

    

    }

    

    for(int &i : v)

        cout << i << " ";

    cout << endl;

}



 

int32_t main() {

    int ct = 1;

    cin >> ct;

    while(ct--)

        solve();

}

Solution 2 (runtime: 77 ms):
// ﷽

#include <bits/stdc++.h>

//#pragma GCC optimize ("03")

using namespace std;

 

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

#define sz(v)           ((int)((v).size()))

#define clr(v, d)        memset(v,d, sizeof(v))

#define rep(i, v)        for(int i = 0; i < sz(v); i++)

#define lp(i, n)         for(int i = 0; i < (int)n; i++)

#define lp1(i, n)         for(int i = 1; i <= (int)n; i++)

#define lpi(i, j, n)     for(int i = (j); i < (int)(n); i++)

#define lpd(i, j, n)     for(int i = (j); i >= (int)(n); i--)

#define pb               push_back

#define MP               make_pair

#define X real()

#define Y imag()

#define sqr(x) ((x)*(x))

#define LETS_GO ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)

 

// You can rewrite any of following as functions

#define angle(a)                (atan2((a).imag(), (a).real()))

#define vec(a,b)                ((b)-(a))

 

#define length(a)               (hypot((a).imag(), (a).real()))

#define normalize(a)            (a)/length(a)

 

// dop = a*b cos(T), if zero -> prep

#define dop(a,b)                 ((conj(a)*(b)).real())

// cp = a*b sin(T), if zero -> parllel

#define cp(a,b)         ((conj(a)*(b)).imag())

#define same(p1,p2)     (dop(vec(p1,p2),vec(p1,p2)) < EPS)

#define lengthSqr(p)    dop(p,p)

#define rotateO(p,ang)          ((p)*exp(point(0,ang)))

#define rotateA(p,ang,about)    (rotateO(vec(about,p),ang)+about)

#define reflect(v,m)            (conj((v)/(m))*(m))

 

#define error(args...) { vector<string> _v = split(#args, ','); \

err(_v.begin(), args); }

 

 

typedef long long   ll;

typedef vector<int> vi;

typedef long double ld;

typedef vector<double> vd;

typedef vector<vi> vvi;

typedef vector<vd> vvd;

typedef vector<string> vs;

typedef complex <double> point;

typedef pair<int,int> pi;

 

 

const int OO = (int)2e9;

const double PI = acos(-1.0);

const double EULER = 2.71828182845904523536028747135266250;

const double EPS = (1e-12);

int dcmp(long double x, long double y) { return fabs(x-y) <= EPS ? 0 : x < y ? -1 : 1;}

 

struct Point {

    double i, j;

 

    Point(const double i, const double j) : i(i) , j(j) {}

 

};

 

double distSq(double x1 , double y1, double x2, double y2)

{

    return (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2);

}

 

double dist(double x1 , double y1, double x2, double y2)

{

    return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));

}

 

bool isSquare(int x1 , int y1, int x2, int y2, int x3, int y3, int x4, int y4)

{

    int d2 = distSq(x1, y1,x2,y2); // from p1 to p2

    int d3 = distSq(x1, y1,x3,y3); // from p1 to p3

    int d4 = distSq(x1, y1,x4,y4); // from p1 to p4

 

    if (d2 == 0 || d3 == 0 || d4 == 0)

        return false;

 

    if (d2 == d3 && 2 * d2 == d4

        && 2 * distSq(x2, y2,x4,y4) == distSq(x2, y2,x3,y3)) {

        return true;

    }

 

    if (d3 == d4 && 2 * d3 == d2

        && 2 * distSq(x3, y3,x2,y2) == distSq(x3, y3,x4,y4)) {

        return true;

    }

    if (d2 == d4 && 2 * d2 == d3

        && 2 * distSq(x2, y2,x3,y3) == distSq(x2, y2,x4,y4)) {

        return true;

    }

 

    return false;

}

 

double fixAngle(double A){

    return A > 1 ? 1 : (A < -1 ? -1 : A);

}

 

 

double getAngle_A_abc(double a, double b, double c){

    return acos( fixAngle( (b*b+c*c-a*a)/(2*b*c)) );

}

 

void merge_sort(vector<int> &v) {

    int n = (int)v.size();

    if (n == 1)

        return;

 

    vector<int> a, b;

    for (int i = 0; i < n / 2; i++) {

        a.push_back(v[i]);

    }

    for (int i = n / 2; i < n; i++) {

        b.push_back(v[i]);

    }

    merge_sort(a);

    merge_sort(b);

 

    v.clear();

 

    // merging a and b into v

    int i = 0, j = 0;

    while (i < a.size() && j < b.size()) {

        if (a[i] < b[j]) {

            v.push_back(a[i]);

            i++;

        }

        else {

            v.push_back(b[j]);

            j++;

        }

    }

    if (i != a.size()) {

        for (; i < a.size(); i++) {

            v.push_back(a[i]);

        }

    }

    if (j != b.size()) {

        for (; j < b.size(); j++) {

            v.push_back(b[j]);

        }

    }

}

vector<int> nextGreaterElement(vector<int>& nums1, vector<int>& nums2) {

    stack<int>stack;

    map<int,int>map;

    vector<int>ans;

    for(int i=0;i<nums2.size();i++){

        while(!stack.empty()){

            if(stack.top()<nums2[i]){

                map.insert(pair<int,int>(stack.top(),nums2[i]));

                stack.pop();

            }else break;

        }

        stack.push(nums2[i]);

    }

 

    for(int i=0;i<nums1.size();i++){

        int val = map[nums1[i]];

        if(val==0)ans.push_back(-1);

        else ans.push_back(val);

    }

    return ans;

}

 

bool is_prime(int n) {

    if (n == 2) return true;

    if (n < 2 || n % 2 == 0) return false;

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

        if(n%i == 0)

            return false;

    }

    return true;

}

 

 

 

bool cmp(const pair<pair<ll,ll>,ll> &a, const pair<pair<ll,ll>,ll> &b)

{

    if(a.first.second != b.first.second)

        return a.first.second < b.first.second;

    return a.first.first < b.first.first;

}



 

double toRadian(double degree){

    return (degree*PI/180.0);

}

 

template<typename T>

T gcd(T a , T b){

    return (b == 0) ? a : gcd(b,a%b);

}

 

template<typename T>

T lcm(T a, T b){

    return a * b / gcd(a,b);

}

 

string DecimalToBinaryString(int a)

{

    string binary = "";

    int mask = 1;

    for(int i = 0; i < 31; i++)

    {

        if((mask&a) >= 1)

            binary = "1"+binary;

        else

            binary = "0"+binary;

        mask<<=1;

    }

    return binary;

}

 

 



struct edge{

    int from , to , w;

    edge(int from, int to, int w) : from(from), to(to), w(w) {}

 

    bool operator < (const edge &e) const{      // STL priority_queue need it > , or modify data -ve, or use parameter less

        return w > e.w;

    }

};

 

 

struct UnionFind{

    vi rank , parent;

    int forests;        // Number of current forests

    UnionFind(int n){

        parent = vi(n); rank = vi(n);

        lp(i,n){ parent[i] = i ; rank[i] = 1;}

    }

 

    int findSet(int x){

        // while(x != parent[x]) x = parent[x];

        if(parent[x] == x) return x;

        return parent[x] = findSet(parent[x]);

    }

    void link(int x, int y){

        if(rank[x] > rank[y]) swap(x,y);

        parent[x] = y;

        if(rank[x] == rank[y]) rank[y] ++;

    }

    bool unionSets(int x, int y){

        x = findSet(x); y = findSet(y);

        if(x != y){

            link(x,y);

            forests --;     // After merging two forests

        }

        return x != y;

    }

 

 

 

 

    ////////////////////// Other Utilities //////////////////////////////

    bool sameSet(int x, int y){

        return findSet(x) == findSet(y);

    }

 

    vector<vi> connectedComponents(){

        vector<vi> list(sz(parent));

        lp(i,sz(parent))    list[findSet(i)].pb(i);

        return list;

    }

};

 

 

 

 

pair<int, vector<edge> > MSTKruskal(vector<edge> edgeList, int n){

    UnionFind uf(n);

    vector<edge> edges;

    int mstCost = 0;

    priority_queue<edge> q;

    rep(i,edgeList)     q.push(edgeList[i]);

    while(!q.empty()){

        edge e = q.top(); q.pop();

        if(uf.unionSets(e.from,e.to)){

            edges.pb(e);

            mstCost += e.w;

        }

    }

    if(sz(edges) != (n-1))      return MP(-OO, vector<edge>());

    return MP(mstCost,edges);

}

 

 

ll factorial(ll n, ll mod, bool take_mod){

    ll fact = 1;

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

        if(take_mod)

            fact = (fact * i) % mod;

        else

            fact = fact * i;

    return fact;

}

 

 

template<typename T>

T power(T a, T b, T m){

    a %= m;

    T res = 1;

    while (b > 0) {

        if (b & 1)

            res = res * a % m;

        a = a * a % m;

        b >>= 1;

    }

    return res;

}

 

 

template<typename T>

T binpow(T a, T b) {

    T res = 1;

    while (b > 0) {

        if (b & 1)

            res = res * a;

        a = a * a;

        b >>= 1;

    }

    return res;

}

 

 

void precompute_fact_inv(vector<ll> &fact, vector<ll> &inv, ll mod){

    const int N = sz(fact);

    fact[0] = inv[0] = 1;

    for(ll i = 1; i <= N; i++){

        fact[i] = fact[i-1] * i % mod;

        inv[i] = power(fact[i], mod-2, mod);

    }

}

 

ll nCk(ll n, ll k, vector<ll> &fact, vector<ll> &inv, ll mod){

    if(k > n) return 0;

    return fact[n] * inv[k] % mod * inv[n-k] % mod;

}

 

 

ll nCk2(ll n, ll k){

    ll z = 1;

    for(ll i = 0; i < k; i++)

        z = z * (n-i) / (i+1);

    return z;

}

 

 

ll cubr(ll x){

    ll l = 0, r = OO;

    while(l != r){

        ll m = (l + r + 1) / 2;

        if(m * m * m > x) r = m - 1;

        else l = m;

    }

    return l;

}

 

template<typename T>

vector<T> generate_divisors(T n){         //  O( sqrt(n) )

    vector<T> divisors;

    T i;

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

        if(n % i == 0){

            divisors.push_back(i);

            divisors.push_back(n/i);

        }

    }

    if(i * i == n)

        divisors.push_back(i);

    return divisors;            // Not sorted

}

 

 

ll perfectSquareSum(ll n){

    return (n*(n+1) * (2*n+1)) / 6;

}

 

ll summation(ll n){

    return n*(n+1) / 2;

}

 

 

int ccw(point p0, point p1, point p2){

    point v1(p1-p0), v2(p2-p0);

    if(cp(v1,v2) > +EPS) return +1;

    if(cp(v1,v2) < -EPS) return -1;

    if(v1.X*v2.X < -EPS || v1.Y*v2.X < -EPS) return -1;

    if(norm(v1) < norm(v2) - EPS) return +1;

    return 0;

}

 

bool intersect(point p1, point p2, point p3, point p4){

    bool x = (p1 == p2), y = (p3 == p4);

    if(x & y) return (p1 == p3);

    if(x) return ccw(p3,p4,p1) == 0;

    if(y) return ccw(p1,p2,p3) == 0;

    return ccw(p1,p2,p3) * ccw(p1,p2,p4) <= 0 && ccw(p3,p4,p1) * ccw(p3,p4,p2) <= 0;

}

 

bool intersectSegments(point a, point b, point c, point d, point &intersect){

    double d1 = cp(a - b, d - c), d2 = cp(a - c, d - c), d3 = cp(a - b, a - c);

    if(fabs(d1) < EPS) return false;   // Parallel || Identical

    double t1 = d2 / d1, t2 = d3 / d1;

    intersect = a + (b-a)*t1;

    if(t1 < -EPS || t2 < -EPS || t2 > 1 + EPS) return false; // e.g ab is ray cd is segment... change to whatever

    return true;

}

 

 

pair<double, point> findCircle(point a, point b, point c){

    // create median, vector, its prependicular

    point m1 = (b+a)*0.5, v1 = (b-a), pv1 = point(v1.Y, -v1.X);

    point m2 = (b+c)*0.5, v2 = (b-c), pv2 = point(v2.Y, -v2.X);

    point end1 = m1 + pv1, end2 = m2 + pv2, center;

    intersectSegments(m1,end1,m2,end2,center);

    return MP(length(a-center), center);

}

 

vector<point> intersectCircleCircle(point c1, double r1, point c2, double r2){

    // Handle infinity case first : same center / radius and r > 0

    if(same(c1,c2) && dcmp(r1,r2) == 0 && dcmp(r1,0) > 0)

        return vector<point> (3,c1);    // infinity : 2 same circles (not points)

 

    // Compute 2 intersection case and handle 0, 1, 2 cases

    double ang1 = angle(c2 - c1), ang2 = getAngle_A_abc(r2,r1,length(c2-c1));

 

    if(::isnan(ang2))   // if r1 or d(length(c2-c1)) = 0 => nan in getAngle_A_abc(/0)

        ang2 = 0;       // fix corruption

 

    vector<point> v(1, polar(r1, ang1 + ang2) + c1);

 

    // if point NOT on the 2 circles = no intersection

    if(dcmp(dop(v[0]-c1,v[0]-c1), r1*r1) != 0 || dcmp(dop(v[0] - c2,v[0] - c2), r2*r2) != 0) return vector<point> ();

 

    v.pb(polar(r1, ang1 - ang2) + c1);

    if(same(v[0],v[1])) v.pop_back();     // if same then 1 intersection only

    return v;

}

 

 

bool cmpPoints(const point &a,

              const point &b)

{

    if(dcmp(a.X,b.X) == -1) return true;

    else if(dcmp(a.X,b.X) == 0){

        if(dcmp(a.Y,b.Y) == -1) return true;

        return false;

    }

    return false;

}

 

 

 

bool checkDivisibility(string &n, int k) {          // Slow : save in array to make it fast

    int rem = 0;

    for (auto i : n)

        rem = ((rem * 10) + i - '0') % k;

    return rem == 0;

}

 

 

vector<bool> countPrimesInRange_sieve(ll n){     // Backward thinking

    vector<bool> isPrime(n+1,true);      // set all of them to primes

    ll cnt = 0;

    isPrime[0] = isPrime[1] = 0;    // 0, 1 are not primes

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

        if(isPrime[i]){

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

                isPrime[j] = 0;

        }

    }

 

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

        if(isPrime[i])

            cnt ++;

    }

    return isPrime;

}

 

vector<string> split(const string& s, char c){

    vector<string> v;

    stringstream ss(s);

    string x;

    while(getline(ss, x, c))

        v.emplace_back(x);

    return move(v);

}

 

 

void err(vector<string>::iterator it){

 

}

 

 

template<typename T, typename ... Args>

void err(vector<string>::iterator it, T a, Args ... args){

    cerr << it->substr((*it)[0] == ' ', it->size()) << " = "<<a<<'\n';

    err(++it,args...);

}

 

 

 

ll tolong(char c){

    string letters = "0123456789ABCDEF";

    return letters.find(c);

}

 

int FromAnyBaseToDecimal(string in, ll base){

    int res = 0;

    for(int i = 0; i < (int)in.size(); i++){

        res *= base;

        res += tolong(in[i]);

    }

    return res;

}

 

 

string FromDecimalToAnyBase(ll num, ll base){

    string letters = "0123456789ABCDEF";

    if(num == 0)

        return "0";

    string res = "";

    for(; num; num /= base)

        res = letters[num % base] + res;

    return res;

}

 

 

ll countBits1(ll mask){     //    O(bits Count)        __builtin_popcount

    int ret = 0;

    while(mask){

        mask &= (mask-1);

        ret++;           // Simply remove the last bit and so on

    }

    return ret;

}

 

int get_first_bit(long long n){

    return 63 - __builtin_clzll(n);

}

 

 

 

const int bitscount = 32;

vector<vector<int>> prefix_count;

 

// Function to find the prefix sum

void findPrefixCount(vi arr, int n)

{

    // Loop for each bit

    for (int i = 0; i < bitscount; i++) {

        // Loop to find prefix count

        prefix_count[i][1] = ((arr[1] >> i) & 1);

        for (int j = 2; j <= n; j++) {

            prefix_count[i][j] = ((arr[j] >> i) & 1);

            prefix_count[i][j] += prefix_count[i][j - 1];

        }

    }

}

 

 

// Function to answer query

int rangeAnd(int l, int r)

{

    // To store the answer

    int ans = 0;

    // Loop for each bit

    for (int i = 0; i < bitscount; i++) {

        // To store the number of variables

        // with ith bit set

        int x;

        if (l == 0)

            x = prefix_count[i][r];

        else

            x = prefix_count[i][r]

                - prefix_count[i][l - 1];

        // Condition for ith bit

        // of answer to be set

        if (x == r - l + 1)

            ans = (ans | (1 << i));

    }

 

    return ans;

}

 

template<typename T>

vector<T> factorize(T n){    // max n is 1e12

    // O( sqrt(n) )

    vector<T> f;

    T i;

    for(i = 2; i * i <= n; i++){      //  Improve start by i = 3.

        while(n % i == 0){

            f.push_back(i);    // Get every prime inside n.    n  i^j    is a new number

            n /= i;

        }

    }

    /*

     Two ways to prove that n will be prime number if n  1

         1) Think in n  pi^x as a NEW sub-problem. If we went to its sqrt and % applied, then we are facing a prime number

         2) Prove that, for any n either all its primes before sqrt(n), or only the biggest prime after sqrt(n).

     */

    if(n > 1)

        f.push_back(n);

    return f;

}

 

int find_gcd(vi arr, int n) {

  int result = arr[0];

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

    result = gcd(arr[i], result);

 

    if(result == 1) {

        return 1;

    }

  }

  return result;

}

 

int countDigitsInFactorial(int n){      // O(1)

    if (n < 0) {

        return 0;

    }

    if (n <= 1) {

        return 1;

    }

    double x = (n * log10(n / EULER) + log10(2 * PI * n) / 2.0);

    return floor(x) + 1;

}

 

template<typename T>

T findProductSum(vector<T> A, T N, T Mod)

{

    T ans = 0;

    T sum = 0;

    for (T i = 0; i < N; i++) {

        ans += (sum * A[i]) % Mod;

        ans %= Mod;

        sum += A[i];

        sum %= Mod;

    }

    return ans;

}

 

// C function for extended Euclidean Algorithm (used to

// find modular inverse.

ll gcdExtended(ll a, ll b, ll *x, ll *y)

{

    // Base Case

    if (a == 0)

    {

        *x = 0; *y = 1;

        return b;

    }

 

    ll x1, y1; // To store results of recursive call

    ll gcd = gcdExtended(b%a, a, &x1, &y1);

 

    // Update x and y using results of recursive

    // call

    *x = y1 - (b/a) * x1;

    *y = x1;

 

    return gcd;

}

 

ll modInverse(ll b, ll m)

{

    ll x, y;

    ll g = gcdExtended(b, m, &x, &y);

 

    if (g != 1)

        return -1;

 

    return (x%m + m) % m;

}

 

ll modDivide(ll a, ll b, ll m)

{

    a = a % m;

    ll inv = modInverse(b, m);

    return (inv * a) % m;

}



int dijkstra(vector<vector<edge>> adjL, int src, int dest = -1){      // O(E log V)

    int n = (int)adjL.size();

    vector<int> prev(n,-1), dist(n,OO);

    dist[src] = 0;

    priority_queue<edge> q;

    q.push(edge(-1,src,0));

    while(!q.empty()){

        edge e = q.top();

        q.pop();

        if(e.w > dist[e.to]) continue;          // some other state reached better

        prev[e.to] = e.from;

        for(int j = 0; j < (int)adjL[e.to].size(); j++){

            edge ne = adjL[e.to][j];

            if(dist[ne.to] > dist[ne.from] + ne.w){

                ne.w = dist[ne.to] = dist[ne.from] + ne.w;

                q.push(ne);

            }

        }

    }

    return (dist[dest] == OO) ? -1 : dist[dest];

}







 

 

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

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

 

/*

 

 

 */















int main()

{

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

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

    LETS_GO;

 

    

    int tc = 1;

    cin>>tc;

    

    while(tc--){

        int n, x, y;

        cin>>n>>x>>y;

        vi a(n+1);

        if(n % 2 == 0){

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

                if(i % 2)

                    a[i] = 0;

                else

                    a[i] = 1;

            }

        }

        else{

            a[1] = 2;

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

                if(i % 2 == 0)

                    a[i] = 0;

                else

                    a[i] = 1;

            }

        }

        if(!(x == y-1 || (x == 1 && y == n))){

            if(n % 2 == 0){

                if(x % 2 == y % 2)

                    a[x] = 2;

            }

            else{

                if(x % 2 == y % 2 && x != 1){

                    if(x == 2)

                        swap(a[1],a[2]);

                    else

                        a[x] = 2;

                }

            }

        }

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

            cout << a[i] << " ";

        cout << "\n";

    }

    return 0;

}