답안 #588209

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
588209 2022-07-02T19:24:13 Z Bungmint Žarulje (COI15_zarulje) C++17
100 / 100
312 ms 28284 KB
// Copyright © 2022 Youngmin Park. All rights reserved.
//#pragma GCC optimize("O3")
//#pragma GCC target("avx2")
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using vi = vector<int>;
using pii = pair<int, int>;
using vpi = vector<pii>;
using pll = pair<ll, ll>;
using vl = vector<ll>;
using vpl = vector<pll>;
using ld = long double;
template <typename T, size_t SZ>
using ar = array<T, SZ>;
template <typename T>
using pqg = priority_queue<T, vector<T>, greater<T>>;

#define all(v) (v).begin(), (v).end()
#define pb push_back
#define sz(x) (int)(x).size()
#define fi first
#define se second
#define lb lower_bound
#define ub upper_bound

constexpr int INF = 1e9;
constexpr ll LINF = 1e18;
const ld PI = acos((ld)-1.0);
constexpr int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
template <typename T>
constexpr bool ckmin(T &a, const T &b) { return b < a ? a = b, 1 : 0; }
template <typename T>
constexpr bool ckmax(T &a, const T &b) { return b > a ? a = b, 1 : 0; }

template <typename A, typename B>
ostream &operator<<(ostream &os, const pair<A, B> &p)
{
	return os << '(' << p.first << ", " << p.second << ')';
}
template <typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type>
ostream &operator<<(ostream &os, const T_container &v)
{
	os << '{';
	string sep;
	for (const T &x : v)
		os << sep << x, sep = ", ";
	return os << '}';
}
template <typename T>
ostream &operator<<(ostream &os, const deque<T> &v) {
	os << vector<T>(all(v));
	return os;
}
template <typename T, typename S, typename C>
ostream &operator<<(ostream &os, priority_queue<T, S, C> pq) {
	vector<T> v;
	while (sz(pq)) {
		v.pb(pq.top());
		pq.pop();
	}
	os << v;
	return os;
}
void dbg_out()
{
	cerr << "\033[0m" << endl;
}
template <typename Head, typename... Tail>
void dbg_out(Head H, Tail... T)
{
	cerr << ' ' << H;
	dbg_out(T...);
}
#ifdef LOCAL
#define dbg(...) cerr << "\033[1;35m" << __func__ << ':' << __LINE__ << " (" << #__VA_ARGS__ << "):\033[33m", dbg_out(__VA_ARGS__)
#else
#define dbg(...) 42
#endif

inline namespace RecursiveLambda
{
	template <typename Fun>
	struct y_combinator_result
	{
		Fun fun_;
		template <typename T>
		explicit y_combinator_result(T &&fun) : fun_(forward<T>(fun)) {}
		template <typename... Args>
		decltype(auto) operator()(Args &&...args)
		{
			return fun_(ref(*this), forward<Args>(args)...);
		}
	};
	template <typename Fun>
	decltype(auto) y_combinator(Fun &&fun)
	{
		return y_combinator_result<decay_t<Fun>>(forward<Fun>(fun));
	}
};

inline namespace Range {
	class ForwardRange {
		int src, dst;

	  public:
	  	explicit constexpr ForwardRange(const int l, const int r) : src(l), dst(r) {}
		explicit constexpr ForwardRange(const int n) : src(0), dst(n) {}
		constexpr ForwardRange begin() const { return *this; }
		constexpr monostate end() const { return {}; }
		constexpr bool operator!=(monostate) const { return src < dst; }
		constexpr void operator++() const {}
		constexpr int operator*() { return src++; }
	};
	class BackwardRange {
		int src, dst;

	  public:
	  	explicit constexpr BackwardRange(const int l, const int r) : src(r), dst(l) {}
		explicit constexpr BackwardRange(const int n) : src(n), dst(0) {}
		constexpr BackwardRange begin() const { return *this; }
		constexpr monostate end() const { return {}; }
		constexpr bool operator!=(monostate) const { return src > dst; }
		constexpr void operator++() const {}
		constexpr int operator*() { return --src; }
	};
	using rep = ForwardRange;
	using per = BackwardRange;
};

/**
 * Description: modular arithmetic operations 
 * Source: 
	* KACTL
	* https://codeforces.com/blog/entry/63903
	* https://codeforces.com/contest/1261/submission/65632855 (tourist)
	* https://codeforces.com/contest/1264/submission/66344993 (ksun)
	* also see https://github.com/ecnerwala/cp-book/blob/master/src/modnum.hpp (ecnerwal)
 * Verification: 
	* https://open.kattis.com/problems/modulararithmetic
 */
template<int MOD, int RT> struct mint {
	static const int mod = MOD;
	static constexpr mint rt() { return RT; } // primitive root for FFT
	int v; explicit operator int() const { return v; } // explicit -> don't silently convert to int
	constexpr mint() { v = 0; }
	constexpr mint(ll _v) noexcept { v = int((-MOD < _v && _v < MOD) ? _v : _v % MOD);
		if (v < 0) v += MOD; }
	constexpr bool operator==(const mint& o) const noexcept{
		return v == o.v; }
	constexpr friend bool operator!=(const mint& a, const mint& b) noexcept { 
		return !(a == b); }
	constexpr friend bool operator<(const mint& a, const mint& b) noexcept { 
		return a.v < b.v; }
	
	constexpr mint& operator+=(const mint& m) noexcept { 
		if ((v += m.v) >= MOD) v -= MOD; 
		return *this; }
	constexpr mint& operator-=(const mint& m) noexcept { 
		if ((v -= m.v) < 0) v += MOD; 
		return *this; }
	constexpr mint& operator*=(const mint& m) noexcept { 
		v = int((ll)v * m.v % MOD); return *this; }
	constexpr mint& operator/=(const mint& m) noexcept { return (*this) *= inv(m); }
	constexpr friend mint pow(mint a, ll p) noexcept {
		mint ans = 1; assert(p >= 0);
		for (; p; p /= 2, a *= a) if (p & 1) ans *= a;
		return ans; }
	constexpr friend mint inv(const mint& a) noexcept { assert(a.v != 0); 
		return pow(a, MOD - 2); }
		
	constexpr mint operator-() const noexcept { return mint(-v); }
	constexpr mint& operator++() noexcept { return *this += 1; }
	constexpr mint& operator--() noexcept { return *this -= 1; }
	constexpr friend mint operator+(mint a, const mint& b) noexcept { return a += b; }
	constexpr friend mint operator-(mint a, const mint& b) noexcept { return a -= b; }
	constexpr friend mint operator*(mint a, const mint& b) noexcept { return a *= b; }
	constexpr friend mint operator/(mint a, const mint& b) noexcept { return a /= b; }

	friend istream& operator>>(istream& is, mint& o){
		ll v; is >> v; o = mint(v); return is; }
	friend ostream& operator<<(ostream& os, const mint& o){
		os << o.v; return os; }
};

template <typename M>
struct Combination {
	static const int mod = M::mod;
	vector<M> fact, invFact, inv;
	Combination(int N) noexcept {
		fact.resize(N + 1), invFact.resize(N + 1), inv.resize(N + 1);
		fact[0] = 1;
		for (int i = 1; i <= N; i++) {
				fact[i] =  fact[i - 1] * i;
		}
		inv[1] = 1;
		for (int i = 2; i <= N; ++i) {
			inv[i] = mod - (mod / i) * inv[mod % i];
		}
		invFact[0] = invFact[1] = 1;
		for (int i = 2; i <= N; ++i){
			invFact[i] = invFact[i - 1] * inv[i];
		}
	}
	constexpr M binom(int n, int k) const noexcept {
		if (n < k || n < 0) return 0;
		return fact[n] * invFact[k] * invFact[n - k];
	}
};

constexpr int MOD = 1e9 + 7; // 998244353;
using Mint = mint<MOD,5>; // 5 is primitive root for both common mods
using Combo = Combination<Mint>;
using vmi = vector<Mint>;
Combo C(300000);


void solve()
{
	int n, k;
	cin >> n >> k;
	vi a(n);
	vector<Mint> ans(n, 1);
	for (auto &e : a) cin >> e;
	stack<int> stckL, stckR;
	vector<vpi> todoL(200001), todoR(200001);
	for (int i : rep(1, n)) {
		while (sz(stckL) && a[stckL.top()] > a[i - 1]) {
			todoL[a[stckL.top()]].pb({i, -1});
			stckL.pop();
		}
		stckL.push(i - 1);
		todoL[a[i - 1]].pb({i, 1});
	}
	for (int i : per(n - 1)) {
		while (sz(stckR) && a[stckR.top()] > a[i + 1]) {
			todoR[a[stckR.top()]].pb({i, -1});
			stckR.pop();
		}
		stckR.push(i + 1);
		todoR[a[i + 1]].pb({i, 1});
	}
	for (int i : rep(1, n + 1)) {
		auto &u = todoL[i];
		auto &v = todoR[i];
		int L{}, R{};
		for (auto &[id, fl] : v) R += fl, id = id + 1, fl = -fl;
		reverse(all(v));
		int p{}, q{};
		while (p != sz(u) || q != sz(v)) {
			if (p < sz(u) && q < sz(v) && u[p].fi == v[q].fi) {
				ans[u[p].fi] /= C.binom(L + R, L);
				L += u[p].se;
				R += v[q].se;
				ans[u[p].fi] *= C.binom(L + R, L);
				p++, q++;
			}else{
				if (p == sz(u) || (q != sz(v) && v[q].fi < u[p].fi)) {
					ans[v[q].fi] /= C.binom(L + R, L);
					R += v[q].se;
					ans[v[q].fi] *= C.binom(L + R, L);
					q++;
				}else{
					ans[u[p].fi] /= C.binom(L + R, L);
					L += u[p].se;
					ans[u[p].fi] *= C.binom(L + R, L);
					p++;
				}
			}
		} 
	}
	for (int i : rep(1, n)) ans[i] *= ans[i - 1];
	for (int i : rep(k)) {
		int t;
		cin >> t;
		cout << ans[t - 1] << '\n';
	}
}

int main()
{
	cin.tie(0)->sync_with_stdio(0);
	cin.exceptions(cin.failbit);
	int testcase = 1;
	// cin >> testcase;
	while (testcase--)
	{
		solve();
	}
#ifdef LOCAL
	cerr << "Time elapsed: " << 1.0 * (double)clock() / CLOCKS_PER_SEC << " s.\n";
#endif
}

Compilation message

zarulje.cpp: In function 'void solve()':
zarulje.cpp:275:11: warning: unused variable 'i' [-Wunused-variable]
  275 |  for (int i : rep(k)) {
      |           ^
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 13140 KB Output is correct
2 Correct 14 ms 13268 KB Output is correct
3 Correct 13 ms 13392 KB Output is correct
4 Correct 14 ms 13396 KB Output is correct
5 Correct 15 ms 13304 KB Output is correct
6 Correct 15 ms 13396 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 100 ms 18448 KB Output is correct
2 Correct 165 ms 23020 KB Output is correct
3 Correct 206 ms 23108 KB Output is correct
4 Correct 204 ms 24324 KB Output is correct
5 Correct 207 ms 25836 KB Output is correct
6 Correct 263 ms 26316 KB Output is correct
7 Correct 251 ms 26744 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 13140 KB Output is correct
2 Correct 14 ms 13268 KB Output is correct
3 Correct 13 ms 13392 KB Output is correct
4 Correct 14 ms 13396 KB Output is correct
5 Correct 15 ms 13304 KB Output is correct
6 Correct 15 ms 13396 KB Output is correct
7 Correct 100 ms 18448 KB Output is correct
8 Correct 165 ms 23020 KB Output is correct
9 Correct 206 ms 23108 KB Output is correct
10 Correct 204 ms 24324 KB Output is correct
11 Correct 207 ms 25836 KB Output is correct
12 Correct 263 ms 26316 KB Output is correct
13 Correct 251 ms 26744 KB Output is correct
14 Correct 23 ms 13908 KB Output is correct
15 Correct 121 ms 20300 KB Output is correct
16 Correct 205 ms 26200 KB Output is correct
17 Correct 202 ms 24308 KB Output is correct
18 Correct 227 ms 27404 KB Output is correct
19 Correct 237 ms 27168 KB Output is correct
20 Correct 277 ms 27852 KB Output is correct
21 Correct 312 ms 28284 KB Output is correct