题目:
Anton likes to play chess. Also he likes to do programming. No wonder that he decided to attend chess classes and programming classes.
Anton has n variants when he will attend chess classes, i-th variant is given by a period of time (l1, i, r1, i). Also he has m variants when he will attend programming classes, i-th variant is given by a period of time (l2, i, r2, i).
Anton needs to choose exactly one of n possible periods of time when he will attend chess classes and exactly one of m possible periods of time when he will attend programming classes. He wants to have a rest between classes, so from all the possible pairs of the periods he wants to choose the one where the distance between the periods is maximal.
The distance between periods (l1, r1) and (l2, r2) is the minimal possible distance between a point in the first period and a point in the second period, that is the minimal possible |i - j|, where l1 ≤ i ≤ r1 and l2 ≤ j ≤ r2. In particular, when the periods intersect, the distance between them is 0.
Anton wants to know how much time his rest between the classes will last in the best case. Help Anton and find this number!
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 200 000) — the number of time periods when Anton can attend chess classes.
Each of the following n lines of the input contains two integers l1, i and r1, i (1 ≤ l1, i ≤ r1, i ≤ 109) — the i-th variant of a period of time when Anton can attend chess classes.
The following line of the input contains a single integer m (1 ≤ m ≤ 200 000) — the number of time periods when Anton can attend programming classes.
Each of the following m lines of the input contains two integers l2, i and r2, i (1 ≤ l2, i ≤ r2, i ≤ 109) — the i-th variant of a period of time when Anton can attend programming classes.
Output
Output one integer — the maximal possible distance between time periods.
Example
Input
3
1 5
2 6
2 3
2
2 4
6 8
Output
3
Input
3
1 5
2 6
3 7
2
2 4
1 4
Output
0
Note
In the first sample Anton can attend chess classes in the period (2, 3) and attend programming classes in the period (6, 8). It’s not hard to see that in this case the distance between the periods will be equal to 3.
In the second sample if he chooses any pair of periods, they will intersect. So the answer is 0.
题意:
给你n个区间,和m个区间。求两个区间的最大值。
分析:
直接记录下棋的最大的开始时间和最小的结束时间。同理记录编程的。然后check一下哪一个大就好了。一开始看错题,以为下棋必须在编程前面,wa了好多发。
代码:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
using namespace std ;
const int INF = 0x3f3f3f3f ;
int main(){
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
long long ans ;
int n , m ;
cin>>n;
long long x,y , endTime1 = INF ,endTime2 = INF , begTime2 = 0 , begTime1 = 0 ;
while(n--){
cin>>x>>y;
//cout<<x<<" "<<y<<endl;
if(y<endTime1) endTime1 = y ;
if(x>begTime1) begTime1 = x ;
//cout<<"endtime="<<endTime1<<endl;
//cout<<"begtime="<<begTime1<<endl;
}
cin>>m;
while(m--){
cin>>x>>y;
//cout<<x<<" "<<y<<endl;
if(x>begTime2) begTime2 = x ;
if(y<endTime2) endTime2 = y ;
//cout<<"endtime="<<endTime2<<endl;
//cout<<"begtime="<<begTime2<<endl;
}
if(endTime1>begTime2&&endTime2>begTime1) printf("0\n");
else{
ans = max((begTime1-endTime2),(begTime2-endTime1));
printf("%I64d\n",ans);
}
return 0 ;
}