You are given an array consisting of zeros. You are also given a set of not necessarily different segments. Each segment is defined by two numbers and ( ) and represents a subarray of the array .
Let's call the segment beautiful if the number of ones on this segment is strictly greater than the number of zeros. For example, if , then the segment is beautiful (the number of ones is , the number of zeros is ), but the segment is not is beautiful (the number of ones is , the number of zeros is ).
You also have changes. For each change you are given the number , which means that you must assign an element the value .
You have to find the first change after which at least one of given segments becomes beautiful, or report that none of them is beautiful after processing all changes.
## 输入格式
The first line contains a single integer ( ) — the number of test cases.
The first line of each test case contains two integers and ( ) — the size of the array and the number of segments, respectively.
Then there are lines consisting of two numbers and ( ) —the boundaries of the segments.
The next line contains an integer ( ) — the number of changes.
The following lines each contain a single integer ( ) — the index of the array element that needs to be set to . It is guaranteed that indexes in queries are distinct.
It is guaranteed that the sum of for all test cases does not exceed .
## 输出格式
For each test case, output one integer — the minimum change number after which at least one of the segments will be beautiful, or if none of the segments will be beautiful.
In the first case, after first 2 changes we won't have any beautiful segments, but after the third one on a segment 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.
#include <bits/stdc++.h>
#define int long long
usingnamespace std;
constint N=1e6+10,mod=1e9+7;
int n,t,a[N],f[N],res,num,ans,m,ll[N],rr[N],q,s[N];
bool vis[N];
bool check(int u)
{
for(int i=1;i<=n;i++) s[i]=0,f[i]=0;
for(int i=1;i<=u;i++) f[a[i]]=1;
for(int i=1;i<=n;i++) s[i]=s[i-1]+f[i];
for(int i=1;i<=m;i++){
if((s[rr[i]]-s[ll[i]-1])*2>(rr[i]-ll[i])+1) returntrue;
}
returnfalse;
}
signed main()
{
std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
cin>>t;
while(t--){
cin>>n>>m;
for(int i=1;i<=m;i++) cin>>ll[i]>>rr[i];
cin>>q;
for(int i=1;i<=q;i++) cin>>a[i];
int l=1,r=q+1,flag=0;
while(l<r){
int mid=l+r>>1;
if(check(mid)) r=mid,flag=1;
else l=mid+1;
}
if(!flag) cout<<-1<<endl;
else cout<<r<<endl;
}
return0;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)