Rooks Defenders(二分)-天博体育官网进入

Rooks Defenders(二分)

2026-12-06 10:13:37投稿人:名鸿娱乐招收优先270(潮州)有限公司围观3次63 评论

Rooks Defenders(二分)

题目 https://codeforces.com/contest/1679/problem/C

题意  :给定一个的矩阵,有三种操作

  • 往矩阵埋一颗地雷 ,保证对应的点之前不存在地雷。
  • 往矩阵挖掉一个颗地雷,保证对应的点之前存在地雷。
  • 查询子矩阵是否所有点都被地雷覆盖。一个点(x,y)被地雷覆盖,当且仅当至少存在一个地雷(a,b)  ,使得x==a或y==b。即至少存在一个地雷和它同行或同列 。

思路:维护没有被地雷覆盖的行和列 。对于每次查询,如果在对应范围内,存在至少一个没有被覆盖的行,和至少一个没有被覆盖的列,则说明子矩阵没有被覆盖。否则 ,说明矩阵被完全覆盖。详见代码  。


#include using namespace std;const int maxn = 100010;int n, q, op;int x, y, x2, y2;setrow, col;int numr[maxn], numc[maxn];void init() { 	row.clear();	col.clear();	memset(numr, 0, sizeof(numr));	memset(numc, 0, sizeof(numc));	vectorve;	for (int i = 1; i <= n; ++i) { 		ve.push_back(i);	}	row = { ve.begin(), ve.end()};	col = { ve.begin(), ve.end()};}// 查找s集合是否存在 [st,ed] 的元素 bool check(set&s, int st, int ed) { 	// >= st	// 用通用lower_bound超时了,还是内置的香 //	set::iterator it = lower_bound(s.begin(), s.end(), st);	set::iterator it = s.lower_bound(st);	if (it == s.end()) { 		return false;	}	if (*it >ed) { 		return false;	}		return true;}void solve() { 	scanf("%d%d", &n, &q);	init();	while (q--) { 		scanf("%d%d%d", &op, &x, &y);		switch (op) { 			case 1: 				if (numr[x]++ == 0) 					row.erase(row.find(x));				if (numc[y]++ == 0) 					col.erase(col.find(y));				break;			case 2:				if (--numr[x] == 0)					row.insert(x);				if (--numc[y] == 0)					col.insert(y);				break;			case 3:				scanf("%d%d", &x2, &y2);				if (check(row, x, x2) && check(col, y, y2))					printf("No");				else					printf("Yes");				break;		}	}}int main() { 	int t;	t = 1;	while (t--) { 		solve();	}}/*8 101 2 43 6 2 7 21 3 23 6 2 7 21 4 33 2 6 4 82 4 33 2 6 4 81 4 83 2 6 4 8*/
展开阅读全文

投稿时间:2022-05-23  最后更新:2022-09-04