Sponsors

Codeforces Round #410 (Div. 2), problem: (E) Mike and code of a permutation Solution In C/C++

#pragma warning(disable:4996)

#include <stdio.h>
#include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <cmath>
#include <map>
#include <set>
#include <queue>
#include <complex>
#include <iterator>
#include <random>
#include <time.h>
#include <tuple>
#include <functional>
#include <list>
#include <limits.h>
#define mp make_pair
#define ni(x) scanf(“%d”, &(x))
#define nii(x,y) scanf(“%d%d”,&(x),&(y))
#define mul(x,y) ((ll)(x)*(y)%mod)
#define mtp make_tuple
#define F(i,n) for(int i = 0; i < (n); i++)
#define FF(i,n) for(int i = 1; i <= (n); i++)
#define FE(i,n) for(int i = 0; i <= (n); i++)

using namespace std;
typedef pair<int, int> pii;
typedef long long ll;
const int mod = 1000000007;
const int inf = 2012345678;
const ll infl = 9012345678901234567;
const double pi = 3.1415926535897932384626433832795;
//—————————————————————————-//

const int N = 500010;
set<pii> tr[N];
int a[N], chk[N], res[N], clk, b[N];

void upd(pii x, int i, int n) {
while (i <= n) {
tr[i].insert(x);
i += i&(-i);
}
}
int n;
void dfs(int u) {
chk[u] = 1;
if (a[u] != inf) if(!chk[a[u]])dfs(a[u]);

int x = b[u];
x–;
if (b[u] == -1) x = n;
while (x) {
while (1) {
auto it = tr[x].lower_bound(pii(u, 0));
if (it != tr[x].end()) {
int v = it->second;
tr[x].erase(it);
if(!chk[v])dfs(v);
}
else break;
}
x -= x&(-x);
}

res[u] = clk++;
}

int main() {
#ifndef __GNUG__
freopen(“input.txt”, “r”, stdin);
#endif
ni(n);
FF(i, n)ni(a[i]);
FF(i, n)b[i] = -1;
FF(i, n) {
if (a[i] == -1) a[i] = inf;
else b[a[i]] = i;
upd(pii(a[i], i), i, n);
}
FF(i, n)if (!chk[i])dfs(i);
FF(i, n)printf(“%d “, n – res[i]); puts(“”);
return 0;
}

Google previews upcoming Android...

Google Unveils Android XR Smart Glasses Powered by Gemini...

Who has the most...

The Cash Kings: Who Holds the World's Most Liquid...

Los Angeles partners kick...

Los Angeles Kicks Off Major Initiative to 'Grow the...

Trump signs off on...

Trump Initiates Nationwide Review of Pediatric Vaccine Schedule Following...

Supreme Court Puts Trump’s...

Supreme Court Puts Trump-Backed Texas Power Grab Back on...

Snowflake’s stock slips on...

Snowflake Stock Plummets Despite Q3 Earnings Beat on Slowing...

Google previews upcoming Android XR smart glasses equipped with Gemini

Google Unveils Android XR Smart Glasses Powered by Gemini AI Google LLC is pulling back the curtain on its long-awaited foray into the next generation...

Who has the most liquid cash?

The Cash Kings: Who Holds the World's Most Liquid Assets? In the high-stakes world of global finance, the question of "Who has the most liquid...

Los Angeles partners kick off “Grow the Game of Soccer” free clinic series aimed at empowering youth sports

Los Angeles Kicks Off Major Initiative to 'Grow the Game of Soccer' Ahead of World Cup 2026 The spirit of the 2026 FIFA World Cup...

Trump signs off on nationwide vaccine schedule review as CDC withdraws infant Hep B guidance: ‘Fast track’

Trump Initiates Nationwide Review of Pediatric Vaccine Schedule Following CDC Guidance Withdrawal In a significant move that thrusts public health policy back into the national...

Supreme Court Puts Trump’s Midterm Power Grab Back on Track

Supreme Court Puts Trump-Backed Texas Power Grab Back on Track The political landscape ahead of the 2022 midterm elections just shifted dramatically, thanks to a...

Snowflake’s stock slips on slowing sales

Snowflake Stock Plummets Despite Q3 Earnings Beat on Slowing Sales Concerns In a bizarre, yet increasingly common twist of the tech market, data management giant...

Women remember when they brought peace to Abriba community

The Day Abiriba Women Marched for Peace: A 1992 Triumph Remembered The history of community resilience often rests on the shoulders of the most unexpected...

(For Southeast Asia) Step into a new adventure with the upcoming DualSense Wireless Controller – Genshin Impact Limited Edition

Step Into Teyvat: PlayStation Announces Genshin Impact Limited Edition DualSense Controller For fans eagerly exploring the vast world of Teyvat, the collaboration between HoYoverse and...

Why phone batteries fade after two years

The Truth About Smartphone Battery Degradation: Why Your Phone Slows Down After Two Years It's a frustrating but universal experience: after roughly two years of...