QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#909372#242. RMQ Similar SequenceLIUIR100 ✓164ms54392kbC++143.3kb2025-02-21 19:04:522025-02-21 19:04:52

Judging History

This is the latest submission verdict.

  • [2025-02-21 19:04:52]
  • Judged
  • Verdict: 100
  • Time: 164ms
  • Memory: 54392kb
  • [2025-02-21 19:04:52]
  • Submitted

answer

/*
  Author: LIUIR
  Created: 2025.02.21  18:46:51
  Last Modified: 2025.02.21  19:04:32
*/
#include <bits/stdc++.h>
#define mp make_pair
#define eb emplace_back
#define fi first
#define se second
#define U(i, l, r) for (int i = (l); i <= (r); i++)
#define D(i, l, r) for (int i = (l); i >= (r); i--)

#ifdef _DEBUG
    #define dbg(...) __VA_ARGS__
    #define output(...) cerr << "[" << #__VA_ARGS__ << "] = " << (__VA_ARGS__) << endl
#else
    #define dbg(...) void();
    #define output(...) void();
#endif

using namespace std;
using ll = long long;
using ull = unsigned long long;
using db = double;
using ld = long double;
using vi = vector<int>;
using pii = pair<int, int>;

const auto START_TIME = chrono::steady_clock::now();
mt19937 myrand(chrono::steady_clock::now().time_since_epoch().count());
mt19937_64 myrand_64(chrono::steady_clock::now().time_since_epoch().count());
int RandInt(int l, int r){return uniform_int_distribution<int>(l, r)(myrand);}
ll RandLL(ll l, ll r){return uniform_int_distribution<ll>(l, r)(myrand_64);}
double Clock(){return chrono::duration<double>(chrono::steady_clock::now() - START_TIME).count();}

void Yes(){cout << "Yes\n";}
void No(){cout << "No\n";}
void YES(){cout << "YES\n";}
void NO(){cout << "NO\n";}
void yes(){cout << "yes\n";}
void no(){cout << "no\n";}

void SetIO(string s = "")
{
    if (s != "")
    {
        freopen((s + ".in").c_str(), "r", stdin);
        freopen((s + ".out").c_str(), "w", stdout);
    }
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout << fixed << setprecision(12);
}

const ll MOD = 1e9 + 7;
ll add(ll x, ll y){return (x += y) >= MOD && (x -= MOD), x;}
void Add(ll& x, ll y){x = add(x, y);}
ll sub(ll x, ll y){return (x -= y) < 0 && (x += MOD), x;}
void Sub(ll& x, ll y){x = sub(x, y);}
ll mul(ll x, ll y){return x * y % MOD;}
void Mul(ll& x, ll y){x = mul(x, y);}
ll Pow(ll x, ll y = MOD - 2)
{
    ll res = 1ll;
    for (; y; Mul(x, x), y >>= 1)if (y & 1)
        Mul(res, x);
    return res;
}
ll Pow(ll x, ll y, ll mod)
{
    ll res = 1ll;
    for (; y; x = x * x % mod, y >>= 1)if (y & 1)
        res = res * x % mod;
    return res;
}

const ld EPS = 1e-12;
ld Abs(ld x){return fabs(x) <= EPS ? 0 : (x > 0 ? x : -x);}
int Cmp(ld x, ld y){return fabs(x - y) <= EPS ? 0 : (x > y ? 1 : -1);}

const int N = 1e6 + 5;

int T, n, top, a[N], ls[N], rs[N], siz[N], stk[N];
ll ans;

void Dfs(int);

signed main()
{
    SetIO();
    cin >> T;
    while(T--)
    {
        cin >> n;
        for (int i = 1; i <= n; i++)
            cin >> a[i], ls[i] = rs[i] = 0;
        stk[top = 1] = 1;
        for (int i = 2; i <= n; i++)
        {
            int tp = top;
            while(top && a[stk[top]] < a[i])
                top--;
            if (top)
                rs[stk[top]] = i;
            if (top < tp)
                ls[i] = stk[top + 1];
            stk[++top] = i;
        }
        Dfs(stk[1]);
        ans = 1;
        for (int i = 1; i <= n; i++)
            ans = ans * siz[i] % MOD;
        ans = Pow(ans);
        ans = ans * n % MOD * Pow(2) % MOD;
        cout << ans << '\n';
    }
    return 0;
}

void Dfs(int u)
{
    if (!u)
        return;
    Dfs(ls[u]), Dfs(rs[u]);
    siz[u] = siz[ls[u]] + siz[rs[u]] + 1;
}

詳細信息


Pretests


Final Tests

Test #1:

score: 100
Accepted
time: 164ms
memory: 54392kb

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