Submission #725025

#TimeUsernameProblemLanguageResultExecution timeMemory
725025groguŽarulje (COI15_zarulje)C++14
100 / 100
929 ms26084 KiB
#define here cerr<<"===========================================\n" #define dbg(x) cerr<<#x<<": "<<x<<endl; #include <bits/stdc++.h> #define ld double #define ll long long #define ull unsigned long long #define llinf 100000000000000000LL // 10^17 #define iinf 2000000000 // 2*10^9 #define pb push_back #define popb pop_back #define fi first #define sc second #define endl '\n' #define pii pair<int,int> #define pll pair<ll,ll> #define pld pair<ld,ld> #define all(a) a.begin(),a.end() using namespace std; #define mod 1000000007 #define maxn 200005 ll fact[maxn]; ll add(ll x,ll y){ x+=y; if(x<0){ x%=mod; x+=mod; }else{ if(x>=mod) x%=mod; } return x; } ll mul(ll a,ll b){ ll ans = (a*b)%mod; if(ans<0) ans+=mod; return ans; } ll po(ll x,ll y){ if(y==0) return 1LL; ll ans = po(x,y/2); ans = mul(ans,ans); if(y&1) ans = mul(ans,x); return ans; } ll inv(ll x){return po(x,mod-2);} ll ch(ll n,ll k){ if(k>n) return 0; ll ans = fact[n]; ans = mul(ans,inv(fact[k])); ans = mul(ans,inv(fact[n-k])); return ans; } ll n,k; ll a[maxn]; ll ask[maxn]; ll rez[maxn]; ll cntl[maxn],cntr[maxn]; vector<pll> v[maxn]; void tc(){ ios_base::sync_with_stdio(false);cerr.tie(0);cout.tie(0);cin.tie(0); fact[0] = 1; for(ll i = 1;i<maxn;i++) fact[i] = mul(fact[i-1],i); cin >> n >> k; for(ll i = 1;i<=n;i++) cin >> a[i]; ll ans = 1; stack<ll> st; for(ll i = n;i>=1;i--){ while(st.size()&&st.top()>a[i]){ cntr[st.top()]--; v[i].pb({st.top(),1}); st.pop(); } cntr[a[i]]++; v[i].pb({a[i],-1}); st.push(a[i]); } while(st.size()) st.pop(); for(ll i = 1;i<=n;i++){ for(pll p : v[i]){ ll x = p.fi; ll d = p.sc; ans = mul(ans,inv(ch(cntr[x]+cntl[x],cntl[x]))); cntr[x]+=d; ans = mul(ans,ch(cntr[x]+cntl[x],cntl[x])); } rez[i] = ans; while(st.size()&&st.top()>a[i]){ ll x = st.top(); ans = mul(ans,inv(ch(cntr[x]+cntl[x],cntl[x]))); cntl[x]--; ans = mul(ans,ch(cntr[x]+cntl[x],cntl[x])); st.pop(); } ll x = a[i]; ans = mul(ans,inv(ch(cntr[x]+cntl[x],cntl[x]))); cntl[x]++; ans = mul(ans,ch(cntr[x]+cntl[x],cntl[x])); st.push(a[i]); } for(ll i = 1;i<=k;i++){ ll x; cin >> x; cout<<rez[x]<<endl; } } int main(){ ios_base::sync_with_stdio(false);cerr.tie(0);cout.tie(0);cin.tie(0); int t; t = 1; while(t--){ tc(); } return 0; } /** 5 2 3 5 1 4 3 3 5 **/
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...