QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#204337#6308. MagiczhouyuhangTL 1ms6708kbC++142.3kb2023-10-07 10:14:282023-10-07 10:14:29

Judging History

你现在查看的是最新测评结果

  • [2023-10-07 10:14:29]
  • 评测
  • 测评结果:TL
  • 用时:1ms
  • 内存:6708kb
  • [2023-10-07 10:14:28]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;

const int N = 5e3 + 10;

int n;
int a[N << 1];

struct edge {
	int to, flow;
	int nxt;
} e[N << 7];

int head[N << 5], cur[N << 5], id = 0;
void addE(int u, int v, int f) {
	e[id].to = v, e[id].flow = f;
	e[id].nxt = head[u], head[u] = id++;
}
void add(int u, int v, int f) {
	addE(u, v, f);
	addE(v, u, 0);
}

int S, T; 

int dis[N << 1];
queue<int> q; 
bool bfs() {
	memset(dis, -1, sizeof dis);
	q.push(S); dis[S] = 0;
	while (!q.empty()) {
		int u = q.front(); q.pop();
		for (int i = head[u]; ~i; i = e[i].nxt) if (e[i].flow) {
			int v = e[i].to;
			if (dis[v] == -1) {
				dis[v] = dis[u] + 1;
				q.push(v);
			}
		}
	}
	return ~dis[T];
}
int dfs(int u, int a) {
	if (u == T || !a) return a;
	int flow = 0, f;
	for (int i = cur[u]; ~i; i = e[i].nxt) {
		cur[u] = i;
		int v = e[i].to;
		if (dis[v] == dis[u] + 1 && (f = dfs(v, min(a, e[i].flow))) > 0) {
			e[i].flow -= f, e[i ^ 1].flow += f;
			a -= f, flow += f;
			if (!a) break;
		}
	}
	return flow;
}
int Dinic() {
	int ans = 0;
	while (bfs()) {
		memcpy(cur, head, sizeof cur);
		ans += dfs(S, 0x3f3f3f3f);
	}
	return ans;
}

int root, tNode;
int lc[N << 4], rc[N << 4];
void modify(int &p, int sl, int sr, int idx) {
	if (sl == sr) {
		if (!a[sl]) p = sl;
		return;
	}
	++tNode;
	lc[tNode] = lc[p], rc[tNode] = rc[p];
	p = tNode;
	int mid = (sl + sr) >> 1;
	if (idx <= mid) modify(lc[p], sl, mid, idx);
	else modify(rc[p], mid + 1, sr, idx);
	if (lc[p]) add(p, lc[p], 1);
	if (rc[p]) add(p, rc[p], 1);
}
void query(int p, int sl, int sr, int ql, int qr, int u) {
	if (!p) return;
	if (ql <= sl && sr <= qr) return add(u, p, 1);
	int mid = (sl + sr) >> 1;
	if (ql <= mid) query(lc[p], sl, mid, ql, qr, u);
	if (mid < qr) query(rc[p], mid + 1, sr, ql, qr, u);
}

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	
	cin >> n;
	for (int i = 1; i <= n; ++i) {
		int l, r;
		cin >> l >> r;
		a[l] = r;
	}
	
	memset(head, -1, sizeof head); tNode = (n << 1);
	for (int i = 1; i <= (n << 1); ++i) if (a[i]) {
		query(root, 1, (n << 1), i, a[i], i);
		modify(root, 1, (n << 1), a[i]);
	}
	S = tNode + 1, T = tNode + 2;
	for (int i = 1; i <= (n << 1); ++i) {
		if (a[i]) add(S, i, 1);
		else add(i, T, 1);
	}
	
	cout << (n << 1) - Dinic() << endl;

	return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 1ms
memory: 6708kb

input:

5
2 3
6 7
1 9
5 10
4 8

output:

9

result:

ok 1 number(s): "9"

Test #2:

score: -100
Time Limit Exceeded

input:

5000
7985 7987
42 46
1591 1593
407 410
6305 6306
1456 1457
5874 5875
7135 7137
7041 7046
6813 6815
8868 8871
665 666
4055 4056
9789 9796
7067 7068
4745 4746
5167 5171
1735 1737
2125 2128
1444 1447
1348 1352
6087 6090
1381 1384
1600 1601
5187 5190
2801 2802
8449 8450
9376 9377
4021 4024
2674 2676
490...

output:


result: