1843E Tracking Segments 二分+前缀和
[Problem - 1843E - Codeforces]
题面翻译
有一个长度为 n n n,初始全为 0 0 0 的序列。我们定义一个区间是美丽的,当且仅当区间内 1 1 1 的个数严格大于 0 0 0 的个数。现给定 m m m 个区间和 q q q 次修改,每次修改可以将该位置上的 0 0 0 变成 1 1 1。对于每组数据,求出最少第几次修改后,给定的 m m m 个区间中至少一个是美丽的。数据保证每次修改各不相同。
题目描述
You are given an array a a a consisting of n n n zeros. You are also given a set of m m m not necessarily different segments. Each segment is defined by two numbers l i l_i li and r i r_i ri ( 1 ≤ l i ≤ r i ≤ n 1 \le l_i \le r_i \le n 1≤li≤ri≤n) and represents a subarray a l i , a l i + 1 , … , a r i a_{l_i}, a_{l_i+1}, \dots, a_{r_i} ali,ali+1,…,ari of the array a a a.
Let’s call the segment l i , r i l_i, r_i li,ri beautiful if the number of ones on this segment is strictly greater than the number of zeros. For example, if a = [ 1 , 0 , 1 , 0 , 1 ] a = [1, 0, 1, 0, 1] a=[1,0,1,0,1], then the segment [ 1 , 5 ] [1, 5] [1,5] is beautiful (the number of ones is 3 3 3, the number of zeros is 2 2 2), but the segment [ 3 , 4 ] [3, 4] [3,4] is not is beautiful (the number of ones is 1 1 1, the number of zeros is 1 1 1).
You also have q q q changes. For each change you are given the number 1 ≤ x ≤ n 1 \le x \le n 1≤x≤n, which means that you must assign an element a x a_x ax the value 1 1 1.
You have to find the first change after which at least one of m m m given segments becomes beautiful, or report that none of them is beautiful after processing all q q q changes.
输入格式
The first line contains a single integer t t t ( 1 ≤ t ≤ 1 0 4 1 \le t \le 10^4 1≤t≤104) — the number of test cases.
The first line of each test case contains two integers n n n and m m m ( 1 ≤ m ≤ n ≤ 1 0 5 1 \le m \le n \le 10^5 1≤m≤n≤105) — the size of the array a a a and the number of segments, respectively.
Then there are m m m lines consisting of two numbers l i l_i li and r i r_i ri ( 1 ≤ l i ≤ r i ≤ n 1 \le l_i \le r_i \le n 1≤li≤ri≤n) —the boundaries of the segments.
The next line contains an integer q q q ( 1 ≤ q ≤ n 1 \le q \le n 1≤q≤n) — the number of changes.
The following q q q lines each contain a single integer x x x ( 1 ≤ x ≤ n 1 \le x \le n 1≤x≤n) — the index of the array element that needs to be set to 1 1 1. It is guaranteed that indexes in queries are distinct.
It is guaranteed that the sum of n n n for all test cases does not exceed 1 0 5 10^5 105.
输出格式
For each test case, output one integer — the minimum change number after which at least one of the segments will be beautiful, or − 1 -1 −1 if none of the segments will be beautiful.
样例 #1
样例输入 #1
6
5 5
1 2
4 5
1 5
1 3
2 4
5
5
3
1
2
4
4 2
1 1
4 4
2
2
3
5 2
1 5
1 5
4
2
1
3
4
5 2
1 5
1 3
5
4
1
2
3
5
5 5
1 5
1 5
1 5
1 5
1 4
3
1
4
3
3 2
2 2
1 3
3
2
3
1
样例输出 #1
3
-1
3
3
3
1
提示
In the first case, after first 2 changes we won’t have any beautiful segments, but after the third one on a segment [ 1 ; 5 ] [1; 5] [1;5] there will be 3 ones and only 2 zeros, so the answer is 3.
In the second case, there won’t be any beautiful segments.
AC
#include <bits/stdc++.h>
using namespace std;
const int N = 100010;
int n, m, q, x[N], s[N];
struct node
{
int l, r;
}a[N];
int check(int u)
{
for(int i=1; i<=n; i++) s[i]=0;
for(int i=1; i<=u; i++) s[x[i]]++;
for(int i=1; i<=n; i++) s[i]+=s[i-1];
for(int i=1; i<=m; i++)
if(2*(s[a[i].r]-s[a[i].l-1]) > a[i].r-a[i].l+1)
return 1;
return 0;
}
void solve()
{
cin>>n>>m;
for(int i=1; i<=m; i++) cin>>a[i].l>>a[i].r;
cin>>q;
for(int i=1; i<=q; i++) cin>>x[i];
int l=1, r=q, ans=-1;
while(l<=r)
{
int mid=(l+r)/2;
if(check(mid))
{
ans=mid;
r=mid-1;
}
else l=mid+1;
}
cout<<ans<<endl;
return ;
}
int main()
{
int t; cin>>t;
while(t--) solve();
return 0;
}