There are n points in an array with index from 11to n, and there are two operations to those points.
1: 1 x marking the point x is not available
2: 2 x query for the index of the first available point after that point (including x itself) .
Input
n q
z1 x1
.....
zq xq
q is the number of queries, z is the type of operations, and x is the index of operations. 1≤x<n<10^9, 1< q<10^6and z is 1 or 2
Output
Output the answer for each query.
样例输入复制
5 3 1 2 2 2 2 1
样例输出复制
3 1
因为x的取值有大数,所以用set当作hash,把删除的x都放到set里,表示被标记,然后查找的时候凡是能在set里面找到的就是被标记的,找不到的就是可以输出的。
#include<cstdio>
#include<set>
using namespace std;
set<int> st;
int main(){
int n,q,z,x;
scanf("%d%d",&n,&q);
while(q--){
scanf("%d%d",&z,&x);
if(z==1){
st.insert(x);
}
else{
if(st.find(x)==st.end())
printf("%d\n",x);
else{
do{
x++;
if(x==n) break;
}while(st.find(x)!=st.end());
if(x<n) printf("%d\n",x);
}
}
}
return 0;
}
#include<cstdio>
#include<vector>
using namespace std;
vector<bool> mp(1e9+1,false);
int main(){
int n,q,z,x;
scanf("%d%d",&n,&q);
while(q--){
scanf("%d%d",&z,&x);
if(z==1){
mp[x]=true;
}
else{
if(mp[x]==false)
printf("%d\n",x);
else{
do{
x++;
if(x==n) break;
}while(mp[x]==true);
if(x<n) printf("%d\n",x);
}
}
}
return 0;
}
开头一定要记得用c++编译器,别再因为这个原因罚时。
用set可以降低内存,用vector可以暴力hash 1到1e9.