답안 #1019908

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1019908 2024-07-11T10:31:25 Z PanosPask Žarulje (COI15_zarulje) C++14
100 / 100
289 ms 171092 KB
#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair

using namespace std;

typedef long long ll;
typedef pair<int, int> pi;

const int MOD = 1e9 + 7;
const int MAXV = 2e5;

struct State {
    int v;
    int cnt;
};

int N, K;
vector<int> a;
stack<State> s;
vector<ll> contribute;
vector<ll> fact;
vector<ll> inv;
vector<int> bef;
vector<vector<pi>> changes_before;
vector<int> aft;
vector<vector<pi>> changes_after;
vector<stack<int>> queries;
vector<int> ans;

ll cur = 1;

ll calc_power(ll a, int b)
{
    ll c = 1;
    while (b) {
        if (b % 2) {
            c = c * a % MOD;
        }

        a = a * a % MOD;
        b /= 2;
    }

    return c;
}

ll find_inverse(ll a) 
{
    return calc_power(a, MOD - 2);
}

void initialize_factorials(void)
{
    fact.resize(N + 1);
    inv.resize(N + 1);

    fact[0] = 1;
    for (int i = 1; i <= N; i++) {
        fact[i] = fact[i - 1] * i % MOD;
    }

    inv[N] = find_inverse(fact[N]);
    for (int i = N - 1; i >= 0; i--) {
        inv[i] = inv[i + 1] * (i + 1) % MOD;
    }
}

ll choose(int a, int b)
{
    ll nom = fact[a];
    ll den = inv[b] * inv[a - b] % MOD;

    return nom * den % MOD;
}

void clearstack(void)
{
    while (!s.empty()) {
        s.pop();
    }
}

void remove_val(int v)
{
    cur = cur * find_inverse(contribute[v]) % MOD;
    contribute[v] = 1;
}

void add_val(int v)
{
    contribute[v] = choose(bef[v] + aft[v], bef[v]);
    cur = cur * contribute[v] % MOD;
}

int main(void)
{
    scanf("%d %d", &N, &K);

    initialize_factorials();

    contribute.assign(MAXV + 1, 1);
    a.resize(N);
    queries.resize(N);
    ans.resize(K);
    changes_before.resize(MAXV + 1);
    changes_after.resize(MAXV + 1);
    bef.assign(MAXV + 1, 0);
    aft.assign(MAXV + 1, 0);

    for (int i = 0; i < N; i++) {
        scanf("%d", &a[i]);
    }
    for (int i = 0; i < K; i++) {
        int p;
        scanf("%d", &p);
        queries[p - 1].push(i);
    }

    for (int i = 0; i < N; i++) {
        while (!s.empty() && s.top().v > a[i]) {
            changes_before[i].pb(mp(s.top().v, -s.top().cnt));
            s.pop();
        }

        if (!s.empty() && s.top().v == a[i]) {
            s.top().cnt++;
        }
        else {
            s.push({a[i], 1});
        }

        changes_before[i].pb(mp(a[i], 1));
    }

    clearstack();

    for (int i = N - 1; i >= 0; i--) {
        while (!s.empty() && s.top().v > a[i]) {
            changes_after[i].pb(mp(s.top().v, s.top().cnt));
            s.pop();
        }

        if (!s.empty() && s.top().v == a[i]) {
            s.top().cnt++;
        }
        else {
            s.push({a[i], 1});
        }

        changes_after[i].pb(mp(a[i], -1));
    }
    while (!s.empty()) {
        aft[s.top().v] = s.top().cnt;
        s.pop();
    }

    cur = 1;
    for (int i = 0; i < N; i++) {
        for (auto [v, c] : changes_after[i]) {
            remove_val(v);
            aft[v] += c;
            add_val(v);
        }

        while (!queries[i].empty()) {
            ans[queries[i].top()] = cur;
            queries[i].pop();
        }

        for (auto [v, c] : changes_before[i]) {
            remove_val(v);
            bef[v] += c;
            add_val(v);
        }
    }

    for (int i = 0; i < K; i++) {
        printf("%d\n", ans[i]);
    }

    return 0;
}

Compilation message

zarulje.cpp: In function 'int main()':
zarulje.cpp:160:19: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  160 |         for (auto [v, c] : changes_after[i]) {
      |                   ^
zarulje.cpp:171:19: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  171 |         for (auto [v, c] : changes_before[i]) {
      |                   ^
zarulje.cpp:98:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   98 |     scanf("%d %d", &N, &K);
      |     ~~~~~^~~~~~~~~~~~~~~~~
zarulje.cpp:112:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  112 |         scanf("%d", &a[i]);
      |         ~~~~~^~~~~~~~~~~~~
zarulje.cpp:116:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  116 |         scanf("%d", &p);
      |         ~~~~~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 13032 KB Output is correct
2 Correct 5 ms 13660 KB Output is correct
3 Correct 8 ms 14428 KB Output is correct
4 Correct 7 ms 14428 KB Output is correct
5 Correct 6 ms 14428 KB Output is correct
6 Correct 8 ms 14428 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 136 ms 90040 KB Output is correct
2 Correct 202 ms 165716 KB Output is correct
3 Correct 227 ms 166784 KB Output is correct
4 Correct 239 ms 167212 KB Output is correct
5 Correct 233 ms 167436 KB Output is correct
6 Correct 236 ms 167488 KB Output is correct
7 Correct 253 ms 167504 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 13032 KB Output is correct
2 Correct 5 ms 13660 KB Output is correct
3 Correct 8 ms 14428 KB Output is correct
4 Correct 7 ms 14428 KB Output is correct
5 Correct 6 ms 14428 KB Output is correct
6 Correct 8 ms 14428 KB Output is correct
7 Correct 136 ms 90040 KB Output is correct
8 Correct 202 ms 165716 KB Output is correct
9 Correct 227 ms 166784 KB Output is correct
10 Correct 239 ms 167212 KB Output is correct
11 Correct 233 ms 167436 KB Output is correct
12 Correct 236 ms 167488 KB Output is correct
13 Correct 253 ms 167504 KB Output is correct
14 Correct 17 ms 20828 KB Output is correct
15 Correct 137 ms 91828 KB Output is correct
16 Correct 236 ms 169744 KB Output is correct
17 Correct 277 ms 168600 KB Output is correct
18 Correct 268 ms 171092 KB Output is correct
19 Correct 275 ms 168716 KB Output is correct
20 Correct 259 ms 170068 KB Output is correct
21 Correct 289 ms 170188 KB Output is correct