CF组赛补题- Shuffle
作者:互联网
You are given an array consisting of nn integers a1a1, a2a2, ..., anan. Initially ax=1ax=1, all other elements are equal to 00.
You have to perform mm operations. During the ii-th operation, you choose two indices cc and dd such that li≤c,d≤rili≤c,d≤ri, and swap acac and adad.
Calculate the number of indices kk such that it is possible to choose the operations so that ak=1ak=1 in the end.
Input
The first line contains a single integer tt (1≤t≤1001≤t≤100) — the number of test cases. Then the description of tt testcases follow.
The first line of each test case contains three integers nn, xx and mm (1≤n≤1091≤n≤109; 1≤m≤1001≤m≤100; 1≤x≤n1≤x≤n).
Each of next mm lines contains the descriptions of the operations; the ii-th line contains two integers lili and riri (1≤li≤ri≤n1≤li≤ri≤n).
Output
For each test case print one integer — the number of indices kk such that it is possible to choose the operations so that ak=1ak=1 in the end.
题目类型:没啥啊类型
解题目标:输出有多少个可能为1的点。
解题思路:不断扩充左边界与右边界,最后输出r-l+1.
AC代码:
#include <bits/stdc++.h>
using namespace std;
int main()
{
int t;
scanf("%d", &t);
while(t -- )
{
int n, m, x;
scanf("%d%d%d", &n, &x, &m);
int l = x, r= x;
int temp1, temp2;
rep(i, 1, m)
{
scanf("%d%d", &temp1, &temp2);
if( l>= temp1 && l<= temp2) l = temp1;
if( r>= temp1 && r<= temp2) r =temp2;
}
cout<<r-l+1<<endl;
}
return 0;
}
标签:operations,d%,Shuffle,int,contains,CF,li,temp1,补题 来源: https://blog.csdn.net/m0_54674275/article/details/121595498