B. Anton and Classes

来源:互联网 发布:怎样修改淘宝卖家地址 编辑:程序博客网 时间:2024/04/30 01:26

time limit per test
4 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Examples
input
31 52 62 322 46 8
output
3
input
31 52 63 722 41 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.


解题说明:两个时间寻找最大的间隙时间 但是事件的发生时间未知 最大的间隙即用最大的开始时间减去最小的结束时间。


#include<cstdio>#include<algorithm>#include<cstring>#include<cstdlib>#include<iostream>using namespace std;int main(){ int n,m,z1=0,w1=INT_MAX,z2=0,w2=INT_MAX,x,y; cin>>n; for(int i=0;i<n;i++) {  cin>>x>>y;  if(x>z1)  {  z1=x;  }  if(y<w1)  {  w1=y;  } } cin>>m; for(int i=0;i<m;i++) {  cin>>x>>y;  if(x>z2)  {  z2=x;  }  if(y<w2)  {  w2=y;  } } int p=max(z2-w1,z1-w2); if(p>0) { cout<<p<<endl; } else { cout<<0<<endl; } return 0;}


1 0
原创粉丝点击