cf1782 E - Rectangle Shrinking(贪心)

传送门

#include <bits/stdc++.h>

using namespace std;

#define endl "\n"
#define debug cout<<"debug"<<endl
mt19937 rd(time(0));
typedef long long ll;
typedef long double ld;
typedef pair<int, int> PII;
const double eps = 1e-8;
const double PI = 3.14159265358979323;
const int N = 2e5+10, M = 2*N, mod = 1e9+7;
const int INF = 0x3f3f3f3f;

int n;
struct Node{
    int u, d, l, r;
}c[N];
vector<int> s;

void solve()
{
    cin>>n;
    for(int i = 1; i<=n; i++) cin>>c[i].u>>c[i].l>>c[i].d>>c[i].r;

    // u == 1 && d == 2 占两行的情况
    s.clear();
    for(int i = 1; i<=n; i++)
        if(c[i].u == 1 && c[i].d == 2) s.push_back(i);
    sort(s.begin(), s.end(), [&](int a, int b)
    {
        return c[a].l == c[b].l ? c[a].r < c[b].r : c[a].l < c[b].l;
    });
    int r = 0;
    for(auto t : s)
    {
        if(c[t].r <= r) c[t] = {0, 0, 0, 0};
        else c[t].l = max(c[t].l, r+1), r = c[t].r;
    }

    // u = 1, 占一行的情况之一
    s.clear();
    for(int i = 1; i<=n; i++)
        if(c[i].u == 1) s.push_back(i);
    sort(s.begin(), s.end(), [&](int a, int b)
    {
        return c[a].l == c[b].l ? c[a].r < c[b].r : c[a].l < c[b].l;
    });
    r = 0;
    for(int i = 0, j = 0; i<s.size(); i++)
    {
        int t = s[i];
        if(c[t].d == 2) // 若是两行
        {
            if(r>=c[t].r) c[t].u = 2; // 删掉u = 1的这一行
            else{
                while(j<i)
                {
                    // 将所有右端点大于当前区间左端点的改到当前区间左端点之前
                    if(c[s[j]].r>=c[t].l) c[s[j]].r = c[t].l - 1;
                    j++;
                }
                r = c[t].r;
            }
        }else
        {
            if(c[t].r <= r) c[t] = {0, 0, 0, 0};
            else c[t].l = max(c[t].l, r+1), r = c[t].r;
        }
    }

    // d = 2, 占一行的情况之一
    s.clear();
    for(int i = 1; i<=n; i++)
        if(c[i].d == 2) s.push_back(i);
    sort(s.begin(), s.end(), [&](int a, int b)
    {
        return c[a].l == c[b].l ? c[a].r < c[b].r : c[a].l < c[b].l;
    });
    r = 0;
    for(int i = 0, j = 0; i<s.size(); i++)
    {
        int t = s[i];
        if(c[t].u == 1) // 若是两行
        {
            if(r>=c[t].r) c[t].d = 1; // 删掉u = 1的这一行
            else{
                while(j<i)
                {
                    // 将所有右端点大于当前区间左端点的改到当前区间左端点之前
                    if(c[s[j]].r>=c[t].l) c[s[j]].r = c[t].l - 1;
                    j++;
                }
                r = c[t].r;
            }
        }else
        {
            if(c[t].r <= r) c[t] = {0, 0, 0, 0};
            else c[t].l = max(c[t].l, r+1), r = c[t].r;
        }
    }

    // 计算面积
    int sum = 0;
    for(int i = 1; i<=n; i++)
    {
        if(c[i].u > c[i].d || c[i].l > c[i].r) c[i] = {0,0,0,0};
        if(c[i].u) sum += (c[i].d - c[i].u + 1)*(c[i].r - c[i].l + 1);
    }
    cout<<sum<<endl;
    for(int i = 1; i<=n; i++) cout<<c[i].u<<" "<<c[i].l<<" "<<c[i].d<<" "<<c[i].r<<endl;
}

int main()
{
    ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
    int T;
    T = 1;
    cin>>T;
    while(T -- )
    {
        solve();
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值