CF983D Arkady and Rectangles (3300🥵)
首先第一步离散化。
然后这个题的一个误区就是按 std::set<int>,然后考虑把这一个坐标要更新的内容都更新完毕了之后,应该怎么加入答案,显然对于每个线段树的叶子节点
然后我们考虑怎么维护这个东西,我们对于每个线段树节点
然后记得每次加入答案的时候,更新一下线段树中每个包含这个颜色的节点的信息,不然下一次求出来要加入的颜色还是这个。
时间复杂度
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
namespace {
int readqwq() {
int x = 0;
bool f = false;
char c = getchar();
for (; c < '0' || c > '9'; c = getchar()) f |= (c == '-');
for (; c >= '0' && c <= '9'; c = getchar()) x = (x << 1) + (x << 3) + (c - '0');
if (f) {
x = -x;
}
return x;
}
ll readllqwq() {
ll x = 0;
bool f = false;
char c = getchar();
for (; c < '0' || c > '9'; c = getchar()) f |= (c == '-');
for (; c >= '0' && c <= '9'; c = getchar()) x = (x << 1) + (x << 3) + (c - '0');
if (f) {
x = -x;
}
return x;
}
const int N = 2e5 + 20;
int n;
int ans[N];
ll siz[2];
ll posx[N][2];
ll posy[N][2];
ll valx[N];
ll valy[N];
map<ll, ll> M;
vector<int> S[N][2];
void init() {
valx[0] = 0;
siz[0] = 0;
M.clear();
for (int i = 1; i <= 2 * n; ++ i) {
if (i == 1 || valx[i] != valx[i - 1]) {
siz[0] ++;
M[valx[i]] = siz[0];
}
}
for (int i = 1; i <= n; ++ i) {
posx[i][0] = M[posx[i][0]];
posx[i][1] = M[posx[i][1]] - 1;
}
valy[0] = 0;
siz[1] = 0;
M.clear();
for (int i = 1; i <= 2 * n; ++ i) {
if (i == 1 || valy[i] != valy[i - 1]) {
siz[1] ++;
M[valy[i]] = siz[1];
}
}
for (int i = 1; i <= n; ++ i) {
posy[i][0] = M[posy[i][0]];
posy[i][1] = M[posy[i][1]] - 1;
}
}
namespace SegmentTree {
struct deletable_heap {
priority_queue<int, vector<int>, less<int>> P, Q;
void init() {
while (!P.empty()) {
P.pop();
}
while (!Q.empty()) {
Q.pop();
}
}
void insertElem(int x) {
P.push(x);
}
void deleteElem(int x) {
Q.push(x);
}
void update_heap() {
while (!P.empty() && !Q.empty()) {
if (P.top() != Q.top()) {
break;
}
P.pop();
Q.pop();
}
}
int query_top() {
update_heap();
if (!P.empty()) {
return P.top();
} else {
return 0;
}
}
bool query_empty() {
update_heap();
if (P.empty()) {
return true;
} else {
return false;
}
}
} st[N * 4];
int dp[N * 4];
int valp[N * 4];
void pull(int u) {
dp[u] = 0;
valp[u] = 0;
if (!st[u].query_empty()) {
dp[u] = st[u].query_top();
if (ans[dp[u]] == 0) {
valp[u] = dp[u];
}
}
if (valp[2 * u] != 0 && valp[2 * u] >= dp[u]) {
valp[u] = max(valp[u], valp[2 * u]);
}
if (valp[2 * u + 1] != 0 && valp[2 * u + 1] >= dp[u]) {
valp[u] = max(valp[u], valp[2 * u + 1]);
}
int x = min(dp[2 * u], dp[2 * u + 1]);
dp[u] = max(dp[u], x);
if (valp[u] < dp[u]) {
valp[u] = 0;
}
}
void upd(int u, int l, int r) {
if (l == r) {
dp[u] = 0;
valp[u] = 0;
if (!st[u].query_empty()) {
dp[u] = st[u].query_top();
if (ans[dp[u]] == 0) {
valp[u] = dp[u];
}
}
} else {
pull(u);
}
}
void build(int u, int l, int r) {
st[u].init();
dp[u] = 0;
valp[u] = 0;
if (l == r) {
return;
}
int mid = (l + r) / 2;
build(2 * u, l, mid);
build(2 * u + 1, mid + 1, r);
}
void update(int u, int l, int r, int x, int y, int o, int v) {
if (x <= l && r <= y) {
if (o == 0) {
st[u].deleteElem(v);
} else if (o == 1) {
st[u].insertElem(v);
}
upd(u, l, r);
return;
}
int mid = (l + r) / 2;
if (x <= mid) update(2 * u, l, mid, x, y, o, v);
if (y > mid) update(2 * u + 1, mid + 1, r, x, y, o, v);
pull(u);
}
int query() {
return valp[1];
}
}
void Main() {
n = readqwq();
for (int i = 1; i <= n; ++ i) {
posx[i][0] = readllqwq();
posy[i][0] = readllqwq();
posx[i][1] = readllqwq();
posy[i][1] = readllqwq();
valx[i] = posx[i][0];
valx[i + n] = posx[i][1];
valy[i] = posy[i][0];
valy[i + n] = posy[i][1];
}
sort(valx + 1, valx + 1 + 2 * n);
sort(valy + 1, valy + 1 + 2 * n);
init();
for (int i = 0; i <= n; ++ i) {
ans[i] = 0;
}
for (int i = 1; i <= n; ++ i) {
S[posx[i][0]][1].push_back(i);
if (posx[i][1] >= posx[i][0] && posx[i][1] < siz[0]) {
S[posx[i][1] + 1][0].push_back(i);
}
}
SegmentTree::build(1, 1, siz[1]);
for (ll i = 1; i <= siz[0]; ++ i) {
for (auto info : S[i][1]) {
SegmentTree::update(1, 1, siz[1], posy[info][0], posy[info][1], 1, info);
}
for (auto info : S[i][0]) {
SegmentTree::update(1, 1, siz[1], posy[info][0], posy[info][1], 0, info);
}
int u = SegmentTree::query();
while (u != 0) {
ans[u] = 1;
SegmentTree::update(1, 1, siz[1], posy[u][0], posy[u][1], 2, u);
u = SegmentTree::query();
}
}
int cnt = 1;
for (int i = 1; i <= n; ++ i) {
if (ans[i] == 1) {
cnt ++;
}
}
printf("%d\n", cnt);
}
}
int main() {
::Main();
return 0;
}