本人第一次提交时 WA 了两个点,但将数组范围扩大十倍就过了。看了看题解区,有很多题解的数组范围也大于理论应开的 MaxN×4。
WA 代码如下:
#include <cstdio>
#include <algorithm>
#include <cctype>
#include <cstring>
using namespace std;
char buf[1<<21],*p1=buf,*p2=buf;
#define GetC() ((p1==p2)&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
struct Ios{}io;
template <typename _tp>
Ios &operator >>(Ios &in,_tp &x){
x=0;int w=0;char c=GetC();
for(;!isdigit(c);w|=c=='-',c=GetC());
for(;isdigit(c);x=x*10+(c^'0'),c=GetC());
if(w) x=-x;
return in;
}
const int N=20005;
auto ab=[](int x)->int{return x>=0?x:-x;};
#define lc(p) ((p)<<1)
#define rc(p) ((p)<<1|1)
struct Seg_node{
int tag,len;
}tr[N<<2];
void push_up(int p,int l,int r){
if(tr[p].tag){
tr[p].len=r-l;
}
else{
tr[p].len=tr[lc(p)].len+tr[rc(p)].len;
}
}
void modify(int p,int l,int r,int ql,int qr,int k){
if(ql<=l&&r<=qr){
tr[p].tag+=k;
push_up(p,l,r);
return ;
}
int mid=(l+r)>>1;
if(ql<mid) modify(lc(p),l,mid,ql,qr,k);
if(qr>mid) modify(rc(p),mid,r,ql,qr,k);
push_up(p,l,r);
}
struct qwq{
int l,r,h,x;
qwq(int _l=0,int _r=0,int _h=0,int _x=0){l=_l;r=_r;h=_h;x=_x;}
}x[N<<1];
bool operator <(qwq x,qwq y){
if(x.h!=y.h) return x.h<y.h;
return x.x>y.x;
}
int main(){
int n;io>>n;
for(int i=1;i<=n*2;i+=2){
int a,b,c,d;io>>a>>b>>c>>d;
a+=10000;
b+=10000;
c+=10000;
d+=10000;
x[i]=qwq(a,c,b,1);
x[i+1]=qwq(a,c,d,-1);
x[i+(n<<1)]=qwq(b,d,a,1);
x[i+(n<<1)+1]=qwq(b,d,c,-1);
}
n<<=1;
sort(x+1,x+n+1);
int ans=0;
int last=0;
for(int i=1;i<=n;++i){
modify(1,0,20000,x[i].l,x[i].r,x[i].x);
ans+=ab(last-tr[1].len);
last=tr[1].len;
}
for(int i=1;i<=n;++i) x[i]=x[i+n];
sort(x+1,x+n+1);
memset(tr,0,sizeof(tr));
last=0;
for(int i=1;i<=n;++i){
modify(1,0,20000,x[i].l,x[i].r,x[i].x);
ans+=ab(last-tr[1].len);
last=tr[1].len;
}
printf("%d",ans);
return 0;
}
AC 代码:
#include <cstdio>
#include <algorithm>
#include <cctype>
#include <cstring>
using namespace std;
char buf[1<<21],*p1=buf,*p2=buf;
#define GetC() ((p1==p2)&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
struct Ios{}io;
template <typename _tp>
Ios &operator >>(Ios &in,_tp &x){
x=0;int w=0;char c=GetC();
for(;!isdigit(c);w|=c=='-',c=GetC());
for(;isdigit(c);x=x*10+(c^'0'),c=GetC());
if(w) x=-x;
return in;
}
const int N=200005;
auto ab=[](int x)->int{return x>=0?x:-x;};
#define lc(p) ((p)<<1)
#define rc(p) ((p)<<1|1)
struct Seg_node{
int tag,len;
}tr[N<<1];
void push_up(int p,int l,int r){
if(tr[p].tag){
tr[p].len=r-l;
}
else{
tr[p].len=tr[lc(p)].len+tr[rc(p)].len;
}
}
void modify(int p,int l,int r,int ql,int qr,int k){
if(ql<=l&&r<=qr){
tr[p].tag+=k;
push_up(p,l,r);
return ;
}
int mid=(l+r)>>1;
if(ql<mid) modify(lc(p),l,mid,ql,qr,k);
if(qr>mid) modify(rc(p),mid,r,ql,qr,k);
push_up(p,l,r);
}
struct qwq{
int l,r,h,x;
qwq(int _l=0,int _r=0,int _h=0,int _x=0){l=_l;r=_r;h=_h;x=_x;}
}x[N<<1];
bool operator <(qwq x,qwq y){
if(x.h!=y.h) return x.h<y.h;
return x.x>y.x;
}
int main(){
int n;io>>n;
for(int i=1;i<=n*2;i+=2){
int a,b,c,d;io>>a>>b>>c>>d;
a+=10000;
b+=10000;
c+=10000;
d+=10000;
x[i]=qwq(a,c,b,1);
x[i+1]=qwq(a,c,d,-1);
x[i+(n<<1)]=qwq(b,d,a,1);
x[i+(n<<1)+1]=qwq(b,d,c,-1);
}
n<<=1;
sort(x+1,x+n+1);
int ans=0;
int last=0;
for(int i=1;i<=n;++i){
modify(1,0,20000,x[i].l,x[i].r,x[i].x);
ans+=ab(last-tr[1].len);
last=tr[1].len;
}
for(int i=1;i<=n;++i) x[i]=x[i+n];
sort(x+1,x+n+1);
memset(tr,0,sizeof(tr));
last=0;
for(int i=1;i<=n;++i){
modify(1,0,20000,x[i].l,x[i].r,x[i].x);
ans+=ab(last-tr[1].len);
last=tr[1].len;
}
printf("%d",ans);
return 0;
}