QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#354102 | #8315. Candy Ads | PlentyOfPenalty# | RE | 1ms | 8768kb | C++20 | 7.1kb | 2024-03-14 21:45:01 | 2024-03-14 21:45:03 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define cho(u, x) ((u) << 1 | (x))
const int N = 5e4 + 9, S = 1;
int n, m, w, h, bln[N], tot, tmp[N], now[N];
bool ans[N], use[N];
vector<vector<int>> G;
int tim, col_tot;
vector<int> dfn, low, col, ins, stk;
void tarjan(int u) {
// fprintf(stderr, "tarjan %d[%d] => dfn=%d low=%d\n", u >> 1, u & 1, dfn[u], low[u]);
dfn[u] = low[u] = ++tim;
ins[u] = true, stk.push_back(u);
for (int v : G[u])
if (!dfn[v]) {
tarjan(v);
low[u] = min(low[u], low[v]);
} else if (ins[v]) {
low[u] = min(low[u], dfn[v]);
}
// fprintf(stderr, "tarjan %d[%d] => dfn=%d low=%d\n", u >> 1, u & 1, dfn[u], low[u]);
if (dfn[u] == low[u]) {
++col_tot;
for (;;) {
col[stk.back()] = col_tot;
ins[stk.back()] = false;
if (u == stk.back()) break;
stk.pop_back();
}
stk.pop_back();
}
}
struct point {
int x, y;
};
struct atom {
int l, r, id;
point u;
inline bool operator<(const atom &rhs) const { return r == rhs.r ? l < rhs.l : r < rhs.r; }
} a[N];
int newnode() {
++tot;
if (G.size() <= static_cast<size_t>(tot)) {
G.resize(G.size() + N);
}
return tot;
}
void addedge(int u, int v) {
// fprintf(stderr, ">>> add edge %d[%d] %d[%d]\n", u >> 1, u & 1, v >> 1, v & 1);
G[u].emplace_back(v);
}
void check(int i, int j) {
if (a[i].u.x < a[j].u.x) {
if (a[i].u.x + w <= a[j].u.x - w) return;
} else {
if (a[j].u.x + w <= a[i].u.x - w) return;
}
if (a[i].u.y < a[j].u.y) {
if (a[i].u.y + h <= a[j].u.y - h) return;
} else {
if (a[j].u.y + h <= a[i].u.y - h) return;
}
addedge(cho(i, 1), cho(j, 0));
addedge(cho(j, 1), cho(i, 0));
}
int d, rt;
struct node {
int x[2], l[2], r[2], ch[2], src, nod;
bool operator<(const node &rhs) const { return x[d] < rhs.x[d]; }
} kdt[N];
inline void maintain(int u) {
kdt[u].l[0] = kdt[u].x[0];
if (kdt[u].ch[0] && kdt[kdt[u].ch[0]].l[0] < kdt[u].l[0]) kdt[u].l[0] = kdt[kdt[u].ch[0]].l[0];
if (kdt[u].ch[1] && kdt[kdt[u].ch[1]].l[0] < kdt[u].l[0]) kdt[u].l[0] = kdt[kdt[u].ch[1]].l[0];
kdt[u].l[1] = kdt[u].x[1];
if (kdt[u].ch[0] && kdt[kdt[u].ch[0]].l[1] < kdt[u].l[1]) kdt[u].l[1] = kdt[kdt[u].ch[0]].l[1];
if (kdt[u].ch[1] && kdt[kdt[u].ch[1]].l[1] < kdt[u].l[1]) kdt[u].l[1] = kdt[kdt[u].ch[1]].l[1];
kdt[u].r[0] = kdt[u].x[0];
if (kdt[u].ch[0] && kdt[kdt[u].ch[0]].r[0] > kdt[u].r[0]) kdt[u].r[0] = kdt[kdt[u].ch[0]].r[0];
if (kdt[u].ch[1] && kdt[kdt[u].ch[1]].r[0] > kdt[u].r[0]) kdt[u].r[0] = kdt[kdt[u].ch[1]].r[0];
kdt[u].r[1] = kdt[u].x[1];
if (kdt[u].ch[0] && kdt[kdt[u].ch[0]].r[1] > kdt[u].r[1]) kdt[u].r[1] = kdt[kdt[u].ch[0]].r[1];
if (kdt[u].ch[1] && kdt[kdt[u].ch[1]].r[1] > kdt[u].r[1]) kdt[u].r[1] = kdt[kdt[u].ch[1]].r[1];
}
int build(int l, int r, int k) {
if (l > r) return 0;
int mid = (l + r) >> 1;
d = k;
nth_element(kdt + l, kdt + mid, kdt + r + 1);
kdt[mid].ch[0] = build(l, mid - 1, k ^ 1);
kdt[mid].ch[1] = build(mid + 1, r, k ^ 1);
maintain(mid);
return mid;
}
void query(int u, const node &cur) {
if (cur.l[0] <= kdt[u].l[0] && cur.l[1] <= kdt[u].l[1] && kdt[u].r[0] <= cur.r[0] && kdt[u].r[1] <= cur.r[1]) {
addedge(cho(cur.src, 1), cho(kdt[u].nod, 0));
addedge(cho(kdt[u].nod, 1), cho(cur.src, 0));
return;
}
if (cur.l[0] > kdt[u].r[0] || cur.l[1] > kdt[u].r[1] || cur.r[0] < kdt[u].l[0] || cur.r[1] < kdt[u].l[1]) {
return;
}
if (use[kdt[u].src] && cur.l[0] <= kdt[u].x[0] && cur.l[1] <= kdt[u].x[1] && kdt[u].x[0] <= cur.r[0] && kdt[u].x[1] <= cur.r[1]) {
addedge(cho(cur.src, 1), cho(kdt[u].src, 0));
addedge(cho(kdt[u].src, 1), cho(cur.src, 0));
}
query(kdt[u].ch[0], cur);
query(kdt[u].ch[1], cur);
}
struct block {
int l, r;
vector<int> q;
void solve() {
for (int i = 1; i <= n; i++) use[i] = false;
for (int x : q) use[x] = true;
for (int i = 1; i <= n; i++) {
kdt[i].nod = newnode();
}
for (int i = 1; i <= n; i++) {
if (kdt[i].ch[0]) {
addedge(cho(kdt[i].nod, 0), cho(kdt[kdt[i].ch[0]].nod, 0));
addedge(cho(kdt[kdt[i].ch[0]].nod, 1), cho(kdt[i].nod, 1));
}
if (kdt[i].ch[1]) {
addedge(cho(kdt[i].nod, 0), cho(kdt[kdt[i].ch[1]].nod, 0));
addedge(cho(kdt[kdt[i].ch[1]].nod, 1), cho(kdt[i].nod, 1));
}
if (use[kdt[i].src]) {
addedge(cho(kdt[i].nod, 0), cho(kdt[i].src, 0));
addedge(cho(kdt[i].src, 1), cho(kdt[i].nod, 1));
}
}
node cur;
for (int i = l; i <= r; i++) {
cur.l[0] = a[i].u.x - (w * 2 - 1), cur.r[0] = a[i].u.x + (w * 2 - 1);
cur.l[1] = a[i].u.y - (h * 2 - 1), cur.r[1] = a[i].u.y + (h * 2 - 1);
cur.src = i, cur.nod = -1;
query(rt, cur);
}
}
} b[N / S + 9];
int main() {
#ifdef memset0
freopen("K.in", "r", stdin);
#endif
cin.tie(0)->sync_with_stdio(0);
cin >> n >> w >> h;
for (int i = 1; i <= n; i++) {
cin >> a[i].l >> a[i].r >> a[i].u.x >> a[i].u.y;
a[i].id = i;
}
sort(a + 1, a + n + 1);
// for (int i = 1; i <= n; i++) fprintf(stderr, "a[%d] => l=%d r=%d x=%d y=%d id=%d\n", i, a[i].l, a[i].r, a[i].u.x, a[i].u.y, a[i].id);
for (int i = 1; i <= n; i++) {
now[a[i].id] = i;
}
cin >> m;
tot = n;
G.resize(n * 2 + 100);
for (int u, v, i = 1; i <= m; i++) {
cin >> u >> v;
u = now[u];
v = now[v];
addedge(cho(u, 0), cho(v, 1));
addedge(cho(v, 0), cho(u, 1));
}
for (int i = 1; i <= n; i++) {
kdt[i].src = i;
kdt[i].x[0] = a[i].u.x;
kdt[i].x[1] = a[i].u.y;
}
rt = build(1, n, 0);
for (int i = 1; i <= n; i++) {
bln[i] = i / S;
if (!b[bln[i]].l) b[bln[i]].l = 1;
b[bln[i]].r = i;
}
for (int i = 1; i <= n; i++) tmp[i] = a[i].r;
for (int ql, qr, i = 1; i <= n; i++) {
ql = lower_bound(tmp + 1, tmp + i, a[i].l) - tmp;
qr = i - 1;
if (ql <= qr) {
// fprintf(stderr, "solve [ql=%d[%d] qr=%d[%d]] i=%d\n", ql, bln[ql], qr, bln[qr], i);
if (bln[ql] == bln[qr]) {
for (int j = ql; j <= qr; j++) check(j, i);
} else {
for (int j = ql; j <= b[bln[ql]].r; j++) check(j, i);
for (int j = b[bln[qr]].l; j <= qr; j++) check(j, i);
for (int k = bln[ql] + 1; k < bln[qr]; k++) {
b[k].q.emplace_back(i);
}
}
}
}
for (int i = 1; i <= bln[n]; i++) {
b[i].solve();
}
dfn.resize((tot + 1) << 1);
low.resize((tot + 1) << 1);
col.resize((tot + 1) << 1);
ins.resize((tot + 1) << 1);
for (int i = cho(1, 0); i <= cho(tot, 1); i++)
if (!dfn[i]) {
tarjan(i);
}
for (int i = 1; i <= tot; i++)
if (col[cho(i, 0)] == col[cho(i, 1)]) {
cout << "No" << endl;
return 0;
}
cout << "Yes" << endl;
for (int i = 1; i <= n; i++) {
ans[i] = col[cho(i, 0)] < col[cho(i, 1)];
}
// for (int i = 1; i <= n; i++) cerr << col[cho(i, 0)] << " \n"[i == n];
// for (int i = 1; i <= n; i++) cerr << col[cho(i, 1)] << " \n"[i == n];
for (int i = 1; i <= n; i++) {
cout << ans[now[i]];
}
cout << endl;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 7236kb
input:
2 2 2 1 2 1 1 2 3 2 2 1 1 2
output:
Yes 10
result:
ok accepted
Test #2:
score: 0
Accepted
time: 0ms
memory: 8768kb
input:
3 2 2 1 2 1 1 1 3 1 2 2 3 2 2 3 1 2 2 3 1 3
output:
No
result:
ok accepted
Test #3:
score: -100
Runtime Error
input:
10 3 3 2 3 7 10 1 3 9 6 3 5 2 1 4 5 8 9 4 7 1 7 3 4 4 4 6 8 9 5 1 3 6 7 2 5 7 4 6 9 9 9 10 9 1 6 2 8 5 9 5 10 4 5 1 3 2 10 9 9 5 8 2