跳到正文

个人记录

扫描线

Caught_in_the_middle约 4 分钟
原文
文章信息与版本

文章信息

发布
2024年2月23日
收录
2026年8月3日
更新
2026年11月15日
篇幅
约 4 分钟

版本

  1. v1当前
C++
#include<bits/stdc++.h>
using namespace std;
inline int read()
{
	int xx=0,ff=1;
	char ch=getchar();
	while(ch<'0'||ch>'9') {if(ch=='-') ff=-1;ch=getchar();}
	while(ch>='0'&&ch<='9') xx=xx*10+(ch-'0'),ch=getchar();
	return xx*ff;
}
inline void write(int out)
{
 	if(out<0) putchar('-'),out=-out;
	if(out>9) write(out/10);
	putchar(out%10+'0');
}
int n,cnt,cnt1,a[200010],b[200010],ans;
map<int,int> mp,mp1;
struct edge
{
    int x,y_1,y_2,v;
} e[200010],f[200010];
struct XM8
{
    struct node
    {
	    node *ch[2];
	    int val,tag,l,r;
	    node(int aa=0,int bb=0) {l=aa,r=bb,val=tag=0;ch[0]=ch[1]=NULL;}
	    void upd() {if(!tag) val=(ch[0]?ch[0]->val:0)+(ch[1]?ch[1]->val:0);}
	    void trn(int v) {tag+=v;tag?(void)(val=a[r+1]-a[l]):upd();}
	    int mid() {return (l+r)>>1;}
	} *root;
	void build(node *&o,int l,int r)
	{
	    o=new node(l,r);
	    if(l==r) return;
	    build(o->ch[0],l,o->mid()),build(o->ch[1],o->mid()+1,r);
	}
	void lazy(node *o,int l,int r,int v)
	{
	    if(l<=o->l&&r>=o->r) return o->trn(v);
	    if(l<=o->mid()) lazy(o->ch[0],l,r,v);
	    if(r>o->mid()) lazy(o->ch[1],l,r,v);
	    o->upd();
	}
	void _build(int l,int r) {build(root,l,r);}
	void _lazy(int l,int r,int v) {lazy(root,l,r,v);}
	int _query() {return root->val;}
} s;
bool cmp(edge aa,edge bb)
{
	if(aa.x==bb.x) return aa.v>bb.v;
    return aa.x<bb.x;
}
int main()
{
    n=read();
    for(int i=1,a1,a2,b1,b2;i<=n;i++)
    {
	    a1=read(),b1=read(),a2=read(),b2=read();
	    e[++cnt]={a1,b1,b2,1},a[cnt]=b1,e[++cnt]={a2,b1,b2,-1},a[cnt]=b2;
	    f[++cnt1]={b1,a1,a2,1},b[cnt1]=a1,f[++cnt1]={b2,a1,a2,-1},b[cnt1]=a2;
	}
	sort(a+1,a+cnt+1),sort(e+1,e+cnt+1,cmp);
	for(int i=1;i<=cnt;i++) mp[a[i]]=i;
	s._build(1,cnt-1);
	for(int i=1,aa;i<=cnt;i++)
	{
	    aa=s._query();
	    s._lazy(mp[e[i].y_1],mp[e[i].y_2]-1,e[i].v);
	    ans+=abs(s._query()-aa);
	}
	sort(b+1,b+cnt1+1),sort(f+1,f+cnt1+1,cmp);
	for(int i=1;i<=cnt;i++) a[i]=b[i],e[i]=f[i];
	for(int i=1;i<=cnt;i++) mp1[a[i]]=i;
	for(int i=1,aa;i<=cnt;i++)
	{
	    aa=s._query();
	    s._lazy(mp1[e[i].y_1],mp1[e[i].y_2]-1,e[i].v);
	    ans+=abs(s._query()-aa);
	}
	write(ans);
	return 0;
}
原文