QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#168759#5047. Permutationpooty#WA 73ms7924kbC++203.0kb2023-09-08 21:17:452023-09-08 21:17:45

Judging History

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

  • [2023-09-08 21:17:45]
  • 评测
  • 测评结果:WA
  • 用时:73ms
  • 内存:7924kb
  • [2023-09-08 21:17:45]
  • 提交

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) {
    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);
    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 ins(pos[i] - 1, pos[i] - 1, 2);
                if (pos[i] + C < N) ins(pos[i] + 1, pos[i] + C, 1);
                else ins(pos[i] + 1, pos[i] + 1, 1);
            } 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);
            }
        }
        printf("%lld\n", t);
        s.clear();
    }
    return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 1ms
memory: 7716kb

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: 73ms
memory: 7924kb

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
2
24
24
6
18
2
24
4
6
2
2
2
1
2
1
6
6
24
18
2
18
2
6
6
6
6
4
2
6
6
1
6
24
18
6
2
12
18
6
4
2
24
2
2
24
2
1
24
6
2
1
1
2
3
1
4
1
6
1
6
2
2
3
24
6
4
6
1
2
1
2
4
6
24
18
6
2
6
1
2
6
24
2
4
6
1
1
2
6
1
24
12
6
1
4
6
1
2
24
6
2
24
6
24
6
2
6
2
18
24
2
4
1
1
1
6
1
6
4
18
1
24
2
2
4
24
1
2
12
1
6...

result:

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