QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#76643#5466. Permutation Compression459045WA 2ms3456kbC++173.7kb2023-02-11 12:10:022023-02-11 12:10:04

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-02-11 12:10:04]
  • 评测
  • 测评结果:WA
  • 用时:2ms
  • 内存:3456kb
  • [2023-02-11 12:10:02]
  • 提交

answer

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

// author : jiangly
template <typename T>
struct Fenwick {
    int n;
    std::vector<T> a;
    
    Fenwick(int n = 0) {
        init(n);
    }
    
    void init(int n) {
        this->n = n;
        a.assign(n, T());
    }
    
    void add(int x, T v) {
        for (int i = x + 1; i <= n; i += i & -i) {
            a[i - 1] += v;
        }
    }
    
    T sum(int x) {
        T ans = 0;
        for (int i = x; i > 0; i -= i & -i) {
            ans += a[i - 1];
        }
        return ans;
    }
    
    T rangeSum(int l, int r) {
        return sum(r) - sum(l);
    }
    
    int kth(T k) {
        int x = 0;
        for (int i = 1 << std::__lg(n); i; i /= 2) {
            if (x + i <= n && k >= a[x + i - 1]) {
                x += i;
                k -= a[x - 1];
            }
        }
        return x;
    }
};

void solve() {
    int n, m, k;
    cin >> n >> m >> k;
    vector<int> a(n), b(m);
    vector<bool> mp(n, false);
    multiset<int> len;
    vector<int> pos(n);
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
        --a[i];
        pos[a[i]] = i;
    }
    for (int i = 0; i < m; ++i) {
        cin >> b[i];
        --b[i];
        mp[b[i]] = true;
    }
    for (int i = 0; i < k; ++i) {
        int x;
        cin >> x;
        len.insert(x);
    }
    Fenwick<i64> fen(n);
    set<int> s;
    // for (int i = 0; i < n; ++i) {
    //     if (mp[i]) {
    //         s.insert(pos[i]);
    //     }
    // }
    for (int i = n - 1; i >= 0; --i) {
        if (mp[i]) {
            s.insert(pos[i]);
        } else {
            if (len.empty()) {
                cout << "NO\n";
                return;
            }
            // cerr << len.size() << '\n';
            // cerr << i + 1 << '\n';
            if (!s.empty()) {
                int l, r;
                auto it = s.upper_bound(pos[i]);
                if (it == s.end()) {
                    r = n;
                    it = prev(it);
                    l = *it;
                } else {
                    r = *it;
                    if (it == s.begin()) {
                        l = -1;
                    } else {
                        it = prev(it);
                        l = *it;
                    }
                }
                int target = (r - 1) - (l + 1) + 1;
                target -= fen.rangeSum(l + 1, r);
                // cerr << i + 1 << ' ' << l + 1 + 1 << ' ' << r - 1 + 1 << '\n';
                // cerr << target << ' ' << fen.rangeSum(l + 1, r) << '\n' << '\n';
                auto it2 = len.upper_bound(target);
                if (it2 == len.begin()) {
                    cout << "NO\n";
                    return;
                }
                it2 = prev(it2);
                len.erase(it2);
            } else {
                int target = n;
                target -= fen.rangeSum(0, n);
                auto it2 = len.upper_bound(target);
                // cerr << i + 1 << ' ' << target << ' ' << fen.rangeSum(0, n) << '\n';
                // for (auto x : len) {
                //     cerr << x << ' ';
                // }
                // cerr << '\n' << '\n';
                if (it2 == len.begin()) {
                    cout << "NO\n";
                    return;
                }
                it2 = prev(it2);
                len.erase(it2);
            }
            fen.add(pos[i], 1);
        }
    }
    cout << "YES\n";
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int t;
    cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}

詳細信息

Test #1:

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

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: 2ms
memory: 3456kb

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
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
YES
NO
YES
YES
YES
YES
YES
YES
NO
YES
YES
YES
YES
YES
YES
YES
NO
YES
YES
YES
YES
Y...

result:

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