QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#691695#5466. Permutation CompressionEsouling#WA 0ms3824kbC++175.3kb2024-10-31 12:40:252024-10-31 12:40:25

Judging History

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

  • [2024-10-31 12:40:25]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:3824kb
  • [2024-10-31 12:40:25]
  • 提交

answer

#include <bits/stdc++.h>

using i64 = long long;

template<class Info>
struct SegmentTree {
    int n;
    std::vector<Info> info;
    SegmentTree() {}
    SegmentTree(int n_) : n(n_) {init();}

    const Info invalidInfo = Info();

    void pull(int id) {
        info[id] = info[2 * id] + info[2 * id + 1];
    }

    void init() {
        info.assign(4 * n, invalidInfo);
    }

    void change(int id, int l, int r, int p, const Info &x) {
        if (l == r) {
            info[id] = x;
            return;
        }
        int mid = l + (r - l) / 2;
        if (p <= mid) {
            change(2 * id, l, mid, p, x);
        } else {
            change(2 * id + 1, mid + 1, r, p, x);
        }
        pull(id);
    }
    void change(int p, Info x) {
        change(1, 1, n, p, x);
    }

    Info query(int id, int l, int r, int ql, int qr) {
        if (l == ql and r == qr) {
            return info[id];
        }
        int mid = l + (r - l) / 2;
        if (qr <= mid) {
            return query(2 * id, l, mid, ql, qr);
        } else if (ql > mid) {
            return query(2 * id + 1, mid + 1, r, ql, qr);
        } else {
            return query(2 * id, l, mid, ql, mid) + query(2 * id + 1, mid + 1, r, mid + 1, qr);
        }
    }
    Info query(int l, int r) {
        return query(1, 1, n, l, r);
    }

    int findLast(int id, int l, int r, int p, int x) {
        if (l > p) {
            return -1;
        }
        if (info[id].x <= x) {
            return -1;
        }
        if (l == r) {
            return l;
        }
        int mid = l + (r - l) / 2;
        if (r <= p) {
            if (info[2 * id + 1].x > x) {
                return findLast(2 * id + 1, mid + 1, r, p, x);
            } else {
                return findLast(2 * id, l, mid, p, x);
            }
        }
        if (p <= mid) {
            return findLast(2 * id, l, mid, p, x);
        } else {
            int pos = findLast(2 * id + 1, mid + 1, r, p, x);
            if (pos == -1) {
                pos = findLast(2 * id, l, mid, p, x);
            }
            return pos;
        }
    }
    int findLast(int p, int x) {
        return findLast(1, 1, n, p, x);
    }

    int findFirst(int id, int l, int r, int p, int x) {
        if (r < p) {
            return -1;
        }
        if (info[id].x <= x) {
            return -1;
        }
        if (l == r) {
            return l;
        }
        int mid = l + (r - l) / 2;
        if (p <= l) {
            if (info[2 * id].x > x) {
                return findFirst(2 * id, l, mid, p, x);
            } else {
                return findFirst(2 * id + 1, mid + 1, r, p, x);
            }
        }
        if (p > mid) {
            return findFirst(2 * id + 1, mid + 1, r, p, x);
        } else {
            int pos = findFirst(2 * id, l, mid, p, x);
            if (pos == -1) {
                pos = findFirst(2 * id + 1, mid + 1, r, p, x);
            }
            return pos;
        }
    }
    int findFirst(int p, int x) {
        return findFirst(1, 1, n, p, x);
    }
};

struct Info {
    int x, sum;
    Info() : x(-1), sum(0) {}
    Info(int x_) : x(x_), sum(1) {}
    Info operator+ (const Info &rhs) const {
        Info res;
        res.x = std::max(this->x, rhs.x);
        res.sum = this->sum + rhs.sum;
        return res;
    }
};

void solve() {
    int n, m, k;
    std::cin >> n >> m >> k;

    std::vector<int> A(n + 1), B(m + 1);
    for (int i = 1; i <= n; i++) {
        std::cin >> A[i];
    }
    for (int i = 1; i <= m; i++) {
        std::cin >> B[i];
    }

    std::vector<int> vis(n + 1);
    for (int i = 1, j = 1; i <= m; i++) {
        while (j <= n and A[j] != B[i]) {
            j++;
        }
        if (j == n + 1) {
            std::cout << "NO\n";
            return;
        }
        j++;
        vis[B[i]] = true;
    }

    std::multiset<int> S;
    for (int i = 0; i < k; i++) {
        int l;
        std::cin >> l;

        S.emplace(l);
    }

    std::vector<int> ord;
    for (int i = 1; i <= n; i++) {
        if (!vis[A[i]]) {
            ord.emplace_back(i);
        }
    }
    std::sort(ord.begin(), ord.end(), [&](int i, int j) {
        return A[i] > A[j];
    });

    SegmentTree<Info> seg(n);
    for (int i = 1; i <= n; i++) {
        seg.change(i, A[i]);
    }
    for (const auto &i : ord) {
        int x = A[i];
        int L = seg.findLast(i, x);
        int R = seg.findFirst(i, x);
        if (L == -1) {
            L = 0;
        }
        if (R == -1) {
            R = n + 1;
        }
        assert(L + 1 <= R - 1);
        // std::cerr << "i:" << i << " x:" << x << "\n";
        // std::cerr << " L:" << L << " R:" << R << "\n";
        auto [mx, cnt] = seg.query(L + 1, R - 1);

        auto it = S.upper_bound(cnt);
        if (it == S.begin()) {
            std::cout << "NO\n";
            return;
        }
        it--;
        int l = *it;
        S.extract(l);
        // std::cerr << " l:" << l << "\n";
        seg.change(i, Info());
    }
    std::cout << "YES\n";
}

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);

    int t;
    std::cin >> t;

    while (t--) {
        solve();
    }

    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3824kb

input:

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

output:

YES
YES
NO

result:

ok 3 lines

Test #2:

score: -100
Wrong Answer
time: 0ms
memory: 3560kb

input:

100
2 1 2
2 1
2
1 1
2 1 2
1 2
1
2 2
2 1 1
1 2
1
2
6 1 5
3 4 2 5 6 1
3
5 2 1 1 1
6 1 6
2 1 3 6 4 5
1
4 1 2 2 1 4
3 3 2
2 1 3
2 1 3
2 2
1 1 1
1
1
1
1 1 1
1
1
1
2 1 2
2 1
2
1 2
4 4 3
2 1 3 4
2 1 3 4
4 3 1
1 1 1
1
1
1
6 5 1
6 2 5 4 3 1
6 2 4 3 1
4
1 1 1
1
1
1
6 5 3
3 6 1 4 5 2
3 6 1 4 2
3 3 4
4 3 4
3 4 ...

output:

YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
NO
YES
YES
YES
YES
NO
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
NO
NO
NO
YES
YES
NO
NO
NO
NO
YES
NO
NO
NO
NO
NO
NO
NO
NO
NO
YES
NO
YES
YES
NO
YES
YES
YES
YES
YES
YES
NO
YES
YES
YES
YES
YES
YES
YES
NO
YES
YES
YES
YE...

result:

wrong answer 46th lines differ - expected: 'YES', found: 'NO'