Language: C++

#include<bits/stdc++.h>
using namespace std;
#define REP(i,st,ed) for(register int i=st,i##end=ed;i<=i##end;++i)
#define DREP(i,st,ed) for(register int i=st,i##end=ed;i>=i##end;--i)
typedef long long ll;
inline int read(){
	int x;
	char c;
	int f=1;
	while((c=getchar())!='-' && (c>'9' || c<'0'));
	if(c=='-') f=-1,c=getchar();
	x=c^'0';
	while((c=getchar())>='0' && c<='9') x=(x<<1)+(x<<3)+(c^'0');
	return x*f;
}
inline ll readll(){
	ll x;
	char c;
	int f=1;
	while((c=getchar())!='-' && (c>'9' || c<'0'));
	if(c=='-') f=-1,c=getchar();
	x=c^'0';
	while((c=getchar())>='0' && c<='9') x=(x<<1ll)+(x<<3ll)+(c^'0');
	return x*f;
}
const int maxn=1e5+10;
int id1[maxn],id2[maxn],p[maxn];
struct point{
	int l,r;
}a[maxn];
bool cmpr(const int &A,const int &B){
	return a[A].r<a[B].r;
}
bool cmpl(const int &A,const int &B){
	return a[A].l>a[B].l;
}
int main(){
	int n=read();
	REP(i,1,n) a[i].l=read(),a[i].r=read(),id1[i]=id2[i]=i;
	sort(id1+1,id1+n+1,cmpr);
	sort(id2+1,id2+n+1,cmpl);
	ll tmp=0,ans=0;
	int flag=0,i=1,j=1,lst=0;
	REP(k,1,n){
		while(i<=n && p[id1[i]]) ++i;
		while(j<=n && p[id2[j]]) ++j;
		if(flag){
			if(a[id1[i]].r<lst) p[id1[i]]=1,tmp+=lst-a[id1[i]].r,lst=a[id1[i]].r;
		}
		else{
			if(a[id2[j]].l>lst) p[id2[j]]=1,tmp+=a[id2[j]].l-lst,lst=a[id2[j]].l;
		}
		flag^=1;
	}
	tmp+=abs(lst);ans=max(ans,tmp);
	tmp=0;
	flag=1,i=1,j=1,lst=0;
	memset(p,0,sizeof(p));
	REP(k,1,n){
		while(i<=n && p[id1[i]]) ++i;
		while(j<=n && p[id2[j]]) ++j;
		if(flag){
			if(a[id1[i]].r<lst) p[id1[i]]=1,tmp+=lst-a[id1[i]].r,lst=a[id1[i]].r;
		}
		else{
			if(a[id2[j]].l>lst) p[id2[j]]=1,tmp+=a[id2[j]].l-lst,lst=a[id2[j]].l;
		}
		flag^=1;
	}
	tmp+=abs(lst);ans=max(ans,tmp);
	printf("%lld\n",ans);
	return 0;
}
#include<bits/stdc++.h>
using namespace std;
#define REP(i,st,ed) for(register int i=st,i##end=ed;i<=i##end;++i)
#define DREP(i,st,ed) for(register int i=st,i##end=ed;i>=i##end;--i)
typedef long long ll;
inline int read(){
	int x;
	char c;
	int f=1;
	while((c=getchar())!='-' && (c>'9' || c<'0'));
	if(c=='-') f=-1,c=getchar();
	x=c^'0';
	while((c=getchar())>='0' && c<='9') x=(x<<1)+(x<<3)+(c^'0');
	return x*f;
}
inline ll readll(){
	ll x;
	char c;
	int f=1;
	while((c=getchar())!='-' && (c>'9' || c<'0'));
	if(c=='-') f=-1,c=getchar();
	x=c^'0';
	while((c=getchar())>='0' && c<='9') x=(x<<1ll)+(x<<3ll)+(c^'0');
	return x*f;
}
const int maxn=1e5+10;
int id1[maxn],id2[maxn],p[maxn];
struct point{
	int l,r;
}a[maxn];
bool cmpr(const int &A,const int &B){
	return a[A].r<a[B].r;
}
bool cmpl(const int &A,const int &B){
	return a[A].l>a[B].l;
}
int main(){
	int n=read();
	REP(i,1,n) a[i].l=read(),a[i].r=read(),id1[i]=id2[i]=i;
	sort(id1+1,id1+n+1,cmpr);
	sort(id2+1,id2+n+1,cmpl);
	ll tmp=0,ans=0;
	int flag=0,i=1,j=1,lst=0;
	REP(k,1,n){
		while(i<=n && p[id1[i]]) ++i;
		while(j<=n && p[id2[j]]) ++j;
		if(flag){
			if(a[id1[i]].r<lst) p[id1[i]]=1,tmp+=lst-a[id1[i]].r,lst=a[id1[i]].r;
		}
		else{
			if(a[id2[j]].l>lst) p[id2[j]]=1,tmp+=a[id2[j]].l-lst,lst=a[id2[j]].l;
		}
		flag^=1;
	}
	tmp+=abs(lst);ans=max(ans,tmp);
	tmp=0;
	flag=1,i=1,j=1,lst=0;
	memset(p,0,sizeof(p));
	REP(k,1,n){
		while(i<=n && p[id1[i]]) ++i;
		while(j<=n && p[id2[j]]) ++j;
		if(flag){
			if(a[id1[i]].r<lst) p[id1[i]]=1,tmp+=lst-a[id1[i]].r,lst=a[id1[i]].r;
		}
		else{
			if(a[id2[j]].l>lst) p[id2[j]]=1,tmp+=a[id2[j]].l-lst,lst=a[id2[j]].l;
		}
		flag^=1;
	}
	tmp+=abs(lst);ans=max(ans,tmp);
	printf("%lld\n",ans);
	return 0;
}


Compiler output:
a.cc: In function 'int main()':
a.cc:39:13: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   39 |         REP(i,1,n) a[i].l=read(),a[i].r=read(),id1[i]=id2[i]=i;
      |             ^
a.cc:3:39: note: in definition of macro 'REP'
    3 | #define REP(i,st,ed) for(register int i=st,i##end=ed;i<=i##end;++i)
      |                                       ^
a.cc:39:13: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   39 |         REP(i,1,n) a[i].l=read(),a[i].r=read(),id1[i]=id2[i]=i;
      |             ^
a.cc:3:44: note: in definition of macro 'REP'
    3 | #define REP(i,st,ed) for(register int i=st,i##end=ed;i<=i##end;++i)
      |                                            ^
a.cc:44:13: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   44 |         REP(k,1,n){
      |             ^
a.cc:3:39: note: in definition of macro 'REP'
    3 | #define REP(i,st,ed) for(register int i=st,i##end=ed;i<=i##end;++i)
      |                                       ^
a.cc:44:13: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   44 |         REP(k,1,n){
      |             ^
a.cc:3:44: note: in definition of macro 'REP'
    3 | #define REP(i,st,ed) for(register int i=st,i##end=ed;i<=i##end;++i)
      |                                            ^
a.cc:59:13: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   59 |         REP(k,1,n){
      |             ^
a.cc:3:39: note: in definition of macro 'REP'
    3 | #define REP(i,st,ed) for(register int i=st,i##end=ed;i<=i##end;++i)
      |                                       ^
a.cc:59:13: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   59 |         REP(k,1,n){
      |             ^
a.cc:3:44: note: in definition of macro 'REP'
    3 | #define REP(i,st,ed) for(register int i=st,i##end=ed;i<=i##end;++i)
      |                                            ^
a.cc: At global scope:
a.cc:79:12: error: redefinition of 'int read()'
   79 | inline int read(){
      |            ^~~~
a.cc:6:12: note: 'int read()' previously defined here
    6 | inline int read(){
      |            ^~~~
a.cc:89:11: error: redefinition of 'll readll()'
   89 | inline ll readll(){
      |           ^~~~~~
a.cc:16:11: note: 'll readll()' previously defined here
   16 | inline ll readll(){
      |           ^~~~~~
a.cc:99:11: error: redefinition of 'const int maxn'
   99 | const int maxn=1e5+10;
      |           ^~~~
a.cc:26:11: note: 'const int maxn' previously defined here
   26 | const int maxn=1e5+10;
      |           ^~~~
a.cc:100:5: error: redefinition of 'int id1 [100010]'
  100 | int id1[maxn],id2[maxn],p[maxn];
      |     ^~~
a.cc:27:5: note: 'int id1 [100010]' previously declared here
   27 | int id1[maxn],id2[maxn],p[maxn];
      |     ^~~
a.cc:100:15: error: redefinition of 'int id2 [100010]'
  100 | int id1[maxn],id2[maxn],p[maxn];
      |               ^~~
a.cc:27:15: note: 'int id2 [100010]' previously declared here
   27 | int id1[maxn],id2[maxn],p[maxn];
      |               ^~~
a.cc:100:25: error: redefinition of 'int p [100010]'
  100 | int id1[maxn],id2[maxn],p[maxn];
      |                         ^
a.cc:27:25: note: 'int p [100010]' previously declared here
   27 | int id1[maxn],id2[maxn],p[maxn];
      |                         ^
a.cc:101:8: error: redefinition of 'struct point'
  101 | struct point{
      |        ^~~~~
a.cc:28:8: note: previous definition of 'struct point'
   28 | struct point{
      |        ^~~~~
a.cc:103:2: error: conflicting declaration 'int a [100010]'
  103 | }a[maxn];
      |  ^
a.cc:30:2: note: previous declaration as 'point a [100010]'
   30 | }a[maxn];
      |  ^
a.cc:104:6: error: redefinition of 'bool cmpr(const int&, const int&)'
  104 | bool cmpr(const int &A,const int &B){
      |      ^~~~
a.cc:31:6: note: 'bool cmpr(const int&, const int&)' previously defined here
   31 | bool cmpr(const int &A,const int &B){
      |      ^~~~
a.cc:107:6: error: redefinition of 'bool cmpl(const int&, const int&)'
  107 | bool cmpl(const int &A,const int &B){
      |      ^~~~
a.cc:34:6: note: 'bool cmpl(const int&, const int&)' previously defined here
   34 | bool cmpl(const int &A,const int &B){
      |      ^~~~
a.cc:110:5: error: redefinition of 'int main()'
  110 | int main(){
      |     ^~~~
a.cc:37:5: note: 'int main()' previously defined here
   37 | int main(){
      |     ^~~~
a.cc: In function 'int main()':
a.cc:112:13: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
  112 |         REP(i,1,n) a[i].l=read(),a[i].r=read(),id1[i]=id2[i]=i;
      |             ^
a.cc:76:39: note: in definition of macro 'REP'
   76 | #define REP(i,st,ed) for(register int i=st,i##end=ed;i<=i##end;++i)
      |                                       ^
a.cc:112:13: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
  112 |         REP(i,1,n) a[i].l=read(),a[i].r=read(),id1[i]=id2[i]=i;
      |             ^
a.cc:76:44: note: in definition of macro 'REP'
   76 | #define REP(i,st,ed) for(register int i=st,i##end=ed;i<=i##end;++i)
      |                                            ^
a.cc:117:13: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
  117 |         REP(k,1,n){
      |             ^
a.cc:76:39: note: in definition of macro 'REP'
   76 | #define REP(i,st,ed) for(register int i=st,i##end=ed;i<=i##end;++i)
      |                                       ^
a.cc:117:13: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
  117 |         REP(k,1,n){
      |             ^
a.cc:76:44: note: in definition of macro 'REP'
   76 | #define REP(i,st,ed) for(register int i=st,i##end=ed;i<=i##end;++i)
      |                                            ^
a.cc:132:13: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
  132 |         REP(k,1,n){
      |             ^
a.cc:76:39: note: in definition of macro 'REP'
   76 | #define REP(i,st,ed) for(register int i=st,i##end=ed;i<=i##end;++i)
      |                                       ^
a.cc:132:13: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
  132 |         REP(k,1,n){
      |             ^
a.cc:76:44: note: in definition of macro 'REP'
   76 | #define REP(i,st,ed) for(register int i=st,i##end=ed;i<=i##end;++i)
      |                                            ^