QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#137040#242. RMQ Similar Sequencedzy521100 ✓200ms91272kbC++232.6kb2023-08-09 17:12:212023-08-09 17:12:29

Judging History

This is the latest submission verdict.

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-08-09 17:12:29]
  • Judged
  • Verdict: 100
  • Time: 200ms
  • Memory: 91272kb
  • [2023-08-09 17:12:21]
  • Submitted

answer

#define _CRT_SEstartE_NO_DEPRECATE
#pragma warning(disable : 4996)
#include <map>
#include <unordered_map>
#include <set>
#include <fstream>
#include <queue>
#include <deque>
#include <stack>
#include <vector>
#include <string>
#include <iostream>
#include <algorithm>
#include <iterator>
#include <bitset>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdio>
#include <bits/stdc++.h>
#define ACcode ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
typedef long long ll;
typedef unsigned long long ull;
const ll maxn = 2e6 + 7;
const ll maxm = 7e6 + 7;
constexpr ll mod = 1e9 + 7;
const ll inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const int Prime = 100007;
const double eps = 1e-5;
const double pi = acos(-1.0);
using namespace std;

int fac[maxn], inv[maxn];
void init()
{
    fac[0] = 1;
    for (int i = 1; i < maxn; i++)
        fac[i] = 1ll * fac[i - 1] * i % mod;
    inv[1] = 1;
    for (int i = 2; i < maxn; i++)
        inv[i] = 1ll * inv[mod % i] * (mod - mod / i) % mod;
    inv[0] = 1;
    for (int i = 1; i < maxn; i++)
        inv[i] = 1ll * inv[i - 1] * inv[i] % mod;
}
int C(int a, int b)
{
    if (a < b || b < 0)
        return 0;
    return 1ll * fac[a] * inv[b] % mod * inv[a - b] % mod;
}
int l[maxn], r[maxn], sz[maxn], n, a[maxn], root;
ll ans = 1;

void dfs(int x)
{
    sz[x] = 1;
    int leftsz = 0;
    if (l[x])
        dfs(l[x]), sz[x] += sz[l[x]];
    if (r[x])
        dfs(r[x]), sz[x] += sz[r[x]];
    ans = ans * C(sz[x] - 1, sz[l[x]]) % mod;
}

void build()
{
    stack<int> st;
    root = 0;
    for (int i = 1; i <= n; i++)
    {
        int last = 0;
        while (!st.empty() && a[st.top()] < a[i])
        {
            last = st.top();
            st.pop();
        }
        if (!st.empty())
            r[st.top()] = i;
        else
            root = i;
        l[i] = last;
        st.push(i);
    }
}

ll qpow(ll a, ll b)
{
    ll ans = 1;
    while (b)
    {
        if (b & 1)
            ans = (ans * a) % mod;
        a = (a * a) % mod, b >>= 1;
    }
    return ans;
}
void solve()
{
    cin >> n;
    for (int i = 1; i <= n; i++)
        cin >> a[i], l[i] = r[i] = 0;
    build();
    ans = 1;
    dfs(root);
    ll temp = 1;
    for (int i = 1; i <= n; i++)
        temp = temp * i % mod;
    cout << ans * qpow(temp, mod - 2) % mod * n % mod * qpow(2, mod - 2) % mod << '\n';
}

signed main()
{
    init();
    ACcode;
    // freopen("house.in", "r", stdin);
    // freopen("house.out", "w", stdout);
    int t = 1;
    cin >> t;
    while (t--)
    {
        solve();
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 200ms
memory: 91272kb

input:

11119
1
1
2
1 2
3
1 1 3
4
3 1 1 2
5
5 5 2 4 2
6
2 6 4 4 3 3
7
3 1 1 3 5 5 4
8
4 8 4 7 1 1 7 6
9
9 3 6 3 7 6 1 7 6
10
5 1 10 2 5 1 7 7 4 1
10
4 5 4 5 10 1 5 1 2 5
10
1 2 3 6 2 9 9 7 6 2
10
2 8 3 5 2 4 10 4 8 4
10
2 9 10 3 1 1 2 10 7 7
10
5 7 7 8 10 6 4 3 10 6
10
1 8 9 3 2 4 2 5 5 9
10
4 7 9 2 5 10 10...

output:

500000004
500000004
250000002
83333334
41666667
520833337
760416672
760416672
190104168
1984127
952083340
650694449
253472224
625744052
301388891
625744052
952083340
63368056
387723217
462574408
500992067
835648154
877232149
476041670
31684028
354456021
877232149
916832017
559076007
650694449
126736...

result:

ok 11119 lines