1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
| #include <iostream> #include <cstdio> #include <vector> #include <algorithm>
#define LL long long using namespace std;
const int MAXN=5e3+2; const int INF=0x7fffffff;
struct line_t{ int l,r,h,mark;
line_t(int _l,int _r,int _h,int _mark):l(_l),r(_r),h(_h),mark(_mark){}
bool operator<(const line_t& o)const{ if(h==o.h)return mark>o.mark; return h<o.h; } };
vector<line_t> lines;
int n,cnt,rt;
#define ls d[p].lson #define rs d[p].rson #define MID int m=1ll*s+((1ll*t-s)>>1)
struct node_t{ int lson,rson,tag; int c; bool lc,rc; int len; }d[MAXN<<7];
void pushup(int p,int s,int t){ if(d[p].tag){ d[p].len=t-s+1; d[p].lc=d[p].rc=true; d[p].c=1; }else{ d[p].len=d[ls].len+d[rs].len; d[p].lc=d[ls].lc,d[p].rc=d[rs].rc; d[p].c=d[ls].c+d[rs].c; if(d[ls].rc&&d[rs].lc){ d[p].c-=1; } } }
void modify(int& p,int s,int t,int l,int r,int c){ if(!p)p=++cnt; if(l<=s&&t<=r){ d[p].tag+=c; pushup(p,s,t); return; } MID; if(l<=m)modify(ls,s,m,l,r,c); if(r>m)modify(rs,m+1,t,l,r,c); pushup(p,s,t); }
int main(){ cin>>n; for(int i=1,x,y,_x,_y;i<=n;++i){ cin>>x>>y>>_x>>_y; lines.push_back({x,_x,y,1}); lines.push_back({x,_x,_y,-1}); } sort(lines.begin(),lines.end());
LL ans=0; int pre=0;
for(int i=0;i<lines.size()-1;++i){ modify(rt,-INF,INF,lines[i].l,lines[i].r-1,lines[i].mark); ans+=abs(d[1].len-pre); ans+=2*d[1].c*(lines[i+1].h-lines[i].h); pre=d[1].len; } ans+=lines.back().r-lines.back().l;
cout<<ans<<'\n'; return 0; }
|