NOIP 模拟 $34\; \rm Rectangle$
题解 \(by\;zj\varphi\)
对于没有在同一行或同一列的情况,直接枚举右边界,左边界从大到小,用树状数组维护上下边界即可。
而对于有多个在一列或一行的情况,这些点将左右分成了几个区间,枚举上边界在哪个区间,同时维护下边界。
Code
#include<bits/stdc++.h>
#define ri register signed
#define p(i) ++i
namespace IO{
char buf[1<<21],*p1=buf,*p2=buf;
#define gc() p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?(-1):*p1++
struct nanfeng_stream{
template<typename T>inline nanfeng_stream operator>>(T &x) {
ri f=0;x=0;register char ch=gc();
while(!isdigit(ch)) f|=ch=='-',ch=gc();
while(isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48),ch=gc();
return x=f?-x:x,*this;
}
}cin;
}
using IO::cin;
namespace nanfeng{
#define Node(x,y) (Node){x,y}
#define FI FILE *IN
#define FO FILE *OUT
template<typename T>inline T cmax(T x,T y) {return x>y?x:y;}
template<typename T>inline T cmin(T x,T y) {return x>y?y:x;}
typedef long long ll;
static const int N=1e5+7,MOD=1e9+7;
int cut[N],Y[N],vis[N],X[N],cnt,xm,tot,n;
ll ans;
struct node{int x,y;}pnt[N];
struct Node{ll x,y;};
inline bool operator<(const node &n1,const node &n2) {return n1.x==n2.x?n1.y<n2.y:n1.x>n2.x;}
struct BIT{
#define lowbit(x) ((x)&-(x))
int c[2507],b[2507];
inline void init() {
memset(c,0,sizeof(int)*(xm+7));
memset(b,0,sizeof(int)*(xm+7));
}
inline void update(int x,int k) {for (ri i(x);i<=xm;i+=lowbit(i)) c[i]+=k,p(b[i]);}
inline Node query(int x) {
register ll res(0),nm(0);
for (ri i(x);i;i-=lowbit(i)) res+=c[i],nm+=b[i];
return Node(res,nm);
}
}B;
inline int main() {
//FI=freopen("nanfeng.in","r",stdin);
//FO=freopen("nanfeng.out","w",stdout);
cin >> n;
for (ri i(1);i<=n;p(i)) cin >> pnt[i].x >> pnt[i].y,xm=cmax(xm,pnt[i].y);
std::sort(pnt+1,pnt+n+1);
cut[p(tot)]=1;
X[tot]=pnt[1].x;
for (ri i(1);i<=n;p(i)) {
if (pnt[i].x!=pnt[i-1].x&&i!=1) Y[p(cnt)]=xm+1,cut[p(tot)]=cnt+1,X[tot]=pnt[i].x;
Y[p(cnt)]=pnt[i].y;
}
Y[p(cnt)]=xm+1;
cut[p(tot)]=cnt+1;
for (ri i(1);i<tot;p(i)) {
register ll tmp=ans;
B.init();
memset(vis,0,sizeof(int)*(xm+7));
for (ri j(cut[i]);j<cut[i+1]-1;p(j))
if (!vis[Y[j]]) B.update(Y[j],Y[j]),vis[Y[j]]=1;
for (ri j(i+1);j<tot;p(j)) {
for (ri k(cut[j]);k<cut[j+1]-1;p(k))
if (!vis[Y[k]]) B.update(Y[k],Y[k]),vis[Y[k]]=1;
ri l1(cut[i]),l2(cut[j]);
ri mn(cmax(Y[l1],Y[l2]));
while(Y[l1+1]<=mn) p(l1);
while(Y[l2+1]<=mn) p(l2);
while(l1<cut[i+1]-1&&l2<cut[j+1]-1) {
int tmp=cmin(Y[l1+1],Y[l2+1]);
register Node tp1=B.query(tmp-1),tp2=B.query(mn-1),tp3=B.query(cmin(Y[l1],Y[l2]));
mn=tmp;
ans=(ans+(ll)(X[i]-X[j])*((tp1.x-tp2.x)*tp3.y-(tp1.y-tp2.y)*tp3.x))%MOD;
if (Y[l1+1]<=mn) p(l1);
if (Y[l2+1]<=mn) p(l2);
}
}
}
printf("%lld\n",ans);
return 0;
}
}
int main() {return nanfeng::main();}