题目:
见https://www.luogu.com.cn/problem/P1884
思路:
见https://www.luogu.com.cn/blog/shik2005/p1884-usaco12feb-guo-du-zhong-zhi-yin-overplanting-ti-xie
和https://blog.csdn.net/u012746396/article/details/41280403
总结说来就是:左大右小上小下大
还有洛谷大佬用的是离散化+线段树,但是由于本人太菜目前还没学线段树,以后再说!
代码:
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
using namespace std;
typedef long long ll;
const int N=1e3+10;
struct node
{
ll x1,y1,x2,y2;
}a[N];
int n,tot;
void add(ll x1,ll y1,ll x2,ll y2)
{
a[++tot]=(node){x1,y1,x2,y2};//第一次见这样给结构体赋值,神奇!!!
}
void cut(ll x1,ll y1,ll x2,ll y2,int now)
{
ll k1=max(x1,a[now].x1);
ll k2=min(x2,a[now].x2);
ll m1=min(y1,a[now].y1);
ll m2=max(y2,a[now].y2);
if(k1>a[now].x1) add(a[now].x1,a[now].y1,k1,a[now].y2);
if(k2<a[now].x2) add(k2,a[now].y1,a[now].x2,a[now].y2);
if(m1<a[now].y1) add(k1,a[now].y1,k2,m1);//这个位置参数需要格外注意
if(m2>a[now].y2) add(k1,m2,k2,a[now].y2);//这个位置参数需要格外注意
}
int main()
{
cin>>n;
ll x1,y1,x2,y2;
cin>>x1>>y1>>x2>>y2;
add(x1,y1,x2,y2);//先读入第一个矩形
for(int j=2;j<=n;j++)
{
cin>>x1>>y1>>x2>>y2;
for(int i=1;i<=tot;i++)
{
if(x2<=a[i].x1||x1>=a[i].x2||y2>=a[i].y1||y1<=a[i].y2) continue;//如果需要切割就进行切割
cut(x1,y1,x2,y2,i);
a[i]=a[tot];//切割完后将当前最后一个矩形赋给已经被切了的矩形
tot--;//矩形总数减1
i--;//i--
}
add(x1,y1,x2,y2);//纳入新矩形
}
ll ans=0;
for(int i=1;i<=tot;i++)
ans+=abs(a[i].x1-a[i].x2)*abs(a[i].y1-a[i].y2);//累加答案
printf("%lld\n",ans);
return 0;
}