CF 527C

题目:

每次都会水平或垂直地切割格子,求最大方块面积

题解:

学会使用Map,线段树也可以解这题,不过更慢且更麻烦

#include<iostream>
#include<map>
#include<string>
#include<algorithm>
using namespace std;

long long w,h,times;

map<long long,long long> width;
map<long long,long long> height;

map<long long,long long> len_wid;
map<long long,long long> len_hei;

int main(){
    cin>>w>>h>>times;
    width[w]=0;
    height[h]=0;
    len_wid[w]=1;
    len_hei[h]=1;
    while(times--){
        char name;
        long long x;
        cin>>name>>x;
        if(name=='V'){
            map<long long,long long>::iterator it = width.lower_bound(x);
            long long lef=it->second;
            long long rig=it->first;
            width[rig]=x;
            width[x]=lef;
            len_wid[rig-lef]--;
            if(len_wid[rig-lef]==0) len_wid.erase(rig-lef);
            len_wid[rig-x]++;
            len_wid[x-lef]++;
        }
        else{
            map<long long,long long>::iterator it = height.lower_bound(x);
            long long lef=it->second;
            long long rig=it->first;
            height[rig]=x;
            height[x]=lef;
            len_hei[rig-lef]--;
            if(len_hei[rig-lef]==0) len_hei.erase(rig-lef);
            len_hei[rig-x]++;
            len_hei[x-lef]++;
        }
        long long ans1=0;
        long long ans2=0;
        if(!len_wid.empty()){
            map<long long,long long>::iterator it1=len_wid.end();
            it1--;
            ans1=(it1->first);
        }
        if(!len_hei.empty()){
            map<long long,long long>::iterator it2=len_hei.end();
            it2--;
            ans2=(it2->first);
        }
        cout<<ans1*ans2<<endl;
    }
}

猜你喜欢

转载自blog.csdn.net/mengwuyaaa/article/details/80329266