結果

問題 No.1013 〇マス進む
ユーザー marurunn11marurunn11
提出日時 2024-01-18 21:42:04
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 16,770 bytes
コンパイル時間 3,601 ms
コンパイル使用メモリ 236,296 KB
実行使用メモリ 16,568 KB
最終ジャッジ日時 2024-01-18 21:42:17
合計ジャッジ時間 12,624 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
13,480 KB
testcase_01 AC 2 ms
6,676 KB
testcase_02 AC 2 ms
6,676 KB
testcase_03 AC 3 ms
6,676 KB
testcase_04 AC 2 ms
6,676 KB
testcase_05 AC 4 ms
6,676 KB
testcase_06 AC 4 ms
6,676 KB
testcase_07 AC 4 ms
6,676 KB
testcase_08 AC 5 ms
6,676 KB
testcase_09 AC 6 ms
6,676 KB
testcase_10 AC 5 ms
6,676 KB
testcase_11 AC 3 ms
6,676 KB
testcase_12 AC 6 ms
6,676 KB
testcase_13 AC 79 ms
6,676 KB
testcase_14 AC 1,410 ms
7,356 KB
testcase_15 TLE -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
testcase_45 -- -
testcase_46 -- -
testcase_47 -- -
testcase_48 -- -
testcase_49 -- -
testcase_50 -- -
testcase_51 -- -
testcase_52 -- -
testcase_53 -- -
testcase_54 -- -
testcase_55 -- -
testcase_56 -- -
testcase_57 -- -
testcase_58 -- -
testcase_59 -- -
testcase_60 -- -
testcase_61 -- -
testcase_62 -- -
testcase_63 -- -
testcase_64 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include "bits/stdc++.h"

//#include <atcoder/all>
//using namespace atcoder;
using namespace std;

#ifdef _MSC_VER
#include <intrin.h>  //gcc上ではこれがあると動かない。__popcnt, umul128 等用のincludeファイル。
#define __builtin_popcount __popcnt
#define __builtin_popcountll __popcnt64
// 1 の位から何個 0 が連なっているか。(0 入れると 0 を返す。)
inline unsigned int __builtin_ctz(unsigned int x) { unsigned long r; _BitScanForward(&r, x); return r; }
inline unsigned int __builtin_ctzll(unsigned long long x) { unsigned long r; _BitScanForward64(&r, x); return r; }
// 2進での leading 0 の個数。(0 入れると 32, 64 を返す。)
inline unsigned int __builtin_clz(unsigned x) { return (unsigned int)__lzcnt(x); }
inline unsigned int __builtin_clzll(unsigned long long x) { return (unsigned int)__lzcnt64(x); }
#pragma warning(disable : 4996)
#pragma intrinsic(_umul128)

#include <boost/multiprecision/cpp_int.hpp>
#define __int128 boost::multiprecision::int128_t
#endif


//---------- 多倍長関連 ----------
//#include <boost/multiprecision/cpp_int.hpp>
//#include <boost/multiprecision/cpp_dec_float.hpp>
//namespace mp = boost::multiprecision;

typedef long long ll;
typedef long double ld;

#define int long long
#define LL128 boost::multiprecision::int128_t
#define LL boost::multiprecision::cpp_int
#define LD50 boost::multiprecision::cpp_dec_float_50
#define LD100 boost::multiprecision::cpp_dec_float_100

#define rep(i, n) for(long long i = 0; i < (n); ++i)
#define REP(i, s, n) for(long long i = (s); i < (n); ++i)
#define rrep(i, n) for(long long i = (n) - 1; i >= 0; --i)
#define sqrt(d) pow((ld) (d), 0.50)
#define PII pair<int, int>
#define ALL(v) v.begin(), v.end()

constexpr int INF2 = std::numeric_limits<int>::max() / 2 - 10000000;
constexpr long long INF = std::numeric_limits<long long>::max() / 2 - 10000000;
const ld pi = acos(-1);

//constexpr int MOD = 1000000007; //1e9 + 7
constexpr int MOD = 998244353;  // 7 * 17 * 2^23 + 1




//---------- chmax, min 関連 ---------- 
template<class T> inline void chmax(T& a, T b) {
	if (a < b) a = b;
}
template<class T> inline void chmin(T& a, T b) {
	if (a > b) a = b;
}




//繰り返し2乗法 (非再帰)
//N^aの、Mで割った余りを求める。
template<typename T = long long>
constexpr T my_pow(T N, long long a, T M) {
	assert(0 <= a);
	T x = N % M, res = (T)1;
	while (a) {
		if (a & 1) {
			res *= x;
			res %= M;
		}
		x *= x; // x は *this の (2のべき乗) 乗を管理する。
		x %= M;
		a >>= 1;
	}
	return res;
}

// 繰り返し2乗法 (非再帰)
// T = modint でも動く。
template<typename T = long long>
constexpr T my_pow(T N, long long a) {
	assert(0 <= a);
	T x = N, res = (T)1;
	while (a) {
		if (a & 1) res *= x;
		x *= x; // x は *this の (2のべき乗) 乗を管理する。
		a >>= 1;
	}
	return res;
}




// base を基数としたときの、n の i 桁目を、res[i] に入れる。
template<typename T = long long>
vector<signed> ll_to_vector(T n, signed base = 10) {
	assert(base > 0);
	if (n == 0) return { 0 };
	vector<signed> res;
	while (n) {
		T q = n / base;
		res.emplace_back(n - q * base);
		n = q;
	}
	return res;
}




//O (sqrt(n)) で素数判定する用。
constexpr bool is_prime(long long N) {
	//有名素数
	if (N == 1000000007 || N == 1000000009) return true;
	if (N == 998244353 || N == 167772161 || N == 469762049 || N == 1224736769) return true; //g = 3;
	if (N == 924844033 || N == 1012924417) return true; //g = 5;
	if (N == 163577857) return true; //g = 23;

	//小さい素数の別処理
	if (N <= 1) return false;
	if (N == 2 || N == 3) return true;
	if (N % 2 == 0) return false;
	if (N % 3 == 0) return false;

	for (long long i = 1; (6 * i + 1) * (6 * i + 1) <= N; ++i) {
		if (N % (6 * i + 1) == 0) return false;
	}
	for (long long i = 0; (6 * i + 5) * (6 * i + 5) <= N; ++i) {
		if (N % (6 * i + 5) == 0) return false;
	}
	return true;
}

template <int n> constexpr bool is_prime_constexpr = is_prime(n);




class UnionFind {
private:
	vector<int> parent;
	vector<int> rank;
	vector<int> v_size;
	vector<int> v_rep; //代表元
public:
	UnionFind(int N) : parent(N), rank(N, 0), v_size(N, 1), v_rep(N, 1) {
		rep(i, N) {
			parent[i] = i;
			v_rep[i] = i;
		}
	}

	int root(int x) {
		if (parent[x] == x) return x;
		return parent[x] = root(parent[x]); //経路圧縮
	}

	void unite(int x, int y) {
		int rx = root(x);
		int ry = root(y);

		if (rx == ry) return; //xの根とyの根が同じなので、何もしない。
		if (rank[rx] < rank[ry]) {
			parent[rx] = ry;
			v_size[ry] += v_size[rx];
		}
		else {
			parent[ry] = rx;
			v_size[rx] += v_size[ry];
			if (rank[rx] == rank[ry]) ++rank[rx];
		}
	}

	bool same(int x, int y) {
		return (root(x) == root(y));
	}

	int count_tree() {
		int N = parent.size();
		int res = 0;

		rep(i, N) {
			if (root(i) == i) ++res;
		}

		return res;
	}

	int size(int x) {
		return v_size[root(x)];
	}

	//代表元のセット (x を含む集合の代表元を x にする)
	void set_rep(int x) {
		assert(0 <= x && x < (int)parent.size());
		int rx = root(x);
		v_rep[rx] = x;
	}

	//x を含む集合の代表元を返す。
	int get_rep(int x) {
		assert(0 <= x && x < (int)parent.size());
		int rx = root(x);
		return v_rep[rx];
	}
};




// 幾何。二点間距離。
template<typename T = long double>
T calc_dist(int x1, int y1, int x2, int y2) {
	long long tempo = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2);
	T res = pow((T)tempo, 0.50);
	return res;
}




//ランレングス圧縮
//T は string 等のコンテナである型。要素 c と連続個数の pair を返す。
template<typename T>
auto RunLength(const T& S) {
	vector<pair<typename T::value_type, int>> res;
	if (S.empty()) return res;
	res.push_back({ S[0], 1 });

	for (int i = 1; i < (int)S.size(); i++) {
		if (res.back().first == S[i]) ++res.back().second;
		else res.push_back({ S[i], 1 });
	}
	return res;
}




void printf_ld(long double res) {
	printf("%.12Lf\n", res);
	//cout << std::fixed << std::setprecision(12) << res << endl;
}

template <class X>
void print_vec(const X& v) {
	if (v.empty()) cout << endl;
	for (typename X::const_iterator iter = std::begin(v); iter != std::end(v); ++iter) {
		if (iter != --std::end(v)) cout << *iter << " ";
		else cout << *iter << endl;
	}
}

template <class X>
void print_vec_debug(const X& v) {
	if (v.empty()) std::cerr << endl;
	for (typename X::const_iterator iter = std::begin(v); iter != std::end(v); ++iter) {
		if (iter != --std::end(v)) std::cerr << *iter << " ";
		else std::cerr << *iter << endl;
	}
}



//mint 構造体。自動で mod を取る。
//m はコンパイル時に決まる定数である必要があるので、入力を用いることはできない。
//割り算に m の素数判定が必要になり、is_prime に依存するようになった。
//※ constexpr 関数の const 修飾は C++11 では許されない。
template<int m, typename T = long long> class mint {
private:
	T _val;
public:
	//---------- コンストラクタ ----------
	constexpr mint(T v = 0LL) noexcept : _val(v% m) {
		if (_val < 0) _val += m;
	}

	constexpr T val() const noexcept {
		return _val;
	}

	//------------------------------ 二項演算子のオーバーロード ------------------------------
	constexpr mint& operator += (const mint& r) noexcept {
		_val += r._val;
		if (_val >= m) _val -= m;
		return *this;
	}
	constexpr mint& operator -= (const mint& r) noexcept {
		_val -= r._val;
		if (_val < 0) _val += m;
		return *this;
	}
	constexpr mint& operator *= (const mint& r) noexcept {
		_val *= r._val; _val %= m;
		return *this;
	}
	constexpr mint& operator /= (const mint& r) noexcept {
		if (!prime) {
			//a * u + b * v = 1 を互除法で解く。但し、gcd(a, m) == 1 でなければならない。
			T a = r._val, b = m, u = 1, v = 0;
			while (b) {
				T q = a / b;
				a -= q * b; swap(a, b); //互除法。余りをとって swap。
				u -= q * v; swap(u, v);
			}
			//assert(a == 1); //gcd(r._val, m) == 1;
			_val *= u; _val %= m;
			if (_val < 0) _val += m;
		}
		else {
			//フェルマーの小定理。底が prime である場合のみ使用可能。
			*this *= r.modpow(m - 2);
		}
		return *this;
	}

	constexpr mint operator + (const mint& r) const noexcept { return mint(*this) += r; }
	constexpr mint operator - (const mint& r) const noexcept { return mint(*this) -= r; }
	constexpr mint operator * (const mint& r) const noexcept { return mint(*this) *= r; }
	constexpr mint operator / (const mint& r) const noexcept { return mint(*this) /= r; }

	constexpr bool operator == (const mint& r) const noexcept {
		return this->_val == r._val;
	}
	constexpr bool operator != (const mint& r) const noexcept {
		return this->_val != r._val;
	}

	//------------------------------ 単項演算子のオーバーロード ------------------------------
	//---------- 前置インクリメントのオーバーロード ----------
	constexpr mint operator ++() noexcept { this->_val++; if (this->_val == m) this->_val = 0; return mint(*this); }
	constexpr mint operator --() noexcept { if (this->_val == 0) this->_val = m;  this->_val--; return mint(*this); }
	//---------- 後置インクリメントのオーバーロード ----------
	constexpr mint operator++(signed) noexcept { mint temp(_val); ++_val; if (_val == m) _val = 0; return temp; }
	constexpr mint operator--(signed) noexcept { mint temp(_val); if (_val == 0) _val = m;  --_val; return temp; }

	constexpr mint operator -() const noexcept { return mint(-_val); }

	//---------- 入出力のオーバーロード ----------
	friend constexpr ostream& operator << (ostream& os, const mint<m, T>& x) noexcept {
		return os << x._val;
	}
	friend istream& operator >> (istream& is, mint<m, T>& x) noexcept {
		T init_val;
		is >> init_val;
		x = mint<m, T>(init_val);
		return is;
	}

	//---------- 逆元 ----------
	constexpr mint<m, T> inverse() const noexcept {
		mint<m, T> e(1);
		return e / (*this);
	}

private:
	// 愚直な O(sqrt(m)) の素数判定; 余りに m が大きすぎると、コンパイル時の定数式の評価に失敗するが、1e11 程度までなら大丈夫。
	// Miller-Rabin を使ってもよい。
	static constexpr bool prime = is_prime_constexpr<m>;


	//---------- 繰り返し二乗法 ----------
	constexpr mint<m, T> modpow(long long n) const noexcept {
		assert(0 <= n);
		mint<m, T> x = *this, r = 1;
		while (n) {
			if (n & 1) r *= x;
			x *= x; // x は *this の (2のべき乗) 乗を管理する。
			n >>= 1;
		}
		return r;
	}
};

using modint = mint<MOD, long long>;




vector<modint> dp_fac;
vector<modint> dp_fac_inv;

// x! まで計算するときに最初に呼び出す。O(x).
template<typename T = modint>
void fac_initialize(int x, vector<T>& dp = dp_fac, vector<T>& dp_inv = dp_fac_inv) {
	if ((int)dp.size() <= x) {
		int n = dp.size(); if (n == 0) ++n;
		dp.resize(x + 1, (T)1);
		for (int i = n; i <= x; ++i) {
			dp.at(i) = dp.at(i - 1) * i;
		}
	}

	if ((int)dp_inv.size() <= x) {
		int n = dp_inv.size();
		dp_inv.resize(x + 1, (T)1);
		dp_inv.at(x) /= dp.at(x);
		for (int i = x - 1; i >= n; --i) {
			dp_inv.at(i) = dp_inv.at(i + 1) * (i + 1);
		}
	}
}

// 階乗。x ! まで計算する。結果は dp (デフォルトで dp_fac<modint>) に保存する。
// long long にするためには、第二引数に vector<long long> を指定する必要がある。20 ! = 2.43e18 まで long long に入る。
template<typename T = modint>
T factorial(int x, vector<T>& dp = dp_fac) {
	assert(x >= 0);

	//既に計算済み
	if ((int)dp.size() > x) {
		return dp.at(x);
	}

	int n = dp.size();
	//dp サイズを x + 1 に伸ばす。
	for (int i = n; i < x + 1; i++) {
		if (i == 0) dp.push_back((T)1);
		else dp.push_back(dp.back() * i);
	}

	return dp.at(x);
}

template<typename T = modint>
T factorial_inv(int x, vector<T>& dp = dp_fac_inv) {
	assert(x >= 0);

	//既に計算済み
	if ((int)dp.size() > x) {
		return dp.at(x);
	}

	int n = dp.size();
	//dp サイズを x + 1 に伸ばす。
	for (int i = n; i < x + 1; i++) {
		if (i == 0) dp.push_back((T)1);
		else dp.push_back(dp.back() / i);
	}

	return dp.at(x);
}


// 二項係数 N_C_a 
template<typename T = modint, typename U = int>
T my_comb(U N, U a, vector<T>& dp = dp_fac, vector<T>& dp_inv = dp_fac_inv) {
	if (N < a) return (T)0;

	T ans = factorial<T>(N, dp);
	ans *= factorial_inv<T>(a, dp_inv);
	ans *= factorial_inv<T>(N - a, dp_inv);

	return ans;
}

//二項係数 N_C_a (1点計算用)
template<typename T, typename U = int>
T my_comb2(U N, U a) {
	if (N < a) return (T)0;

	T answer = 1;
	for (U i = (U)0; i < a; i++) {
		answer *= (N - i);
		answer /= i + 1;
	}

	return answer;
}




ld now_clock() {
	ld t = (ld)clock() / (ld)CLOCKS_PER_SEC;
	return t;
}




//周期問題を解く。
template<typename U = long long>
class vec_loop {
private:
	int s = -1; // loopの開始位置
	int period = -1; //loopの周期
	U sum_period = 0; //1周期での和

	vector<U> vec; //本体のベクトル
	vector<U> sum_vec = { 0 }; // vec の和
	unordered_map<U, int> cnt; //要素の出現回数を記録。(2になったらループ発生)
	unordered_map<U, int> index_mp; //index_mp[vec[i]] = i

	vector<U> max_sum_vec_base; //ループ前の [0, i) における和の最大値
	vector<U> max_sum_vec_loop; //ループ内の [s, s + i) における和の最大値
public:
	//コンストラクタ
	vec_loop() : s(-1), period(-1), sum_vec(1, 0) {};


	//配列に push_back して、ループ検出した場合 true を返す。
	//基本的に最初にこれを true を返すまでやって、配列を構築する。
	bool push_back(U y) {
		assert(s == -1 && period == -1);
		++cnt[y];

		
		if (cnt[y] == 2) {
			//ループを検出した。
			s = index_mp[y];
			period = vec.size() - s;
			sum_period = sum_vec.back() - sum_vec[s];
			assert(vec[s] == y);


			for (int i = 0; i <= s; ++i) {
				U tmp = max_sum_vec_base.empty() ? 0 : max_sum_vec_base.back();
				tmp = max(tmp, sum_vec[i]);
				max_sum_vec_base.push_back(tmp);
			}

			for (int i = s; i <= (int)vec.size(); ++i) {
				U tmp = max_sum_vec_loop.empty() ? 0 : max_sum_vec_loop.back();
				tmp = max(tmp, sum_vec[i] - sum_vec[s]);
				max_sum_vec_loop.push_back(tmp);
			}

			return true;
		}
		else {
			//ループを検出しなかった。
			index_mp[y] = vec.size();
			vec.push_back(y);
			sum_vec.push_back(y + sum_vec.back());
			return false;
		}
	}


	//i 番目の値を返す。
	U at(const long long i) {
		assert(0 <= i);

		if (i < (int)vec.size()) {
			return vec[i];
		}
		else {
			assert(s != -1 && period != -1);
			return vec.at(s + (i - s) % period);
		}
	}
	U operator[](const int i) { return at(i); };


	//[0, r) の区間和
	U sum(long long r) {
		assert(0 <= r);
		if (r <= (int)vec.size()) {
			return sum_vec[r];
		}
		else {
			assert(s != -1 && period != -1);
			return sum_vec[s + (r - s) % period] + sum_period * ((r - s) / period);
		}
	}

	//[l, r) の区間和
	U sum(long long l, long long r) {
		assert(0 <= l && l <= r);
		return sum(r) - sum(l);
	}

	//[0, r) の 和の最大値
	U max_sum(long long r) {
		assert(s != -1 && period != -1);
		if (r <= s) return max_sum_vec_base[r];
		
		long long cnt_loop = (r - s) / period;
		U res = max_sum_vec_base.back();
		res += cnt_loop * max((U)0, sum_period) + max_sum_vec_loop[(r - s) - cnt_loop * period]; //右辺は 0 以上
		
		if (cnt_loop > 0) res = max(res, max_sum_vec_base.back() + max_sum_vec_loop.back());
		return res;
	}


	std::size_t size() {return vec.size();}
};







signed main() {
	ll N, K;
	cin >> N >> K;

	vector<int> P(N);
	rep(i, N) cin >> P[i];
	rep(i, N) P.push_back(P[i]);


	vector<int> revP(N + 1, 0);
	rep(i, N) revP[P[i]] = i;


	vector<int> to(N + 1);
	vector<vector<int>> from(N + 1);

	rep(i, N) {
		to[P[i]] = P[i + P[i]];
		from[P[i + P[i]]].push_back(P[i]);
	}


	vector<int> res0(N + 1, -1);
	REP(i, 1, N + 1) {
		if (!from[P[i]].empty()) continue;

		vec_loop vec;
		int now = i;
		while (true) {
			bool flag = vec.push_back(now);
			if (flag) break;
			now = to[now];
		}


		rep(i, (int)vec.size()) {
			res0[vec[i]] = vec.sum(i, i + K);
		}
	}



	REP(i, 1, N + 1) {
		if (res0[i] != -1) continue;

		vec_loop vec;
		int now = i;
		while (true) {
			bool flag = vec.push_back(now);
			if (flag) break;
			now = to[now];
		}


		//cerr << "i == " << i << endl;
		//cerr << "P[i] == " << P[i] << endl;
		//rep(j, (int)vec.size()) cerr << vec[j] << " ";
		//cerr << endl;

		rep(i, (int)vec.size()) {
			res0[vec[i]] = vec.sum(i, i + K);
		}
	}



	REP(i, 0, N) {
		cout << res0[P[i]] + i + 1 << endl;
	}
}
0