답안 #1004061

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1004061 2024-06-21T03:49:02 Z saddd Žarulje (COI15_zarulje) C++17
100 / 100
717 ms 55404 KB
#include<bits/stdc++.h>
#include<fstream>
using namespace std;
#define sz(a) (int)a.size()
#define ALL(v) v.begin(), v.end()
#define ALLR(v) v.rbegin(), v.rend()
#define int long long
#define ll long long
#define pb push_back
#define forr(i, a, b) for(int i = a; i < b; i++)
#define dorr(i, a, b) for(int i = a; i >= b; i--)
#define ld long double
#define vt vector
#include<fstream>
#define fi first
#define se second
#define pll pair<ll, ll>
#define pii pair<int, int>
#define mpp make_pair
const ld PI = 3.14159265359, prec = 1e-9;;
//using u128 = __uint128_t;
const int cox[4] = {1, 0, -1, 0};
const int coy[4] = {0, -1, 0, 1};
const ll mod = 1e9 + 7, pr = 31;
const int mxn = 2e5 + 5, mxq = 1e5 + 5, sq = 400, mxv = 5e4 + 1;
//const int base = (1 <<18);
const ll inf = 1e9 + 5, neg = -69420, inf2 = 1e14;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
// have fun!
int n, q;
ll fact[mxn + 1];
int x[mxn + 1], a[mxn + 1];
ll pw(ll a, ll b){
    ll res = 1;
    for(;b;b >>= 1){
        if(b & 1){
            res = (res * a) % mod;
        }
        a = (a * a) % mod;
    }
    return(res);
}
ll choose(int n, int k){
    ll dem = pw((fact[k] * fact[n - k]) % mod, mod - 2);
    return((fact[n] * dem) % mod);
}
namespace sub2{
    void solve(){
        for(int i = 1; i <= q; i++){
            int pos = x[i];
            ll mn = inf;
            map<int, int>cntl, cntr;
            for(int j = pos - 1; j >= 1; j--){
                if(a[j] <= mn){
                    cntl[a[j]]++; mn = a[j];
                }
            }
            mn = inf;
            for(int j = pos + 1; j <= n; j++){
                if(a[j] <= mn){
                    mn = a[j]; cntr[a[j]]++;
                }
            }
            ll ans = 1;
            for(auto j: cntl){
                ans = (ans * choose(j.se + cntr[j.fi], j.se)) % mod;
            }
            cout << ans << "\n";
        }
    }
}
namespace sub3{
    ll res[mxn + 1];
    vt<pii>eventl[mxn + 1], eventr[mxn + 1];
    void solve(){
        vt<int>st;
        map<int, int>cntr, cntl;
        for(int i = n; i >= 1; i--){
            while(sz(st) && a[st.back()] > a[i]){
                eventr[i].pb(mpp(a[st.back()], -1)); st.pop_back();
            }
            eventr[i].pb(mpp(a[i], 1)); st.pb(i);
        }
        for(auto i: st)cntr[a[i]]++;
        st.clear();
        for(int i = 1; i <= n; i++){
            while(sz(st) && a[st.back()] > a[i]){
                eventl[i].pb(mpp(a[st.back()], -1)); st.pop_back();
            }
            eventl[i].pb(mpp(a[i], 1)); st.pb(i);
        }
        ll ans = 1;
        ll mnp = inf;
        for(int i = 1; i <= n; i++){
            for(auto [x, type]: eventr[i]){
                ll rem = pw(choose(cntl[x] + cntr[x], cntr[x]), mod - 2);
                ans = (ans * rem) % mod;
                cntr[x] += -1 * type;
                ans = (ans * choose(cntl[x] + cntr[x], cntr[x])) % mod;
            }
            res[i] = ans;
            for(auto [x, type]: eventl[i]){
                ll rem = pw(choose(cntl[x] + cntr[x], cntr[x]), mod - 2);
                ans = (ans * rem) % mod;
                cntl[x] += type;
                ans = (ans * choose(cntl[x] + cntr[x], cntr[x])) % mod;
            }
        }
        for(int i = 1; i <= q; i++){
            cout << res[x[i]] << "\n";
        }
    }
}
void solve(){
    cin >> n >> q;
    fact[0] = 1;
    for(int i = 1; i <= n; i++){
        fact[i] = (fact[i - 1] * i) % mod;
    }
    for(int i = 1; i <= n; i++){
        cin >> a[i];
    }
    for(int i = 1; i <= q; i++)cin >> x[i];
    if(q <= 5 || max(n, q) <= 2000)sub2::solve();
    else sub3::solve();
}

 
 
 
signed main(){
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    ////freopen("NOCTURNE.inp", "r", stdin);
    //freopen("NOCTURNE.out", "w", stdout);
    int tt; tt = 1;
    while(tt--){
        solve();
 
    }
    return(0);
}

Compilation message

zarulje.cpp: In function 'void sub3::solve()':
zarulje.cpp:93:12: warning: unused variable 'mnp' [-Wunused-variable]
   93 |         ll mnp = inf;
      |            ^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 14936 KB Output is correct
2 Correct 3 ms 14940 KB Output is correct
3 Correct 6 ms 15016 KB Output is correct
4 Correct 7 ms 14936 KB Output is correct
5 Correct 5 ms 14940 KB Output is correct
6 Correct 8 ms 14940 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 15196 KB Output is correct
2 Correct 11 ms 16276 KB Output is correct
3 Correct 11 ms 16476 KB Output is correct
4 Correct 12 ms 16732 KB Output is correct
5 Correct 16 ms 16984 KB Output is correct
6 Correct 17 ms 17240 KB Output is correct
7 Correct 14 ms 17244 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 14936 KB Output is correct
2 Correct 3 ms 14940 KB Output is correct
3 Correct 6 ms 15016 KB Output is correct
4 Correct 7 ms 14936 KB Output is correct
5 Correct 5 ms 14940 KB Output is correct
6 Correct 8 ms 14940 KB Output is correct
7 Correct 7 ms 15196 KB Output is correct
8 Correct 11 ms 16276 KB Output is correct
9 Correct 11 ms 16476 KB Output is correct
10 Correct 12 ms 16732 KB Output is correct
11 Correct 16 ms 16984 KB Output is correct
12 Correct 17 ms 17240 KB Output is correct
13 Correct 14 ms 17244 KB Output is correct
14 Correct 21 ms 15960 KB Output is correct
15 Correct 257 ms 27476 KB Output is correct
16 Correct 371 ms 39488 KB Output is correct
17 Correct 380 ms 38560 KB Output is correct
18 Correct 517 ms 40536 KB Output is correct
19 Correct 558 ms 39764 KB Output is correct
20 Correct 717 ms 50192 KB Output is correct
21 Correct 685 ms 55404 KB Output is correct