Codeforces Educational Round89div2

Codeforces Educational Round89div2

原题:

You are given an array consisting of n integers a1, a2, …, an. Initially ax=1, all other elements are equal to 0.

You have to perform m operations. During the i-th operation, you choose two indices c and d such that li≤c,d≤ri, and swap ac and ad.

Calculate the number of indices k such that it is possible to choose the operations so that ak=1 in the end.

Input
The first line contains a single integer t (1≤t≤100) — the number of test cases. Then the description of t testcases follow.

The first line of each test case contains three integers n, x and m (1≤n≤109; 1≤m≤100; 1≤x≤n).

Each of next m lines contains the descriptions of the operations; the i-th line contains two integers li and ri (1≤li≤ri≤n).

Output
For each test case print one integer — the number of indices k such that it is possible to choose the operations so that ak=1 in the end.

思路:找能覆盖x的最大区域

ACcodes:

#include 
using namespace std;
const int N = 1e9+10;
int test=1;

int main()
{
	scanf("%d",&test);
	while(test--)
	{
		int n,x,m;scanf("%d%d%d",&n,&x,&m);
		int l[105],r[105],L=0,R=0;
		for(int i=1;i<=m;i++)
		{
			scanf("%d%d",&l[i],&r[i]);
		}
		for(int i=1;i<=m;i++)
		{
			if(!L&&!R)
			{
				if(l[i]<=x&&r[i]>=x)
				{
					L=l[i];
					R=r[i];
				}
			}
			else
			{
				if(!(r[i]<L) && !(l[i]>R))
				{
					L = min(L,l[i]);
					R = max(R,r[i]);
				}
			}
		}
		cout << R-L+1 << endl;
		
	}

	return 0;
}

你可能感兴趣的:(Codeforces)