답안 #995174

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
995174 2024-06-08T14:50:51 Z vjudge1 Žarulje (COI15_zarulje) C++17
100 / 100
462 ms 33108 KB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll N = 2e5 + 10;
const ll MOD = 1e9 + 7;
ll a[N], ans[N], aft[N], prv[N];
vector <ll> add[N];
vector <ll> pos[N];
ll num_aft[N];
ll num_bef[N];
ll res = 1, fact[N], invfact[N];
ll binpow(ll x, ll n){
	x %= MOD;
	ll cur = 1;
	while (n > 0) {
		if (n % 2==1) {cur = cur * x % MOD;}

		x = x * x % MOD;
		n /= 2;
	}
	return cur;
}
void tinh(ll n){
	fact[0] = 1;
	invfact[0] = 1;
	for (int i = 1; i < n; i++) fact[i] = (fact[i - 1] * i) % MOD;
	for (int i = 1; i < n; i++) invfact[i] = binpow(fact[i], MOD - 2) % MOD;
}

ll choose(ll n, ll k){
	if (k == 0 || n == k) return 1;
	return (fact[n] * ((invfact[k] * invfact[n - k]) % MOD)) % MOD;
}
int main() {
	tinh(N);
	ll n, k;
	cin >> n >> k;
	for(int i = 0; i < n; i++) {
		cin >> a[i];
        pos[a[i]].push_back(i);
	}

    stack<int> st;
    st.push(-1);
    for(int i = 0; i < n; i++) {
        while (st.size() > 1 && a[st.top()] >= a[i]) st.pop();
        num_bef[i] = upper_bound(pos[a[i]].begin(), pos[a[i]].end(), i) - lower_bound(pos[a[i]].begin(), pos[a[i]].end(), st.top());
        st.push(i);
    }
    while (!st.empty()) st.pop();
    st.push(n + 1);

    for (int i = n - 1; i >= 0; i--) {
        while (st.size() > 1 && a[st.top()] >= a[i]) st.pop();
        num_aft[i] = upper_bound(pos[a[i]].begin(), pos[a[i]].end(), st.top()) - lower_bound(pos[a[i]].begin(), pos[a[i]].end(), i);
        st.push(i);
    }
	stack<ll> end;
	for(int i = 0; i < n; i++) {
		while (!end.empty() && a[end.top()] > a[i]) end.pop();
		if (!end.empty()) add[end.top()].push_back(i);
		end.push(i);
	}

	for (auto val: add[0]) {
		aft[a[val]] = num_aft[val];
	}

	stack<ll> beg;
	ans[0] = 1;
	for(int i = 1; i < n; i++){
		while (!beg.empty() && a[beg.top()] >= a[i - 1]) {
			res = res * binpow(choose(aft[a[beg.top()]] + prv[a[beg.top()]], aft[a[beg.top()]]), MOD - 2) % MOD;
			prv[a[beg.top()]] = 0;
			beg.pop();
		}

		res = res * binpow(choose(aft[a[i -  1]] + prv[a[i - 1]], aft[a[i - 1]]), MOD - 2) % MOD;
		prv[a[i - 1]] = num_bef[i - 1];
		res = res * choose(aft[a[i - 1]] + prv[a[i - 1]], aft[a[i - 1]]) % MOD;
		beg.push(i - 1);
		res = res * binpow(choose(aft[a[i]] + prv[a[i]], aft[a[i]]), MOD - 2) % MOD;
		aft[a[i]] = 0;
		for (auto val: add[i]) {
			res = res * binpow(choose(aft[a[val]] + prv[a[val]], prv[a[val]]), MOD - 2) % MOD;
			aft[a[val]] = num_aft[val];
			res = res * choose(aft[a[val]] + prv[a[val]], prv[a[val]]) % MOD;
		}

		ans[i]=res;

	}
	while (k--) {
		ll s;
		cin >> s;
		cout << ans[s-1] << "\n";
	}
}
# 결과 실행 시간 메모리 Grader output
1 Correct 30 ms 21084 KB Output is correct
2 Correct 30 ms 19028 KB Output is correct
3 Correct 33 ms 21080 KB Output is correct
4 Correct 33 ms 19028 KB Output is correct
5 Correct 33 ms 21076 KB Output is correct
6 Correct 33 ms 19028 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 106 ms 22812 KB Output is correct
2 Correct 196 ms 26404 KB Output is correct
3 Correct 189 ms 28368 KB Output is correct
4 Correct 189 ms 28788 KB Output is correct
5 Correct 194 ms 29440 KB Output is correct
6 Correct 204 ms 29272 KB Output is correct
7 Correct 199 ms 31572 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 30 ms 21084 KB Output is correct
2 Correct 30 ms 19028 KB Output is correct
3 Correct 33 ms 21080 KB Output is correct
4 Correct 33 ms 19028 KB Output is correct
5 Correct 33 ms 21076 KB Output is correct
6 Correct 33 ms 19028 KB Output is correct
7 Correct 106 ms 22812 KB Output is correct
8 Correct 196 ms 26404 KB Output is correct
9 Correct 189 ms 28368 KB Output is correct
10 Correct 189 ms 28788 KB Output is correct
11 Correct 194 ms 29440 KB Output is correct
12 Correct 204 ms 29272 KB Output is correct
13 Correct 199 ms 31572 KB Output is correct
14 Correct 49 ms 21380 KB Output is correct
15 Correct 235 ms 24148 KB Output is correct
16 Correct 462 ms 31804 KB Output is correct
17 Correct 317 ms 30036 KB Output is correct
18 Correct 446 ms 31952 KB Output is correct
19 Correct 338 ms 28496 KB Output is correct
20 Correct 458 ms 32084 KB Output is correct
21 Correct 460 ms 33108 KB Output is correct