分类讨论题,用一个set维护1操作,当2操作来到的时候讨论此时是否满足条件即可。
#include<bits/stdc++.h>
#define int long long
#define double long double
#define x first
#define y second
using namespace std;
typedef long long LL;
typedef long long ll;
typedef pair<int,int> PII;
const int N=3e5+10;
const int M=1e3+10;
int mod=1e9+7;
int a[N];
void solve(){
int n,m;cin>>n>>m;
set<int>st;
int flag=0,cnt=0;
for(int i=1;i<=m;i++){
int op;cin>>op;
if(op==1){
int x;cin>>x;
st.insert(x);
}else{
int x;cin>>x;
if(st.find(x)!=st.end()||cnt>=2||(flag&&flag!=x)){
cout<<i<<"\n";
return ;
}
if(x!=flag){
cnt++;
flag=x;
}
}
if(st.size()==n||(st.find(flag)!=st.end())){
cout<<i<<"\n";
return ;
}
}
cout<<-1<<"\n";
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
int _;
_=1;
//cin>>_;
while(_--){
solve();
}
}

京公网安备 11010502036488号