結果
| 問題 | No.3529 2p Teleportations |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2026-05-04 17:41:31 |
| 言語 | C++23(gnu拡張) (gcc 15.2.0 + boost 1.89.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 9,857 bytes |
| 記録 | |
| コンパイル時間 | 8,826 ms |
| コンパイル使用メモリ | 410,984 KB |
| 実行使用メモリ | 7,976 KB |
| 最終ジャッジ日時 | 2026-05-04 20:58:45 |
| 合計ジャッジ時間 | 21,514 ms |
|
ジャッジサーバーID (参考情報) |
judge1_0 / judge2_0 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 8 WA * 41 |
ソースコード
#pragma region Yoyoyo
#ifdef LOCAL
#define _GLIBCXX_DEBUG
#endif
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using i128 = __int128_t;
using pii = pair<int, int>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
const string Yes = "Yes";
const string No = "No";
const string YES = "YES";
const string NO = "NO";
const string abc = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const ll MOD = 1000000007;
const ll mod = 998244353;
const long long inf = 1ll << 60;
const long double PI = 3.1415926535897932384626;
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define faster ios::sync_with_stdio(false);cin.tie(nullptr);
const vector<int> dx = {0, 1, 0, -1, 1, -1, -1, 1};
const vector<int> dy = {1, 0, -1, 0, 1, 1, -1, -1};
#if __has_include(<atcoder/all>)
#include <atcoder/all>
using namespace atcoder;
using mint = modint998244353;
using Mint = modint1000000007;
using pint = modint;
#endif
#define YESNO(T) \
if (T) \
{ \
cout << "YES" << endl; \
} \
else \
{ \
cout << "NO" << endl; \
}
#define yesno(T) \
if (T) \
{ \
cout << "yes" << endl; \
} \
else \
{ \
cout << "no" << endl; \
}
#define YesNo(T) \
if (T) \
{ \
cout << "Yes" << endl; \
} \
else \
{ \
cout << "No" << endl; \
}
#define print(s) cout << s << endl;
template <typename T>
inline bool chmax(T &a, T b) { return ((a < b) ? (a = b, true) : (false)); }
template <typename T>
inline bool chmin(T &a, T b) { return ((a > b) ? (a = b, true) : (false)); }
template <class T>
ll sum(const T &a) { return accumulate(all(a), 0LL); }
// pair_out
template <typename T, typename U>
ostream &operator<<(ostream &os, const pair<T, U> &p)
{
os << p.first << " " << p.second;
return os;
}
// pair_in
template <typename T, typename U>
istream &operator>>(istream &is, pair<T, U> &p)
{
is >> p.first >> p.second;
return is;
}
// vector_out
template <typename T>
ostream &operator<<(ostream &os, const vector<T> &v)
{
int s = (int)v.size();
for (int i = 0; i < s; i++)
os << (i ? " " : "") << v[i];
return os;
}
// vector_in
template <typename T>
istream &operator>>(istream &is, vector<T> &v)
{
for (auto &x : v)
is >> x;
return is;
}
//__int128_t_in
istream &operator>>(istream &is, __int128_t &x)
{
string S;
is >> S;
x = 0;
int flag = 0;
for (auto &c : S)
{
if (c == '-')
{
flag = true;
continue;
}
x *= 10;
x += c - '0';
}
if (flag)
x = -x;
return is;
}
//__uint128_t_in
istream &operator>>(istream &is, __uint128_t &x)
{
string S;
is >> S;
x = 0;
for (auto &c : S)
{
x *= 10;
x += c - '0';
}
return is;
}
//__int128_t_out
ostream &operator<<(ostream &os, __int128_t x)
{
if (x == 0)
return os << 0;
if (x < 0)
os << '-', x = -x;
string S;
while (x)
S.push_back('0' + x % 10), x /= 10;
reverse(begin(S), end(S));
return os << S;
}
//__uint128_t_out
ostream &operator<<(ostream &os, __uint128_t x)
{
if (x == 0)
return os << 0;
string S;
while (x)
S.push_back('0' + x % 10), x /= 10;
reverse(begin(S), end(S));
return os << S;
}
// vector<vector>_out
template <typename T>
ostream &operator<<(ostream &os, const vector<vector<T>> &v)
{
for (int i = 0; i < (int)v.size(); i++)
{
os << v[i] << endl;
}
return os;
}
// vector<vector<vector>>_out
template <typename T>
ostream &operator<<(ostream &os, const vector<vector<vector<T>>> &v)
{
for (int i = 0; i < (int)v.size(); i++)
{
os << "i = " << i << endl;
os << v[i];
}
return os;
}
// map_out
template <typename T, typename S>
ostream &operator<<(ostream &os, const map<T, S> &m)
{
for (auto &[key, val] : m)
{
os << key << ":" << val << " ";
}
return os;
}
// set_out
template <typename T>
ostream &operator<<(ostream &os, const set<T> &st)
{
auto itr = st.begin();
for (int i = 0; i < (int)st.size(); i++)
{
os << *itr << (i + 1 != (int)st.size() ? " " : "");
itr++;
}
return os;
}
// multiset_out
template <typename T>
ostream &operator<<(ostream &os, const multiset<T> &st)
{
auto itr = st.begin();
for (int i = 0; i < (int)st.size(); i++)
{
os << *itr << (i + 1 != (int)st.size() ? " " : "");
itr++;
}
return os;
}
// queue_out
template <typename T>
ostream &operator<<(ostream &os, queue<T> q)
{
while (q.size())
{
os << q.front() << " ";
q.pop();
}
return os;
}
// deque_out
template <typename T>
ostream &operator<<(ostream &os, deque<T> q)
{
while (q.size())
{
os << q.front() << " ";
q.pop_front();
}
return os;
}
// stack_out
template <typename T>
ostream &operator<<(ostream &os, stack<T> st)
{
while (st.size())
{
os << st.top() << " ";
st.pop();
}
return os;
}
// priority_queue_out
template <class T, class Container, class Compare>
ostream &operator<<(ostream &os, priority_queue<T, Container, Compare> pq)
{
while (pq.size())
{
os << pq.top() << " ";
pq.pop();
}
return os;
}
#if __has_include(<atcoder/all>)
// 998244353_in
istream &operator>>(istream &a, mint &b)
{
long long tmp;
a >> tmp;
b = tmp;
return a;
}
// 998244353_out
ostream &operator<<(ostream &a, mint &b)
{
a << b.val();
return a;
}
// 1000000007_in
istream &operator>>(istream &a, Mint &b)
{
long long tmp;
a >> tmp;
b = tmp;
return a;
}
// 1000000007_out
ostream &operator<<(ostream &a, Mint &b)
{
a << b.val();
return a;
}
#endif
#ifdef LOCAL
template<class... Args>
void debug_out(Args... args) {
int _i = 0;
((cerr << (_i++ ? ", " : " ") << args), ...);
cerr << "\n";
}
#define debug(...) do { \
cerr << "[" << #__VA_ARGS__ << "]:"; \
debug_out(__VA_ARGS__); \
} while(0)
#else
#define debug(...)
#endif
#pragma endregion Yoyoyo
int main(){
faster;
ll T;
cin>>T;
while(T--){
ll N,K;
cin>>N>>K;
vector<int>P(N);
cin>>P;
for(auto &e:P)e--;
vector<int>rev(N);
for(int i=0;i<N;i++)rev[P[i]]=i;
vector<vector<int>>loop(N+1);
{
vector<bool>vis(N);
for(int i=0;i<N;i++){
if(vis[i])continue;
vis[i]=true;
int sz=1;
int now=rev[i];
while(now!=i){
sz++;
vis[now]=true;
now=rev[now];
}
loop[sz].eb(i);
}
}
debug(loop);
vector<ll>ans(N,-1);
auto odd_loop = [&](int x)->void {
vector<int>lp(1,x);
{
int now=rev[x];
while(now!=x){
lp.eb(now);
now=rev[now];
}
}
int sz=lp.size();
vector<int>nex(sz);
for(ll i=0;i<sz;i++){
nex[(i*2*K)%sz]=lp[i];
}
for(ll i=0;i<sz;i++){
ans[nex[i]]=nex[(i+1)%sz];
}
};
auto even_unite = [&](int x,int y)->void {
vector<int>lp1(1,x);
{
int now=rev[x];
while(now!=x){
lp1.eb(now);
now=rev[now];
}
}
vector<int>lp2(1,y);
{
int now=rev[y];
while(now!=y){
lp2.eb(now);
now=rev[now];
}
}
int sz=lp1.size();
vector<int>nex(2*sz);
for(ll i=0;i<sz;i++){
nex[(i*2*K)%(2*sz)]=lp1[i];
}
for(ll i=0;i<sz;i++){
nex[(i*2*K+1)%(2*sz)]=lp2[i];
}
for(ll i=0;i<2*sz;i++){
ans[nex[i]]=nex[(i+1)%sz];
}
};
auto even_loop = [&](int x)->void {
vector<int>lp(1,x);
{
int now=rev[x];
while(now!=x){
lp.eb(now);
now=rev[now];
}
}
debug(lp);
int sz=lp.size();
vector<int>nex(sz-1);
for(ll i=0;i<sz-1;i++){
nex[(i*2*K)%(sz-1)]=lp[i];
}
debug("safe");
for(ll i=0;i<sz-1;i++){
ans[nex[i]]=nex[(i+1)%(sz-1)];
}
ans[lp[sz-1]]=ans[lp[sz-2]];
};
for(int i=0;i<=N;i++){
debug(i);
if(i%2==1){
for(auto e:loop[i])odd_loop(e);
}else{
int sz=loop[i].size();
for(int j=0;j<sz/2;j++){
even_unite(loop[i][2*j],loop[i][2*j+1]);
}
debug("safe");
if(sz%2)even_loop(loop[i].back());
}
}
for(auto &x:ans)x++;
print(ans);
}
}