【codeforces 527C】Glass Carving

【题目链接】:http://codeforces.com/contest/527/problem/C

【题意】

让你切割一个长方形;
只能横切或竖切;
让你实时输出切完之后最大的长方形的面积;

【题解】

这里最大的长方形的面积对应的就是最大的竖线间隔*最大的横线间隔;
我们先把所有的边读入->存起来
然后分成横线和竖线两类;
按照位置从小到大排个序;
然后先求出第n个询问的答案
也即这个时候竖线的最大间隔和横线的最大间隔;
然后离线处理;
逆序删掉第i条线
然后维护这个时候最大的横线和竖线间隔;
因为只会删掉一条边,
所以只要考虑这条边左边最靠近它的线和它右边最靠近它的线;
看看它们俩的差值能不能更新横线或者是竖线的最大间隔;
用一个类似链表的东西模拟删除的过程就好;

【完整代码】

#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define ps push_back
#define fi first
#define se second
#define rei(x) scanf("%d",&x)
#define rel(x) scanf("%lld",&x)
#define ref(x) scanf("%lf",&x)

typedef pair<int, int> pii;
typedef pair<LL, LL> pll;

const int dx[9] = { 0,1,-1,0,0,-1,-1,1,1 };
const int dy[9] = { 0,0,0,-1,1,-1,1,-1,1 };
const double pi = acos(-1.0);
const int N = 2e5+100;

struct abc
{
    LL pos;
    int l,r;
};

LL w, h,x,gap_heng,gap_shu, pp[N];
int n,sshu,sheng;
char p[N][5];
vector <abc> shu, heng;
vector <LL> ans;
abc temp;

bool cmp(abc a, abc b)
{
    return a.pos < b.pos;
}

int main()
{
    //freopen("F:\rush.txt", "r", stdin);
    rel(w), rel(h), rei(n);
    rep1(i, 1, n)
    {
        scanf("%s", p[i]); rel(pp[i]);
        temp.pos = pp[i];
        if (p[i][0] == 'V')
            shu.ps(temp);
        else
            heng.ps(temp);
    }
    temp.pos = 0; shu.ps(temp); heng.ps(temp);
    temp.pos = w; shu.ps(temp); temp.pos = h, heng.ps(temp);
    sort(shu.begin(), shu.end(),cmp),sort(heng.begin(), heng.end(),cmp);
    sshu = shu.size();
    rep1(i, 1, sshu - 2)
    {
        shu[i].l = i - 1;
        shu[i].r = i + 1;
    }
    rep1(i, 1, sshu - 1)
    {
        gap_shu = max(gap_shu, shu[i].pos - shu[i - 1].pos);
    }

    sheng = heng.size();
    rep1(i, 1, sheng - 2)
    {
        heng[i].l = i - 1;
        heng[i].r = i + 1;
    }
    rep1(i, 1, sheng - 1)
    {
        gap_heng = max(gap_heng, heng[i].pos - heng[i - 1].pos);
    }

    rep2(i, n, 1)
    {
        ans.ps(gap_heng*gap_shu);
        if (p[i][0] == 'V')
        {
            temp.pos = pp[i];
            int pos = lower_bound(shu.begin(), shu.end(), temp,cmp) - shu.begin();
            int l = shu[pos].l, r = shu[pos].r;
            gap_shu = max(gap_shu, shu[r].pos - shu[l].pos);
            shu[l].r = r;
            shu[r].l = l;
        }
        else
        {
            temp.pos = pp[i];
            int pos = lower_bound(heng.begin(), heng.end(), temp,cmp) - heng.begin();
            int l = heng[pos].l, r = heng[pos].r;
            gap_heng = max(gap_heng, heng[r].pos - heng[l].pos);
            heng[l].r = r;
            heng[r].l = l;
        }
    }
    rep2(i, n - 1, 0)
    {
        printf("%lld
", ans[i]);
    }
    //printf("
%.2lf sec 
", (double)clock() / CLOCKS_PER_SEC);
    return 0;
}
原文地址:https://www.cnblogs.com/AWCXV/p/7626506.html