제출 #1262501

#제출 시각아이디문제언어결과실행 시간메모리
1262501MisterReaperŽarulje (COI15_zarulje)C++20
100 / 100
88 ms43336 KiB
// File A.cpp created on 22.08.2025 at 14:16:08
#include <bits/stdc++.h>

using i64 = long long;

#ifdef DEBUG 
    #include "/home/ahmetalp/Desktop/Workplace/debug.h"
#else
    #define debug(...) void(23)
#endif

template<typename T>
T power(T a, i64 b) {
    T res {1};
    while (b) {
        if (b & 1) {
            res *= a;
        }
        a *= a;
        b >>= 1;
    }
    return res;
}

constexpr int md = int(1E9) + 7;
struct MInt {
    int val;
    MInt() : val(0) {}
    template<typename T>
    MInt(T x) {
        if (-md <= x && x < md) {
            val = x;
        } else {
            val = x % md;
        }
        if (val < 0) {
            val += md;
        }
    }
    int operator()() { return val; }
    MInt& operator+= (MInt rhs) {
        if ((val += rhs.val) >= md) {
            val -= md;
        }
        return *this;
    }
    MInt& operator-= (MInt rhs) {
        if ((val -= rhs.val) < 0) {
            val += md;
        }
        return *this;
    } 
    MInt& operator*= (MInt rhs) {
        val = int(1LL * val * rhs.val % md);
        return *this;
    }
    MInt inv() {
        return power(*this, md - 2);
    }
    MInt& operator/= (MInt rhs) {
        return *this *= rhs.inv();
    }
    bool operator== (MInt rhs) const {
        return val == rhs.val;
    }
    bool operator!= (MInt rhs) const {
        return val != rhs.val;
    }
};
MInt operator+ (MInt lhs, MInt rhs) {
    return lhs += rhs;
}
MInt operator- (MInt lhs, MInt rhs) {
    return lhs -= rhs;
}
MInt operator* (MInt lhs, MInt rhs) {
    return lhs *= rhs;
}
MInt operator/ (MInt lhs, MInt rhs) {
    return lhs /= rhs;
}
std::ostream& operator<< (std::ostream& os, MInt x) {
    return os << x.val;
}

using Z = MInt;

struct Comb {
    int n;
    std::vector<Z> fac_, inv_fac_, inv_;
    Comb() : n(0), fac_{1}, inv_fac_{1}, inv_{0} {}
    Comb(int m) : Comb() {
        init(m);
    }
    void init(int m) {
        fac_.resize(m + 1);
        inv_fac_.resize(m + 1);
        inv_.resize(m + 1);
        for (int i = n + 1; i <= m; ++i) {
            fac_[i] = fac_[i - 1] * i;
        }
        inv_fac_[m] = fac_[m].inv();
        for (int i = m; i > n; --i) {
            inv_fac_[i - 1] = inv_fac_[i] * i;
            inv_[i] = fac_[i - 1] * inv_fac_[i];
        }
        n = m;
    }
    Z fac(int m) {
        if (n < m) init(2 * m);
        return fac_[m];
    }
    Z inv_fac(int m) {
        if (n < m) init(2 * m);
        return inv_fac_[m];
    }
    Z inv(int m) {
        if (n < m) init(2 * m);
        return inv_[m];
    }
    Z binom(int m, int r) {
        if (m < r || r < 0) return 0;
        return fac(m) * inv_fac(r) * inv_fac(m - r);
    }
    Z inv_binom(int m, int r) {
        if (m < r || r < 0) return 0;
        return inv_fac(m) * fac(r) * fac(m - r);
    }
} comb;

constexpr int max_A = int(2E5) + 5;

int cntl[max_A], cntr[max_A];

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);

    int N, Q;
    std::cin >> N >> Q;

    std::vector<int> A(N);
    for (int i = 0; i < N; ++i) {
        std::cin >> A[i];
    }

    std::vector<int> stk;
    std::vector<int> R(N, N - 1);
    for (int i = 0; i < N; ++i) {
        while (!stk.empty() && A[stk.back()] > A[i]) {
            R[stk.back()] = i;
            stk.pop_back();
        }
        stk.emplace_back(i);
    }

    stk.clear();
    std::vector<int> L(N, 0);
    for (int i = N - 1; i >= 0; --i) {
        while (!stk.empty() && A[stk.back()] > A[i]) {
            L[stk.back()] = i;
            stk.pop_back();
        }
        stk.emplace_back(i);
    }

    debug(L, R);

    std::vector<Z> ans(N);
    std::vector<std::vector<int>> insL(N + 1), delL(N + 1);
    for (int i = 0; i < N; ++i) {
        insL[i + 1].emplace_back(A[i]);
        delL[R[i] + 1].emplace_back(A[i]);
    }
    std::vector<std::vector<int>> insR(N + 1), delR(N + 1);
    for (int i = 0; i < N; ++i) {
        insR[L[i]].emplace_back(A[i]);
        delR[i].emplace_back(A[i]);
    }

    Z res = 1;
    for (int i = 0; i < N; ++i) {
        for (auto j : insL[i]) {
            res *= comb.inv_binom(cntl[j] + cntr[j], cntl[j]);
            cntl[j]++;
            res *= comb.binom(cntl[j] + cntr[j], cntl[j]);
        }
        for (auto j : delL[i]) {
            res *= comb.inv_binom(cntl[j] + cntr[j], cntl[j]);
            cntl[j]--;
            res *= comb.binom(cntl[j] + cntr[j], cntl[j]);
        }
        for (auto j : insR[i]) {
            res *= comb.inv_binom(cntl[j] + cntr[j], cntl[j]);
            cntr[j]++;
            res *= comb.binom(cntl[j] + cntr[j], cntl[j]);
        }
        for (auto j : delR[i]) {
            res *= comb.inv_binom(cntl[j] + cntr[j], cntl[j]);
            cntr[j]--;
            res *= comb.binom(cntl[j] + cntr[j], cntl[j]);
        }
        ans[i] = res;
    }

    while (Q--) {
        int X;
        std::cin >> X;
        --X;

        std::cout << ans[X] << '\n';
    }

    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...