(STL之set与multiset)SPOJ

来源:互联网 发布:幼儿园虐童 知乎 编辑:程序博客网 时间:2024/05/22 15:55

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*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

210 10 50 50 81 11 91 510 10 50 51 41 61 80 5

Example Output

50504540205030202020
解析:

不了解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;        }    }}




0 0
原创粉丝点击