QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#168813#5047. Permutationpooty#WA 76ms7872kbC++203.4kb2023-09-08 22:35:312023-09-08 22:35:33

Judging History

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

  • [2023-09-08 22:35:33]
  • 评测
  • 测评结果:WA
  • 用时:76ms
  • 内存:7872kb
  • [2023-09-08 22:35:31]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;

long long N, A[500000], pos[500000], fw[500001], M = 998244353;
map<pair<long long, long long>, long long> s;

long long q(long long i) {
    long long t = 0;
    while (i) t += fw[i], i -= i & -i;
    return t;
}

void u(long long i, long long v) {
    while (i < N + 1) fw[i] += v, i += i & -i;
}

pair<pair<long long, long long>, long long> con(long long i) {
    auto it = s.upper_bound(make_pair(i, LLONG_MAX));
    if (it == s.begin()) return make_pair(make_pair(-1, -1), -1);
    if ((--it)->first.second < i) return make_pair(make_pair(-1, -1), -1);
    return make_pair(it->first, it->second);
}

void ins(long long l, long long r, long long fl) {
    //printf("|| %lld %lld %lld\n", l, r, fl);
    if (q(r + 1) - q(l) >= 1000000) return;
    auto it = s.lower_bound(make_pair(l, 0));
    while (it != s.end() && it->first.first <= r) {
        long long nl = max(l, it->first.first);
        if (!(fl & 1)) nl = min(nl, it->first.first);
        if (!(it->second & 1)) nl = min(nl, l);
        l = nl;
        long long nr = min(r, it->first.second);
        if (!(fl & 2)) nr = max(nr, it->first.second);
        if (!(it->second & 2)) nr = min(nr, r);
        r = nr;
        fl |= it->second;
        it = s.erase(it);
    }
    while (it != s.begin() && (--it)->first.second >= l) {
        long long nl = max(l, it->first.first);
        if (!(fl & 1)) nl = min(nl, it->first.first);
        if (!(it->second & 1)) nl = min(nl, l);
        l = nl;
        long long nr = min(r, it->first.second);
        if (!(fl & 2)) nr = max(nr, it->first.second);
        if (!(it->second & 2)) nr = min(nr, r);
        r = nr;
        fl |= it->second;
        it = s.erase(it);
    }
    s.emplace(make_pair(l, r), fl);
}

int main() {
    //freopen("in.txt", "r", stdin);
    long long T;
    scanf("%lld", &T);
    //printf("%lld\n", T);
    while (T--) {
        long long C, t = 1, i, j;
        scanf("%lld %lld", &N, &C);
        for (i = 0; i < N; ++i) scanf("%lld", &A[i]), pos[--A[i]] = i;
        for (i = 0; i < N + 1; ++i) fw[i] = 0;
        for (i = 0; i < N; ++i) {
            auto p = con(pos[i]);
            //printf("%lld %lld: (%lld %lld %lld)\n", i, pos[i], p.first.first, p.first.second, p.second);
            if (p.first.first == p.first.second) {
                if (pos[i] - C >= 0) ins(pos[i] - C, pos[i] - 1, 2);
                else if (pos[i]) ins(pos[i] - 1, pos[i] - 1, 2);
                if (pos[i] + C < N) ins(pos[i] + 1, pos[i] + C, 1);
                else if (pos[i] < N - 1) ins(pos[i] + 1, pos[i] + 1, 1);
                ins(pos[i], pos[i], 3);
                u(pos[i] + 1, 1000000);
            } else {
                t *= p.first.second - p.first.first + 1 - q(p.first.second + 1) + q(p.first.first);
                //printf("<%lld | %lld %lld>\n", t, q(p.first.first), q(p.first.second + 1));
                t %= M;
                if (p.first.second - C >= 0) ins(max(p.first.first - C, 0LL), p.first.second, 0);
                if (p.first.first + C < N) ins(p.first.first, min(p.first.second + C, N - 1), 0);
                u(pos[i] + 1, 1);
            }
            //for (auto it = s.begin(); it != s.end(); ++it) printf("__ %lld %lld %lld __\n", it->first.first, it->first.second, it->second);
        }
        printf("%lld\n", t);
        s.clear();
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 3ms
memory: 7804kb

input:

5
5 3
3 4 2 1 5
5 4
4 2 1 3 5
5 2
4 5 3 1 2
5 3
4 3 2 1 5
5 2
2 3 1 5 4

output:

6
1
4
6
4

result:

ok 5 number(s): "6 1 4 6 4"

Test #2:

score: -100
Wrong Answer
time: 76ms
memory: 7872kb

input:

100000
5 4
1 3 2 5 4
5 3
5 1 4 2 3
5 2
1 4 5 3 2
5 4
5 2 4 3 1
5 4
2 5 4 1 3
5 4
1 2 3 5 4
5 4
4 3 2 5 1
5 3
1 5 4 3 2
5 3
3 2 5 4 1
5 4
4 3 1 5 2
5 4
4 3 5 2 1
5 2
3 2 1 4 5
5 3
2 4 5 1 3
5 3
2 1 4 3 5
5 3
2 1 5 4 3
5 2
2 1 3 4 5
5 4
2 3 1 4 5
5 2
1 2 4 5 3
5 3
2 4 1 5 3
5 3
2 4 3 5 1
5 3
4 1 3 5 2...

output:

24
6
2
24
1
24
24
6
18
1
24
4
6
6
6
2
1
2
1
6
6
24
18
2
18
2
6
6
6
6
4
1
6
6
1
6
24
18
6
1
12
18
6
4
2
24
2
2
24
2
2
24
6
1
1
1
1
6
1
4
1
6
1
6
2
2
6
24
6
4
6
1
2
1
2
4
6
24
18
6
2
6
1
2
6
24
1
4
6
1
1
6
1
1
24
12
6
1
4
6
1
2
24
6
2
24
6
24
1
1
6
1
18
24
1
4
1
1
2
6
1
6
4
18
1
24
6
2
4
24
1
2
12
2
6...

result:

wrong answer 3rd numbers differ - expected: '6', found: '2'