#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;
}
cf1782 E - Rectangle Shrinking(贪心)
最新推荐文章于 2024-07-19 22:36:19 发布