spoj ADAFIELD - Ada and Field(水题)

ADAFIELD 是一道关于二分查找和数据结构的题目。Ada 蝴蝶拥有一片菜地,并在上面画了多条垂直或水平线将其划分。题目要求找出每次划分后最大的田地面积。输入包含测试用例数、田地尺寸及线条信息,输出每次划分后的最大田地面积。

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

ADAFIELD - Ada and Field

#binary-search #datastructures

 

Ada the Ladybug owns a beautiful field where she grows vegetables. She often visits local Farmers Market, where she buys new seeds. Since two types of vegetable can't share same field, she always divide the field, by either vertical or horizontal line (she is very precise, so the width of line is negligible). Since she visits Farmers Market almost every day, she has already made a lot of such lines, so she needs your help with finding out the area of biggest field.

Input

The first line will contain 0 < T ≤ 200, the number of test-cases.

Then T test-cases follow, each beginning with three integers 1 ≤ N,M ≤ 2*1091 ≤ Q ≤ 105, top right corner of field (field goes from [0,0] to [N,M]) and number of field divisions.

Afterward Q lines follows:

0 x (0 ≤ x ≤ N), meaning that line was made vertically, on coordinate x

1 y (0 ≤ y ≤ M), meaning that line was made horizontally, on coordinate y

Sum of over all test-cases won't exceed 106

Output

Print Q lines, the area of biggest field after each line was made.

Example Input

2
10 10 5
0 5
0 8
1 1
1 9
1 5
10 10 5
0 5
1 4
1 6
1 8
0 5

Example Output

50
50
45
40
20
50
30
20
20
20
#include<bits/stdc++.h>
using namespace std;
map<long long,long long> dx,dy;
set<long long> lx,ly;
set<long long> ::iterator it,l,r;
map<long long,long long> ::iterator tmp;
int main()
{
	long long t;
	scanf("%lld",&t);
	while(t--)
	{
		lx.clear();ly.clear();
		dx.clear();dy.clear();
		long long n,m,q;
		scanf("%lld%lld%lld",&n,&m,&q);
		lx.insert(0);lx.insert(n);
		ly.insert(0);ly.insert(m);
		dx[n]=1;dy[m]=1;
		for(long long i=0;i<q;i++)
		{
			long long op,c;
			scanf("%lld%lld",&op,&c);
			if(op==0)
			{
				it=lx.find(c);
				if(it==lx.end())
				{
					lx.insert(c);
					it=lx.find(c);
					l=--it;it++;r=++it;it--;
					dx[*r-*l]--;
					if(dx[*r-*l]==0)
						dx.erase(*r-*l);
					tmp=dx.find(*it-*l);
					if(tmp==dx.end())
						dx[*it-*l]=1;
					else
						dx[*it-*l]++;
					tmp=dx.find(*r-*it);
					if(tmp==dx.end())
						dx[*r-*it]=1;
					else
						dx[*r-*it]++;
				}
			}
			else
			{
				it=ly.find(c);
				if(it==ly.end())
				{
					ly.insert(c);
					it=ly.find(c);
					l=--it;it++;r=++it;it--;
					dy[*r-*l]--;
					if(dy[*r-*l]==0)
						dy.erase(*r-*l);
					tmp=dy.find(*it-*l);
					if(tmp==dy.end())
						dy[*it-*l]=1;
					else
						dy[*it-*l]++;
					tmp=dy.find(*r-*it);
					if(tmp==dy.end())
						dy[*r-*it]=1;
					else
						dy[*r-*it]++;
				}
			}
			tmp=dx.end();tmp--;
			long long len=(*tmp).first;
			tmp=dy.end();tmp--;
			long long wid=(*tmp).first;
			long long ans=len*wid;
			printf("%lld\n",ans);	
		}
	}
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值