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 <bits/stdc++.h>
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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值