QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#667393#9477. Topological Sortqianchen06WA 2ms5876kbC++202.6kb2024-10-22 22:43:352024-10-22 22:43:41

Judging History

你现在查看的是最新测评结果

  • [2024-10-22 22:43:41]
  • 评测
  • 测评结果:WA
  • 用时:2ms
  • 内存:5876kb
  • [2024-10-22 22:43:35]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
#define int long long
const int maxn = 2e5 + 10;
vector<int> e[maxn];
const int inf = 1e18;
int idx = 1;
int a[maxn];
const int mod = 998244353;
int ksm(int a, int b)
{
    if (b <= 0)
        return 1;
    int ans = 1;
    while (b)
    {
        if (b & 1)
        {
            ans = ans * a % mod;
        }
        a = a * a % mod;
        b >>= 1;
    }
    return ans;
}
int lowbit(int x)
{
    return x & -x;
}
int tr[maxn];
void update(int x, int c)
{
    for (int i = x; i < maxn; i += lowbit(i))
    {
        tr[i] += c;
    }
}
int query(int x)
{
    int ans = 0;
    for (int i = x; i; i -= lowbit(i))
    {
        ans += tr[i];
    }
    return ans;
}
vector<int> v[maxn];
signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n;
    cin >> n;
    for (int i = 1; i <= n; i++)
    {
        cin >> a[i];
    }
    int ans = 1;
    int j = 1;
    for (int i = 1; i <= n; i++)
    {
        if (i == 1 || a[i] < a[i - 1])
        {
            v[j].push_back(a[i]);
            ans = ans * ksm(2, (i - 2)) % mod;
        }
        else
        {
            v[++j].push_back(a[i]);
        }
    }
    vector<int> fir(j + 1);
    for (int i = 1; i <= j; i++)
    {
        fir[i] = v[i][0];
        reverse(v[i].begin(), v[i].end());
    }
    n = j;
    vector<int> sum(n + 1);
    vector<pair<int, int>> vec;
    for (int i = 1; i <= n; i++)
    {
        sum[i] = sum[i - 1] + v[i].size();
        vec.emplace_back(fir[i], i);
    }
    sort(vec.begin(), vec.end(), greater<pair<int, int>>());
    auto check = [&](int mid, int i) -> bool
    {
        return query(mid) == query(i);
    };
    for (auto &[fir, i] : vec)
    {
        if (query(i) == 0)
        {
            ans = ans * ksm(2, sum[i - 1]) % mod;
            update(i, 1);
            continue;
        }
        int l = 1, r = i;
        while (l < r)
        {
            int mid = l + r >> 1;
            if (check(mid, i))
            {
                r = mid;
            }
            else
                l = mid + 1;
        }
        int res = 1;
        int j = l;
        int p = upper_bound(v[j].begin(), v[j].end(), fir) - v[j].begin();
        res = res * ((ksm(2, (p + 1 + sum[i - 1] - sum[j])) % mod - 1 + mod) % mod) % mod;
        int les = v[j].size();
        les -= (p + 1);
        res = res * (ksm(2, sum[i - 1] - v[j].size() - sum[j] + les)) % mod;
        ans = ans * res % mod;
        update(i, 1);
    }
    cout << ans << '\n';
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 5876kb

input:

3
1 3 2

output:

4

result:

ok "4"

Test #2:

score: 0
Accepted
time: 0ms
memory: 5676kb

input:

5
1 2 3 4 5

output:

1024

result:

ok "1024"

Test #3:

score: 0
Accepted
time: 2ms
memory: 5620kb

input:

6
4 2 1 5 6 3

output:

4096

result:

ok "4096"

Test #4:

score: -100
Wrong Answer
time: 1ms
memory: 5552kb

input:

492
397 486 227 395 58 452 172 216 130 181 268 482 85 209 365 104 373 90 260 326 252 96 267 106 102 398 441 41 292 314 12 78 242 353 153 424 179 86 299 228 54 390 73 465 396 349 4 10 451 99 342 250 391 6 323 197 159 47 136 473 392 77 125 362 418 255 291 13 238 339 8 28 413 121 384 157 152 23 221 305...

output:

242821749

result:

wrong answer 1st words differ - expected: '73428942', found: '242821749'