QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#794023 | #5466. Permutation Compression | JustJie# | WA | 1ms | 3844kb | C++20 | 3.7kb | 2024-11-30 09:32:03 | 2024-11-30 09:32:04 |
Judging History
answer
/***************************************************************************************************
* author : Jie Chen (4th Year CS)
* school : Rochester Institute of Technology
* created: 11.29.2024 19:49:43
****************************************************************************************************/
#include "bits/stdc++.h"
using namespace std;
using i64 = long long;
using Segment = array<int, 2>;
// Credit: Jiangly
template <typename T>
struct Fenwick {
int n;
vector<T> a;
Fenwick(int n = 0) {
init(n);
}
void init(int n) {
this->n = n;
a.assign(n, T());
}
// x is 0-index
void add(int x, T v) {
for (int i = x + 1; i <= n; i += i & -i) {
a[i - 1] += v;
}
}
// sum from a[0:x]
T sum(int x) {
auto ans = T();
for (int i = x; i > 0; i -= i & -i) {
ans += a[i - 1];
}
return ans;
}
// sum from a[l:r]
T rangeSum(int l, int r) {
return sum(r + 1) - sum(l);
}
// kth largest sum
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 work(int tc) {
int n, m, k;
cin >> n >> m >> k;
vector<int> a(n), pos(n), b(m);
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]--;
}
multiset<int> t;
for (int i = 0; i < k; i++) {
int x;
cin >> x;
t.insert(x);
}
const auto get = [&](int x) {
return pos[x];
};
int cur = 0;
vector<bool> del(n);
for (int i = 0; i < m - 1; i++) {
int l = get(b[i]);
int r = get(b[i + 1]);
if (r < l || l < cur) {
cout << "NO\n";
return;
}
cur = l;
for (int j = l + 1; j < r; j++) {
del[a[j]] = true;
}
}
Fenwick<int> fen(n);
const auto get2 = [&](int l, int r) {
return r - l + 1 - fen.rangeSum(l, r);
};
set<Segment> s;
s.insert({0, n - 1});
for (int tr = n - 1; tr >= 0; tr--) {
int p = get(tr);
auto it = prev(s.upper_bound({p, n}));
auto [l, r] = *it;
if (del[tr]) {
if (t.empty()) {
cout << "NO\n";
return;
}
auto it2 = t.upper_bound(get2(l, r));
if (it2 == t.begin()) {
cout << "NO\n";
return;
}
it2--;
t.erase(it2);
fen.add(p, 1);
if (get2(l, r) == 0) {
s.erase(it);
}
} else {
s.erase(it);
if (l == r) {
continue;
}
if (p == l) {
if (get2(l + 1, r)) {
s.insert({l + 1, r});
}
} else if (p == r) {
if (get2(l, r - 1)) {
s.insert({l, r - 1});
}
} else {
if (get2(l, p - 1)) {
s.insert({l, p - 1});
}
if (get2(p + 1, r)) {
s.insert({p + 1, r});
}
}
}
}
cout << "YES\n";
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int T;
cin >> T;
for (int t = 1; T--; t++) {
work(t);
}
}
// ~ JustJie
詳細信息
Test #1:
score: 100
Accepted
time: 1ms
memory: 3504kb
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: 3844kb
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 YES YES YES YES YES YES YES YES YES YES YES YES YES YES YES YES NO YES NO YES YES NO NO YES YES YES YES YES YES YES YES YES YES YES YES NO YES YES YES YES YES YES YES YES YES YES YES YES YES YES YES YES YES YES Y...
result:
wrong answer 23rd lines differ - expected: 'NO', found: 'YES'