B. Anton and Classes

题目连接:

http://codeforces.com/contest/785/problem/B

Description

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.

Sample Input

3

1 5

2 6

2 3

2

2 4

6 8

Sample Output

3

Hint

题意

给你n个区间,再给你m个区间,然后从上面区间选出来一个区间,从下面选出来一个区间。

然后问你在这两个区间选出两个点来,这两个点的最近距离是多少。

题解:

显然最远的就是左边界减去右边界的这种情况。

如果都为负数,那么就说明答案显然为0.

代码

#include<bits/stdc++.h>
using namespace std; int l1,r1,l2,r2;
int n,m,a,b;
int main(){
cin>>n;
for(int i=0;i<n;i++){
cin>>a>>b;
if(i==0)l1=a,r1=b;
else{
l1=max(a,l1);
r1=min(b,r1);
}
}
cin>>m;
for(int i=0;i<m;i++){
cin>>a>>b;
if(i==0)l2=a,r2=b;
else{
l2=max(a,l2);
r2=min(b,r2);
}
}
cout<<max(0,max(l1-r2,l2-r1))<<endl;
}

最新文章

  1. 字符串str功能介绍
  2. 三种读写XML的方法
  3. The server does not support version 3.1 of the JEE Web module specification.
  4. ckplayer 参数设置详解
  5. Nginx配置-伪静态,隐藏index.php大入口
  6. 微信小程序 支付功能(前端)的实现
  7. c#中foreach的一种用法
  8. Loadrunner分布式测试
  9. 201521123096《Java程序设计》第八周学习总结
  10. [转]用python 10min手写一个简易的实时内存监控系统
  11. MAC本apache+php配置虚拟域名时踩的坑
  12. Linux 安装 lrzsz,使用 rz、sz 上传下载文件
  13. git 入门教程之基本概念
  14. 使用JSX的注意事项
  15. BZOJ1782[USACO 2010 Feb Gold 3.Slowing down]——dfs+treap
  16. 给sql server2005打补丁报错:无法安装Windows Installer MSP文件
  17. SmartOS之以太网精简协议栈TinyIP
  18. 史上最强网推案例,没有之一【ZW团队实战经典】
  19. 使用Nancy搭建简单的Http服务的示例demo
  20. JMS - ActiveMQ的简单使用

热门文章

  1. HDU 1524 树上无环博弈 暴力SG
  2. javascript沙箱模式
  3. 阿里云Linux服务器安装 nginx+mysql+php
  4. Django进阶之缓存和信号
  5. 关于Mysql5.6半同步主从复制的开启方法【转】
  6. php扩展Redis功能
  7. 如何将java项目转化为web项目
  8. Project Euler Problem3
  9. ckeditor:基本使用方法
  10. javascript-词法分析解析