#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2e5 + 5;
const int MOD = 1e9 + 7;
int N, K;
vector <int> pos[MAXN];
int fact[MAXN], inv[MAXN];
int equal_lft[MAXN], equal_rig[MAXN];
int ans[MAXN];
void load() {
scanf("%d%d", &N, &K);
for (int i = 1; i <= N; i++) {
int x;
scanf("%d", &x);
pos[x].push_back(i);
}
}
inline int mul(int x, int y) {
return (long long)x * y % MOD;
}
inline void mult(int &x, int y) {
x = mul(x, y);
}
int pot(int x, int y) {
int res = 1;
for (; y; y /= 2) {
if (y % 2)
res = mul(res, x);
x = mul(x, x);
}
return res;
}
void inc(int lo, int hi, int a, int b) {
mult(ans[lo], mul(fact[a + b], mul(inv[a], inv[b])));
mult(ans[hi], mul(mul(fact[a], fact[b]), inv[a + b]));
}
void solve() {
fact[0] = inv[0] = 1;
for (int i = 1; i <= N; i++) {
fact[i] = mul(fact[i - 1], i);
inv[i] = pot(fact[i], MOD - 2);
ans[i] = 1;
}
set <int> sofar;
for (int i = 1; i < MAXN; i++) {
auto get_prev = [&](int x) {
if (sofar.empty())
return 0;
auto it = sofar.lower_bound(x);
return it == sofar.begin() ? 0 : *--it;
};
auto get_next = [&](int x) {
if (sofar.empty())
return N + 1;
auto it = sofar.lower_bound(x + 1);
return it == sofar.end() ? N + 1 : *it;
};
for (auto it : pos[i]) {
auto count_equal = [&](int lo, int hi) {
return lower_bound(pos[i].begin(), pos[i].end(), hi) - lower_bound(pos[i].begin(), pos[i].end(), lo);
};
equal_lft[it] = count_equal(get_prev(it) + 1, it + 1);
equal_rig[it] = count_equal(it, get_next(it));
}
for (auto it : pos[i])
sofar.insert(it);
for (int j = 0; j < (int)pos[i].size() - 1; j++) {
auto attempt = [&](int l, int r) {
int tmp = get_next(l);
if (tmp >= r)
inc(l + 1, r, equal_lft[l], equal_rig[r]);
else if (get_next(tmp) >= r)
inc(tmp, tmp + 1, equal_lft[l], equal_rig[r]);
};
attempt(pos[i][j], pos[i][j + 1]);
if (j < (int)pos[i].size() - 2)
attempt(pos[i][j], pos[i][j + 2]);
}
}
for (int i = 2; i <= N; i++)
mult(ans[i], ans[i - 1]);
while (K--) {
int p;
scanf("%d", &p);
printf("%d ", ans[p]);
}
puts("");
}
int main() {
load();
solve();
return 0;
}
Compilation message
zarulje.cpp: In function 'void load()':
zarulje.cpp:14:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
14 | scanf("%d%d", &N, &K);
| ~~~~~^~~~~~~~~~~~~~~~
zarulje.cpp:17:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
17 | scanf("%d", &x);
| ~~~~~^~~~~~~~~~
zarulje.cpp: In function 'void solve()':
zarulje.cpp:92:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
92 | scanf("%d", &p);
| ~~~~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
4 ms |
5008 KB |
Output is correct |
3 |
Correct |
6 ms |
5204 KB |
Output is correct |
4 |
Correct |
5 ms |
5076 KB |
Output is correct |
5 |
Correct |
5 ms |
5076 KB |
Output is correct |
6 |
Correct |
5 ms |
5152 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
134 ms |
12112 KB |
Output is correct |
2 |
Correct |
283 ms |
19336 KB |
Output is correct |
3 |
Correct |
231 ms |
19264 KB |
Output is correct |
4 |
Correct |
288 ms |
19576 KB |
Output is correct |
5 |
Correct |
320 ms |
19688 KB |
Output is correct |
6 |
Correct |
250 ms |
21116 KB |
Output is correct |
7 |
Correct |
242 ms |
22380 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
4 ms |
5008 KB |
Output is correct |
3 |
Correct |
6 ms |
5204 KB |
Output is correct |
4 |
Correct |
5 ms |
5076 KB |
Output is correct |
5 |
Correct |
5 ms |
5076 KB |
Output is correct |
6 |
Correct |
5 ms |
5152 KB |
Output is correct |
7 |
Correct |
134 ms |
12112 KB |
Output is correct |
8 |
Correct |
283 ms |
19336 KB |
Output is correct |
9 |
Correct |
231 ms |
19264 KB |
Output is correct |
10 |
Correct |
288 ms |
19576 KB |
Output is correct |
11 |
Correct |
320 ms |
19688 KB |
Output is correct |
12 |
Correct |
250 ms |
21116 KB |
Output is correct |
13 |
Correct |
242 ms |
22380 KB |
Output is correct |
14 |
Correct |
16 ms |
5908 KB |
Output is correct |
15 |
Correct |
168 ms |
13568 KB |
Output is correct |
16 |
Correct |
266 ms |
22948 KB |
Output is correct |
17 |
Correct |
274 ms |
21332 KB |
Output is correct |
18 |
Correct |
348 ms |
23260 KB |
Output is correct |
19 |
Correct |
340 ms |
21740 KB |
Output is correct |
20 |
Correct |
284 ms |
23880 KB |
Output is correct |
21 |
Correct |
343 ms |
25196 KB |
Output is correct |