QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#748688 | #9252. Penguins in Refrigerator | DeaphetS | WA | 7ms | 46640kb | C++14 | 3.7kb | 2024-11-14 21:04:50 | 2024-11-14 21:04:52 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
int read() {
int x = 0, f = 1; char c = getchar();
while (c < '0' || c > '9') {if (c == '-') f = -f; c = getchar();}
while (c >= '0' && c <= '9') {x = x * 10 + (c ^ 48); c = getchar();}
return x * f;
}
void write(int x) {
static char buf[11]; int tot = 0;
if (!x) return void(putchar('0'));
if (x < 0) x = -x, putchar('-');
while (x) {
buf[++tot] = x % 10 + '0';
x /= 10;
}
for (int i = tot; i; i--) putchar(buf[i]);
}
const int N = 1e6 + 5, mod = 1e9 + 7;
int n, W, root, top, ans, tot;
int a[N], b[N], rt[N], lson[N], rson[N], stk[N], siz[N], L[N], R[N], fa[N][21], In[N], Ans[N];
vector <int> e[N];
struct Seg1 {
#define lx lson[x]
#define rx rson[x]
#define ly lson[y]
#define ry rson[y]
int tot;
int t[N * 31], lson[N * 31], rson[N * 31];
void pushup(int x) {
t[x] = t[lx] + t[rx];
}
void modify(int &x, int y, int pos, int L, int R) {
x = ++tot; t[x] = t[y] + 1; lx = ly; rx = ry;
if (L == R) return;
int mid = L + R >> 1;
if (pos <= mid) modify(lx, ly, pos, L, mid);
else modify(rx, ry, pos, mid + 1, R);
pushup(x);
}
int query(int x, int y, int l, int r, int L, int R) {
if (!x) return 0;
if (l <= L && R <= r) return t[x] - t[y];
int mid = L + R >> 1, res = 0;
if (l <= mid) res += query(lx, ly, l, r, L, mid);
if (r > mid) res += query(rx, ry, l, r, mid + 1, R);
return res;
}
#undef lx
#undef rx
#undef ly
#undef ry
} seg1;
int calc(int l, int r, int L, int R) {
if (L > R) return 0;
return seg1.query(rt[r], rt[l - 1], L, R, 0, W);
}
#define lx lson[x]
#define rx rson[x]
void dfs(int x, int w, int l, int r) {
L[x] = l; R[x] = r;
if (lx) dfs(lx, W - b[a[x]], l, x - 1), fa[lx][0] = x;
if (rx) dfs(rx, W - b[a[x]], x + 1, r), fa[rx][0] = x;
if (b[a[x]] <= w) return;
siz[x] = siz[lx] + siz[rx] + 1;
int cnt = calc(l, r, w + 1, W - b[a[x]]);
if (b[a[x]] * 2 <= W) cnt--;
for (int i = 1; i <= cnt; i++) ans = 1LL * ans * (siz[x] + i) % mod;
siz[x] += cnt;
}
#undef lx
#undef rx
priority_queue <int, vector <int>, greater <int> > Q;
void Solve() {
n = read(); W = read(); ans = 1;
for (int i = 1; i <= n; i++) a[i] = read();
for (int i = 1; i <= n; i++) b[i] = read();
for (int i = 1; i <= n; i++) seg1.modify(rt[i], rt[i - 1], b[a[i]], 0, W);
for (int i = 1; i <= n; i++) {
while (top && b[a[i]] > b[a[stk[top]]]) lson[i] = stk[top--];
if (!top) root = i;
else rson[stk[top]] = i;
stk[++top] = i;
}
dfs(root, 0, 1, n);
for (int j = 1; j <= 19; j++)
for (int i = 1; i <= n; i++)
fa[i][j] = fa[fa[i][j - 1]][j - 1];
for (int i = 1; i <= n; i++) {
int x = i;
for (int j = 19; ~j; j--) {
if (!fa[x][j]) continue;
int y = fa[x][j];
if (b[a[y]] + b[a[i]] <= W) x = y;
}
if (L[x] > 1) e[a[L[x] - 1]].push_back(a[i]);
if (R[x] < n) e[a[i]].push_back(a[R[x] + 1]);
}
for (int x = 1; x <= n; x++) for (int y : e[x]) In[y]++;
for (int i = 1; i <= n; i++) if (!In[i]) Q.push(i);
tot = 0;
while (Q.size()) {
int x = Q.top(); Q.pop(); Ans[++tot] = x;
for (int y : e[x]) {
In[y]--;
if (!In[y]) Q.push(y);
}
}
write(ans); putchar('\n');
for (int i = 1; i <= n; i++) write(Ans[i]), putchar(' '); putchar('\n');
}
int main() {
int _ = 1;
while (_--) Solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 7ms
memory: 46640kb
input:
5 10 1 2 3 4 5 6 5 3 9 2
output:
3 1 2 3 4 5
result:
wrong answer 2nd lines differ - expected: '5 4 2 1 3', found: '1 2 3 4 5 '