QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#89623 | #5466. Permutation Compression | maoxuyi | TL | 37ms | 10644kb | C++14 | 3.6kb | 2023-03-20 19:52:10 | 2023-03-20 19:52:12 |
Judging History
answer
#include <bits/stdc++.h>
#define ls ch[x][0]
#define rs ch[x][1]
using namespace std;
int val[200005], maxn[200005], minn[200005], ch[200005][2], fa[200005], rt;
int chk (int x) {
return ch[fa[x]][1] == x;
}
void pushup (int x) {
maxn[x] = minn[x] = val[x];
if (ls) {
maxn[x] = max(maxn[x], maxn[ls]);
minn[x] = min(minn[x], minn[ls]);
}
if (rs) {
maxn[x] = max(maxn[x], maxn[rs]);
minn[x] = min(minn[x], minn[ls]);
}
return;
}
void rotate (int x) {
int y = fa[x], z = fa[y], d = chk(x);
fa[x] = z;
if (z) ch[z][chk(y)] = x;
else rt = x;
if (ch[x][d^1]) fa[ch[x][d^1]] = y;
ch[y][d] = ch[x][d^1];
fa[y] = x;
ch[x][d^1] = y;
pushup(y);
pushup(x);
return;
}
void splay (int x, int top) {
while (fa[x] != top) {
int y = fa[x], z = fa[y];
if (z != top) {
if (chk(x) == chk(y))
rotate(y);
else
rotate(x);
}
rotate(x);
}
if (!top)
rt = x;
return;
}
int build (int l, int r) {
if (l > r)
return 0;
int x = (l + r) >> 1;
ls = build(l, x - 1);
rs = build(x + 1, r);
if (ls)
fa[ls] = x;
if (rs)
fa[rs] = x;
pushup(x);
return x;
}
void remove (int x) {
while (ls || rs) {
if (!rs) {
rotate(ls);
}else {
rotate(rs);
}
}
ch[fa[x]][chk(x)] = 0;
splay(fa[x], 0);
return;
}
int lmax (int k) {
splay(k, 0);
int x = ch[k][0];
if (!x)
return k;
while (x) {
if (rs && maxn[rs] > val[k])
x = rs;
else if (val[x] > val[k])
return x + 1;
else if (ls)
x = ls;
else
return x;
}
return 1;
}
int rmax (int k) {
splay(k, 0);
int x = ch[k][1];
if (!x)
return k;
while (x) {
if (ls && maxn[ls] > val[k])
x = ls;
else if (val[x] > val[k])
return x - 1;
else if (rs)
x = rs;
else
return x;
}
return 0;
}
int T;
int e[200005], p[200005], L[200005];
bool cmp (int x, int y) {
return val[x] > val[y];
}
int tr[200005] = {0};
inline int lowbit (int x) {
return x & (-x);
}
multiset <int> st;
int main () {
//freopen("a.in", "r", stdin);
//freopen("a.ans", "w", stdout);
scanf("%d", &T);
//printf("YES\nYES\nNO\n");
while (T--) {
memset(val, 0, sizeof(val));
memset(p, 0, sizeof(p));
memset(fa, 0, sizeof(fa));
memset(ch, 0, sizeof(ch));
memset(maxn, 0, sizeof(maxn));
memset(L, 0, sizeof(L));
memset(e, 0, sizeof(e));
int n, m, k, fl = 1;
scanf("%d%d%d", &n, &m, &k);
for (int i = 1; i <= n; i++)
scanf("%d", &val[i]);
rt = build(1, n);
fa[rt] = 0;
for (int i = 1, j = 1; i <= m; i++) {
int t;
scanf("%d", &t);
while (val[j] != t && j <= n)
j++;
if (j > n) {
fl = 0;
}
e[j] = 1;
}
m = n - m;
for (int i = 1, t = 0; i <= n; i++) {
if (!e[i])
p[++t] = i;
}
sort(p + 1, p + m + 1, cmp);
memset(tr, 0, sizeof(tr));
for (int i = 1; i <= n; i++) {
for (int j = i; j <= n; j += lowbit(j)) {
tr[j]++;
}
}
st.clear();
for (int i = 1; i <= k; i++) {
scanf("%d", &L[i]);
st.insert(-L[i]);
}
if (!fl) {
puts("NO");
continue;
}
for (int i = 1, j = k; i <= m; i++) {
int t, len = 0;
for (t = p[i]; t <= n && (!e[t] || (e[t] && val[t] <= val[p[i]])); t++);
for (t = t - 1; t; t -= lowbit(t))
len += tr[t];
for (t = p[i]; t >= 1 && (!e[t] || (e[t] && val[t] <= val[p[i]])); t--);
for (; t; t -= lowbit(t))
len -= tr[t];
for (t = p[i]; t <= n; t += lowbit(t))
tr[t]--;
remove(p[i]);
multiset<int>::iterator it = st.lower_bound(-len);
if (it == st.end()) {
fl = 0;
}else {
st.erase(it);
}
}
puts(fl ? "YES" : "NO");
}
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 2ms
memory: 10484kb
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: 0
Accepted
time: 29ms
memory: 10416kb
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 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 YE...
result:
ok 100 lines
Test #3:
score: 0
Accepted
time: 37ms
memory: 10468kb
input:
99 6 1 6 1 5 3 4 2 6 1 1 2 1 1 1 6 1 1 1 1 1 1 4 1 3 3 4 1 2 1 1 1 2 2 2 1 2 1 2 1 2 1 1 1 1 1 1 2 1 2 1 2 2 1 2 1 1 1 1 1 1 1 1 1 1 1 1 3 2 2 3 2 1 2 1 1 2 3 3 1 2 3 1 2 3 1 1 6 1 5 3 4 2 5 6 1 3 4 2 1 1 1 6 4 4 1 6 5 2 3 4 1 2 3 4 5 4 4 6 2 1 1 1 2 1 1 6 5 1 2 1 4 5 6 3 2 1 4 6 3 2 6 3 6 5 6 2 1 3...
output:
YES YES YES YES YES YES YES YES YES YES YES YES YES YES NO NO YES YES YES YES YES YES YES YES YES NO YES YES YES YES YES YES YES YES YES YES YES YES YES YES NO YES YES YES YES YES YES YES NO NO YES YES YES NO YES YES YES NO YES YES NO YES NO YES NO YES YES YES YES YES YES NO YES NO NO YES YES YES YE...
result:
ok 99 lines
Test #4:
score: 0
Accepted
time: 28ms
memory: 10644kb
input:
98 6 1 6 6 1 4 5 2 3 3 1 2 2 1 1 6 4 3 2 2 3 4 1 2 1 3 3 4 1 1 1 1 1 1 6 1 6 6 4 3 1 2 5 1 3 1 3 1 1 5 1 1 1 1 1 1 6 4 4 3 4 1 2 5 6 3 4 1 2 2 4 3 1 6 5 1 4 5 3 6 1 2 4 5 3 1 2 6 1 1 1 1 1 1 5 1 4 1 3 2 4 5 1 5 3 4 4 6 3 4 1 4 2 3 6 5 1 2 5 5 4 6 5 4 1 3 2 1 4 3 2 1 1 1 1 1 1 1 1 1 6 3 5 5 1 3 6 4 2...
output:
YES NO YES YES YES YES YES YES NO NO YES YES NO YES YES YES YES YES YES YES YES YES YES YES NO YES YES YES NO YES YES YES NO YES YES YES YES YES YES YES YES YES YES YES YES NO YES YES YES YES NO YES YES YES YES YES NO YES NO YES YES YES YES YES YES YES YES YES YES YES YES YES YES YES YES NO YES NO Y...
result:
ok 98 lines
Test #5:
score: -100
Time Limit Exceeded
input:
60000 1 1 1 1 1 1 1 1 1 1 1 1 3 2 1 2 3 1 2 1 2 3 3 1 1 2 3 1 2 3 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 3 3 2 3 2 1 3 2 1 1 1 2 2 1 2 1 2 1 1 1 1 1 1 1 1 2 2 1 1 2 1 2 1 1 1 1 1 1 1 3 1 3 2 3 1 1 2 3 2 3 3 2 2 3 1 2 3 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 3 2 3 3 2 1 2 1 1 2 1 3 2 2 1 3 2 3 2 3 ...
output:
YES YES YES YES YES 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 YES YES YES YES YES YES YES 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 YES YES YES YES YES...