今日SGU 6.6
sgu 177
题意:给你一个一开始全是白色的正方形,边长为n,然后问你经过几次染色之后,最后的矩形里面
还剩多少个白色的块
收获:矩形切割,我们可以这么做,离散处理,对于每次染黑的操作,看看后面有没有染白的矩形和它这个染黑的
矩形有相交,那么我们就把它重合的部分减掉
#include<bits/stdc++.h> #define de(x) cout<<#x<<"="<<x<<endl; #define dd(x) cout<<#x<<"="<<x<<" "; #define rep(i,a,b) for(int i=a;i<(b);++i) #define repd(i,a,b) for(int i=a;i>=(b);--i) #define repp(i,a,b,t) for(int i=a;i<(b);i+=t) #define ll long long #define mt(a,b) memset(a,b,sizeof(a)) #define fi first #define se second #define inf 0x3f3f3f3f #define INF 0x3f3f3f3f3f3f3f3f #define pii pair<int,int> #define pdd pair<double,double> #define pdi pair<double,int> #define mp(u,v) make_pair(u,v) #define sz(a) (int)a.size() #define ull unsigned long long #define ll long long #define pb push_back #define PI acos(-1.0) #define qc std::ios::sync_with_stdio(false) #define db double #define all(a) a.begin(),a.end() const int mod = 1e9+7; const int N = 5e3+6; const double eps = 1e-6; using namespace std; bool eq(const db &a, const db &b) { return fabs(a - b) < eps; } bool ls(const db &a, const db &b) { return a + eps < b; } bool le(const db &a, const db &b) { return eq(a, b) || ls(a, b); } ll gcd(ll a,ll b) { return a==0?b:gcd(b%a,a); }; ll lcm(ll a,ll b) { return a/gcd(a,b)*b; } ll kpow(ll a,ll b) {ll res=1; if(b<0) return 1; for(;b;b>>=1){if(b&1)res=res*a;a=a*a;}return res;} ll read(){ ll x=0,f=1;char ch=getchar(); while (ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while (ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f; } int n,m; int x[N],y[N],y2[N],x2[N],col[N]; int ans; void gao(int d, int xs, int ys, int xe, int ye) { while(d <= m && ( xs > x2[d] || xe < x[d] || ys > y2[d] || ye < y[d] ) ) d++; if(d == m + 1) { ans -= (ye - ys + 1) * (xe - xs + 1); return ; } // 想想被每个后面的矩形的四条边,每条切割完后的剩下什么 int k1 = max(xs, x[d]); if(xs < k1) gao(d + 1, xs, ys, k1 - 1, ye); int k2 = min(xe, x2[d]); if(xe > k2) gao(d + 1, k2 + 1, ys, xe, ye); xs = k1, xe = k2; k1 = max(ys, y[d]); if(ys < k1) gao(d + 1, xs, ys, xe, k1 - 1); k2 = min(ye, y2[d]); if(ye > k2) gao(d + 1, xs, k2 + 1, xe, ye); } int main(){ scanf("%d%d",&n,&m); ans = n * n; rep(i, 1, m+1) { char c; scanf("%d%d%d%d %c",&x[i],&y[i],&x2[i],&y2[i],&c); if(x[i] > x2[i]) swap(x2[i], x[i]); if(y[i] > y2[i]) swap(y2[i], y[i]); col[i] = c == 'b' ? 1 : 0; } repd(i,m,1) if(col[i]) gao(i+1,x[i],y[i],x2[i],y2[i]); printf("%d\n",ans); return 0; }