https://www.luogu.org/problem/show?pid=CF1029C
1 #include2 using namespace std ; 3 #define LL long long 4 5 LL read(){ 6 char c ; 7 int sign = 1 ; 8 while((c = getchar()) > '9' || c < '0') 9 if(c == '-') sign = -1 ;10 LL ans = c - '0' ;11 while((c = getchar()) <='9' && c >= '0')12 ans = ans * 10 + c - '0' ;13 return ans * sign ;14 }15 16 int n ;17 LL l[400000] , r[400000] ;18 LL la = 0 , ra = 10e9 + 1 ;19 20 void answer(){21 la = 0 , ra = 10e9 + 1 ;22 for(int i = 1 ; i <= n ; ++ i){23 la = max(la , l[i]) ;24 ra = min(ra , r[i]) ;25 }26 }27 28 int main(){29 n = read() ;30 LL ansl = 0 , ansr = 0 ;31 r[0] = 10e9 + 1 ;32 l[0] = 0 ;33 for(int i = 1 ; i <= n ; ++ i){34 l[i] = read() , r[i] = read() ;35 }36 answer() ;37 if(la > ra) {38 printf("%d",0);39 return 0 ;40 }41 for(int i = 1 ; i <= n ; ++ i){42 if(l[i] == la){43 if(r[i] < r[ansl]) {44 ansl = i ;45 }46 }47 if(r[i] == ra){48 if(l[i] > l[ansr]){49 ansr = i ;50 }51 }52 }53 if(ansr == ansl){54 l[ansr] = 0 ;55 r[ansr] = 10e9 + 1 ;56 answer() ;57 printf("%ld",ra - la) ;58 return 0 ;59 }60 else {61 LL ii = l[ansr] ;62 LL ff = r[ansr] ;63 l[ansr] = 0 , r[ansr] = 10e9 + 1 ;64 answer() ;65 LL len = ra - la ;66 l[ansr] = ii , r[ansr] = ff ;67 l[ansl] = 0 , r[ansl] = 10e9 + 1 ; 68 answer() ;69 len = max(len , ra - la) ;70 printf("%ld",len) ;71 return 0 ;72 }73 }