大神博客:http://www.matrix67.com/blog/archives/108
例题1:VOJ 1056 图形面积
代码:
#include<iostream> #include<cstdio> #include<algorithm> #include<cstring> #include<vector> using namespace std; #define ll long long #define pb push_back #define mp make_pair #define pi acos(-1.0) #define pii pair<int,int> #define mem(a,b) memset(a,b,sizeof(a)) const int MOD=1e9+7; const int INF=0x3f3f3f3f; const int N=205; int x[N],y[N]; int x1[N],x2[N]; int y1[N],y2[N]; int main() { ios::sync_with_stdio(false); cin.tie(0); int n; cin>>n; for(int i=0;i<n;i++) { cin>>x1[i]>>y1[i]>>x2[i]>>y2[i]; x[i*2]=x1[i]; x[i*2+1]=x2[i]; y[i*2]=y1[i]; y[i*2+1]=y2[i]; } sort(x,x+2*n); sort(y,y+2*n); ll s,ans=0; for(int i=0;i<2*n-1;i++) { for(int j=0;j<2*n-1;j++) { for(int k=0;k<n;k++) { if(x1[k]<=x[i]&&x[i+1]<=x2[k]&&y1[k]<=y[j]&&y[j+1]<=y2[k]) { s=(ll)(x[i+1]-x[i])*(y[j+1]-y[j]); ans+=s; //cout<<x[i]<<' '<<x[i+1]<<' '<<y[j]<<' '<<y[j+1]<<endl<<s<<endl; break; } } } } cout<<ans<<endl; return 0; }
例题2:VOJ 1238 容易的网络游戏
注意端点单独处理
#include<bits/stdc++.h> using namespace std; #define ll long long #define pb push_back #define mem(a,b) memset(a,b,sizeof(a)) const int INF=0x3f3f3f3f; const int N=1e3+5; struct node { int l,r; }a[N]; vector<int>t; int main() { ios::sync_with_stdio(false); cin.tie(0); ll n,m,p; cin>>n>>m>>p; p++; ll mn=min(m,p); for(int i=0;i<n;i++) { cin>>a[i].l>>a[i].r; t.pb(a[i].l); t.pb(a[i].r); } sort(t.begin(),t.end()); t.erase(unique(t.begin(),t.end()),t.end()); ll ans=0; int N=t.size(); for(int i=0;i<N;i++) { if(i==0) { ll cnt=0; for(int j=0;j<n;j++) if(a[j].l<=t[i]&&t[i]<=a[j].r)cnt++; ans+=min(mn,cnt); } else { ll cnt=0; for(int j=0;j<n;j++) if(a[j].l<=t[i]&&t[i]<=a[j].r)cnt++; ans+=min(mn,cnt); cnt=0; for(int j=0;j<n;j++) if(a[j].l<=t[i-1]+1&&t[i]-1<=a[j].r)cnt++; ans+=(ll)min(mn,cnt)*(t[i]-t[i-1]-1); } } cout<<ans<<endl; return 0; }
例题3: