(STL之set与multiset)SPOJ - Ada and Field

本篇介绍了一个关于划分田地的问题,通过垂直或水平分割来计算每次分割后最大地块的面积。采用set和multiset数据结构进行高效管理,实现了动态更新最大面积。

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

ADAFIELD - Ada and Field

 

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*109, 1 ≤ 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 Q 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
 
 
 
 
解析:

不了解set与multiset的话会感到有些无从下手,使用之后问题就变得简单。

分别建立set维护行列中线的位置,以及multiset记录行间距、列间距。

每次最大面积就是行间距最大值*列间距最大值(注意这个可能超过int范围)

以插入新的一行为例(显然,列同理)

如果之前没有插入过这一行:

利用lower_bound找到第一个比它高的行(由于初始化时将边界都insert进去,所以必定存在)之前行间距记录的是这一行及比其低的第一行的间距,要更新为插入的这一行与它们的行间距。只要erase原本的行间距,插入新的两个行间距即可。

如果之前插入过:

无需进行任何操作。

#include<bits/stdc++.h>
using namespace std;
set<int>hang,lie;
multiset<int>hlen,llen;
long long an;
typedef long long ll;
int t,n,m,Q;
int len,zuo,you;
int tem,num;

set<int>::iterator p;
multiset<int>::iterator q;

int main()
{
    cin>>t;
    while(t--)
    {
        cin>>n>>m>>Q;
        hang.clear();
        lie.clear();
        hlen.clear();
        llen.clear();
        hang.insert(0);
        hang.insert(n);
        hlen.insert(n);
        lie.insert(0);
        lie.insert(m);
        llen.insert(m);
        while(Q--)
        {
            scanf("%d%d",&tem,&num);
            if(tem==1)
            {
                if(lie.find(num)==lie.end())
                {
                      p=lie.lower_bound(num);
                      you=*p;
                      p--;
                      zuo=*p;
                      q=llen.find(you-zuo);
                      llen.erase(q);
                      len=you-num;
                      llen.insert(len);
                      len=num-zuo;
                      llen.insert(len);
                      lie.insert(num);
                }



            }
            else
            {
                if(hang.find(num)==hang.end())
                {
                      p=hang.lower_bound(num);
                      you=*p;
                      p--;
                      zuo=*p;
                      q=hlen.find(you-zuo);
                      hlen.erase(q);

                      len=you-num;
                      hlen.insert(len);
                      len=num-zuo;
                      hlen.insert(len);
                      hang.insert(num);
                }
            }
            q=llen.end();
            q--;
            an=(ll)(*q);
            q=hlen.end();
            q--;
            an*=(ll)(*q);

            cout<<an<<endl;
        }
    }
}























评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值