This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#include "perm.h"
using namespace std;
const int N = 200;
int node = 0;
vector<int> g[N];
int a[N];
int sz[N];
void precalc(int v) {
sz[v] = 1;
for (int to : g[v]) {
precalc(to);
sz[v] += sz[to];
}
}
void add(int v, int x) {
a[v] += x;
for (int to : g[v])
add(to, x);
}
void dfs(int v) {
int x = (v > 0);
for (int to : g[v]) {
dfs(to);
add(to, x);
x += sz[to];
}
}
vector<int> b;
void construct(int v) {
sort(g[v].begin(), g[v].end(), [&](int x, int y) {
return a[x] > a[y];
});
if (v) b.push_back(a[v]);
for (int to : g[v])
construct(to);
}
vector<int> construct_permutation(long long k) {
if (k == 1)
return {};
{
g[0].clear();
while (node) {
g[node].clear();
a[node] = 0;
sz[node] = 0;
node--;
}
b.clear();
}
g[0].push_back(1);
int mx = 63 - __builtin_clzll(k);
node = 1;
while (node < mx) {
g[node].push_back(node + 1);
++node;
}
k ^= (1ll << mx);
vector<pair<int, int>> st;
for (int i = 0; i < mx; i++) {
if (~k >> i & 1) continue;
if (st.empty() || st.back().first + st.back().second < i)
st.push_back({i, 1});
else
st.back().second++;
}
for (auto [pos, k] : st) {
++node; k--;
g[pos].push_back(node);
while (k--) {
g[node].push_back(++node);
}
}
precalc(0);
dfs(0);
construct(0);
return b;
}
Compilation message (stderr)
perm.cpp: In function 'std::vector<int> construct_permutation(long long int)':
perm.cpp:84:22: warning: operation on 'node' may be undefined [-Wsequence-point]
84 | g[node].push_back(++node);
| ^~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |