Submission #283664

# Submission time Handle Problem Language Result Execution time Memory
283664 2020-08-26T04:56:27 Z 임성재(#5753) Žarulje (COI15_zarulje) C++17
100 / 100
119 ms 18280 KB
#include<bits/stdc++.h>
using namespace std;

#define fast ios::sync_with_stdio(false); cin.tie(0);
#define fi first
#define se second
#define em emplace
#define eb emplace_back
#define mp make_pair
#define all(v) (v).begin(), (v).end()

typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
const ll INF = 1e18;
const ll inf = 1e9 + 7;

ll inv(ll a) {
	ll b = inf - 2;
	ll ret = 1;

	while(b) {
		if(b & 1) ret *= a;
		a *= a;
		b >>= 1;
		ret %= inf;
		a %= inf;
	}

	return ret;
}

int n, k;
int a[200010];
ll f[200010];
ll in[200010];
vector<int> l, r;
vector<int> er[200010];
ll ans[200010];
int cnt[2][200010];
ll p[200010];

int main() {
	fast;

	cin >> n >> k;

	f[0] = 1;
	for(int i=1; i<=n; i++)
		f[i] = f[i-1] * i % inf;

	in[n] = inv(f[n]);

	for(int i=n; i>=1; i--) {
		in[i-1] = in[i] * i % inf;
	}

	for(int i=1; i<=n; i++) {
		cin >> a[i];
	}

	for(int i = n; i >= 1; i--) {
		while(r.size() && r.back()> a[i]) cnt[1][r.back()]--, er[i].eb(r.back()), r.pop_back();
		r.eb(a[i]);
		cnt[1][a[i]]++;
	}

	ll ans = 1;

	for(int i=1; i<=n; i++) {
		ans = ans * in[cnt[0][r.back()] + cnt[1][r.back()]] % inf * f[cnt[0][r.back()]] % inf * f[cnt[1][r.back()]] % inf;
		cnt[1][r.back()]--;
		ans = ans * f[cnt[0][r.back()] + cnt[1][r.back()]] % inf * in[cnt[0][r.back()]] % inf * in[cnt[1][r.back()]] % inf;
		
		r.pop_back();

		reverse(all(er[i]));
		for(auto j : er[i]) {
			ans = ans * in[cnt[0][j] + cnt[1][j]] % inf * f[cnt[0][j]] % inf * f[cnt[1][j]] % inf;
			cnt[1][j]++;
			ans = ans * f[cnt[0][j] + cnt[1][j]] % inf * in[cnt[0][j]] % inf * in[cnt[1][j]] % inf;

			r.eb(j);
		}

		p[i] = ans;
		
		while(l.size() && l.back() > a[i]) {
			ans = ans * in[cnt[0][l.back()] + cnt[1][l.back()]] % inf * f[cnt[0][l.back()]] % inf * f[cnt[1][l.back()]] % inf;
			cnt[0][l.back()]--;
			ans = ans * f[cnt[0][l.back()] + cnt[1][l.back()]] % inf * in[cnt[0][l.back()]] % inf * in[cnt[1][l.back()]] % inf;

			l.pop_back();
		}

		l.eb(a[i]);
		
		ans = ans * in[cnt[0][a[i]] + cnt[1][a[i]]] % inf * f[cnt[0][a[i]]] % inf * f[cnt[1][a[i]]] % inf;
		cnt[0][a[i]]++;
		ans = ans * f[cnt[0][a[i]] + cnt[1][a[i]]] % inf * in[cnt[0][a[i]]] % inf * in[cnt[1][a[i]]] % inf;
	}

	while(k--) {
		int x;
		cin >> x;

		cout << p[x] << "\n";
	}
}
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5120 KB Output is correct
2 Correct 4 ms 5120 KB Output is correct
3 Correct 4 ms 5120 KB Output is correct
4 Correct 4 ms 5120 KB Output is correct
5 Correct 4 ms 5120 KB Output is correct
6 Correct 5 ms 5152 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 35 ms 9464 KB Output is correct
2 Correct 62 ms 13816 KB Output is correct
3 Correct 65 ms 14188 KB Output is correct
4 Correct 71 ms 14164 KB Output is correct
5 Correct 81 ms 14332 KB Output is correct
6 Correct 77 ms 14968 KB Output is correct
7 Correct 76 ms 15740 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5120 KB Output is correct
2 Correct 4 ms 5120 KB Output is correct
3 Correct 4 ms 5120 KB Output is correct
4 Correct 4 ms 5120 KB Output is correct
5 Correct 4 ms 5120 KB Output is correct
6 Correct 5 ms 5152 KB Output is correct
7 Correct 35 ms 9464 KB Output is correct
8 Correct 62 ms 13816 KB Output is correct
9 Correct 65 ms 14188 KB Output is correct
10 Correct 71 ms 14164 KB Output is correct
11 Correct 81 ms 14332 KB Output is correct
12 Correct 77 ms 14968 KB Output is correct
13 Correct 76 ms 15740 KB Output is correct
14 Correct 9 ms 5736 KB Output is correct
15 Correct 56 ms 11384 KB Output is correct
16 Correct 118 ms 17400 KB Output is correct
17 Correct 103 ms 16032 KB Output is correct
18 Correct 108 ms 17784 KB Output is correct
19 Correct 98 ms 15992 KB Output is correct
20 Correct 117 ms 17332 KB Output is correct
21 Correct 119 ms 18280 KB Output is correct