B. Game with Doors

time limit per test

2 seconds

memory limit per test

256 megabytes

There are 100100 rooms arranged in a row and 9999 doors between them; the ii-th door connects rooms ii and i+1i+1. Each door can be either locked or unlocked. Initially, all doors are unlocked.

We say that room xx is reachable from room yy if all doors between them are unlocked.

You know that:

  • Alice is in some room from the segment [l,r][l,r];
  • Bob is in some room from the segment [L,R][L,R];
  • Alice and Bob are in different rooms.

However, you don't know the exact rooms they are in.

You don't want Alice and Bob to be able to reach each other, so you are going to lock some doors to prevent that. What's the smallest number of doors you have to lock so that Alice and Bob cannot meet, regardless of their starting positions inside the given segments?

Input

The first line contains a single integer tt (1≤t≤1041≤t≤104) — the number of test cases.

The first line of each test case contains two integers ll and rr (1≤l<r≤1001≤l<r≤100) — the bounds of the segment of rooms where Alice is located.

The second line of each test case contains two integers LL and RR (1≤L<R≤1001≤L<R≤100) — the bounds of the segment of rooms where Bob is located.

Output

For each test case, print a single integer — the smallest number of doors you have to lock so that Alice and Bob cannot meet, regardless of their starting positions inside the given segments.

Example

Input

Copy

 

4

1 2

3 4

2 5

2 5

3 7

6 7

4 5

2 8

Output

Copy

1
3
2
3

Note

In the first test case, it is sufficient to lock the door between rooms 22 and 33.

In the second test case, the following doors have to be locked: (2,3)(2,3), (3,4)(3,4), (4,5)(4,5).

In the third test case, the following doors have to be locked: (5,6)(5,6) and (6,7)(6,7).

解题说明:此题是一道数学题,能发现其实只需要求出两个区间是否存在交集,然后把交集部分中间的门全部锁上即可,注意相等的情况。如果不存在交集,那么只需要锁一扇。

#include <stdio.h>
int main()
{
	int t;
	scanf("%d", &t);
	for (int i = 0; i < t; i++) 
	{
		int l, r, L, R;
		scanf("%d %d", &l, &r);
		scanf("%d %d", &L, &R);
		if (r < L || R < l) 
		{
			printf("1\n");
		}
		else
		{
			int res = (r < R ? r : R) - (l > L ? l : L);
			if (r != R)
			{
				res++;
			}
			if (l != L)
			{
				res++;
			}
			printf("%d\n", res);
		}
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值