Codeforces869E The Untended Antiquity

本文介绍了一个关于模拟算法的应用实例,通过标记法处理矩形网格中的障碍物建造与移除问题,判断两点是否处于同一连通区域。采用行标记策略优化计算效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

标签:模拟,标记法

Koyomi is helping Oshino, an acquaintanceof his, to take care of an open space around the abandoned Eikou Cram Schoolbuilding, Oshino's makeshift residence.

The space is represented by a rectangulargrid of n × m cells, arranged into n rows and m columns. The c-thcell in the r-th row is denoted by (r, c).

Oshino places and removes barriers aroundrectangular areas of cells. Specifically, an action denoted by "1 r1c1 r2 c2" meansOshino's placing barriers around a rectangle with two corners being (r1, c1)and (r2, c2) and sides parallel to squaressides. Similarly, "2 r1 c1 r2c2" means Oshino's removing barriers around therectangle. Oshino ensures that no barriers staying on the ground share anycommon points, nor do they intersect with boundaries of the n × m area.

Sometimes Koyomi tries to walk from onecell to another carefully without striding over barriers, in order to avoiddamaging various items on the ground. "3 r1 c1r2 c2" means that Koyomi tries to walkfrom (r1, c1) to (r2, c2)without crossing barriers.

And you're here to tell Koyomi thefeasibility of each of his attempts.

Input

The first line of input contains threespace-separated integers n, m and q (1 ≤ n, m ≤ 2 500, 1 ≤ q ≤ 100 000) — the number of rows and columns inthe grid, and the total number of Oshino and Koyomi's actions, respectively.

The following q lines each describesan action, containing five space-separated integers t, r1,c1, r2, c2 (1 ≤ t ≤ 3, 1 ≤ r1, r2 ≤ n, 1 ≤ c1, c2 ≤ m) — the type and two coordinates of an action. Additionally, thefollowing holds depending on the value of t:

  • If t = 1: 2 ≤ r1 ≤ r2 ≤ n - 1, 2 ≤ c1 ≤ c2 ≤ m - 1;
  • If t = 2: 2 ≤ r1 ≤ r2 ≤ n - 1, 2 ≤ c1 ≤ c2 ≤ m - 1, the specified group of barriers exist on the ground before the removal.
  • If t = 3: no extra restrictions.

Output

For each of Koyomi's attempts (actions witht = 3), output one line — containing "Yes"(without quotes) if it's feasible, and "No" (without quotes)otherwise.

Examples

Input

5 6 5
1 2 2 4 5
1 3 3 3 3
3 4 4 1 1
2 2 2 4 5
3 1 1 4 4

Output

No
Yes

Input

2500 2500 8
1 549 1279 1263 2189
1 303 795 1888 2432
1 2227 622 2418 1161
3 771 2492 1335 1433
1 2017 2100 2408 2160
3 48 60 798 729
1 347 708 1868 792
3 1940 2080 377 1546

Output

No
Yes
No

Note

For the first example, the situations ofKoyomi's actions are illustrated below.


题意:给定一个大小为N*M的矩形和Q次操作,每次操作可以以(r1,c1)->(r2,c2)建立围墙,同样也可以拆除围墙,围墙将矩形划分为若干区域。若干次询问两个点的坐标是否在同一区域内。

分析:肯定是打标记操作了,将每一行单独视为一个序列,拆建围墙只需要将x=r1打上标记i,r2+1打上标记-1

在询问的时候,只需要判断该点所处的那一行,就可以知道其处于第几个区域中,如果区域相同,那么就输出yes

And else 这题的卡常很恶心啊,建议使用读入优化,数组不要开大了,否则绝对会TLE啊

 

Code

#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define dep(i,a,b) for(int i=a;i>=b;i--)
using namespace std;
const int maxn=2506;
int was[maxn][maxn];

inline int read()
{
	int f=1,x=0;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
	return x*f;
}

int main()
{
    int n=read(),m=read(),q=read();
    rep(i,1,q){
    	int t=read(),r1=read(),c1=read(),r2=read(),c2=read();
		if(t==1)rep(r,r1,r2){was[r][c1]=i;was[r][c2+1]=-1;}
		else if(t==2)rep(r,r1,r2)was[r][c1]=was[r][c2+1]=0;
		else{
		    int st=0,res1=0,res2=0;
			dep(i,c1,0)
			    if(was[r1][i]>0)
				    if(st==0){res1=was[r1][i];break;}
				    else st++;
				else if(was[r1][i]<0)st--;
			st=0;
			dep(i,c2,0)
			    if(was[r2][i]>0)
				    if(st==0){res2=was[r2][i];break;}
					else st++;
				else if(was[r2][i]<0)st--; 
			printf("%s",res1==res2?"Yes\n":"No\n");
		}
	}
	return 0;
}


### Codeforces 887E Problem Solution and Discussion The problem **887E - The Great Game** on Codeforces involves a strategic game between two players who take turns to perform operations under specific rules. To tackle this challenge effectively, understanding both dynamic programming (DP) techniques and bitwise manipulation is crucial. #### Dynamic Programming Approach One effective method to approach this problem utilizes DP with memoization. By defining `dp[i][j]` as the optimal result when starting from state `(i,j)` where `i` represents current position and `j` indicates some status flag related to previous moves: ```cpp #include <bits/stdc++.h> using namespace std; const int MAXN = ...; // Define based on constraints int dp[MAXN][2]; // Function to calculate minimum steps using top-down DP int minSteps(int pos, bool prevMoveType) { if (pos >= N) return 0; if (dp[pos][prevMoveType] != -1) return dp[pos][prevMoveType]; int res = INT_MAX; // Try all possible next positions and update 'res' for (...) { /* Logic here */ } dp[pos][prevMoveType] = res; return res; } ``` This code snippet outlines how one might structure a solution involving recursive calls combined with caching results through an array named `dp`. #### Bitwise Operations Insight Another critical aspect lies within efficiently handling large integers via bitwise operators instead of arithmetic ones whenever applicable. This optimization can significantly reduce computation time especially given tight limits often found in competitive coding challenges like those hosted by platforms such as Codeforces[^1]. For detailed discussions about similar problems or more insights into solving strategies specifically tailored towards contest preparation, visiting forums dedicated to algorithmic contests would be beneficial. Websites associated directly with Codeforces offer rich resources including editorials written after each round which provide comprehensive explanations alongside alternative approaches taken by successful contestants during live events. --related questions-- 1. What are common pitfalls encountered while implementing dynamic programming solutions? 2. How does bit manipulation improve performance in algorithms dealing with integer values? 3. Can you recommend any online communities focused on discussing competitive programming tactics? 4. Are there particular patterns that frequently appear across different levels of difficulty within Codeforces contests?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值