백준 23326
set을 이용해서 i번 구역이 명소인 것만 set에 i를 넣음으로써 표시한다.
따로 현재 위치를 now라는 변수를 둬서 계속해서 기록해둔다. 3번에서는
set의 lower_bound 함수를 이용해서 now라는 현재위치에 가장 가까운 명소의 i를 찾아준다. (lower_bound는 이분탐색으로 원소를 탐색해줌)
#include <bits/stdc++.h>
#include<set>
#include<unordered_map>
using namespace std;
int n, q;
int now = 1;
set<int> s;
int main() {
ios::sync_with_stdio(0); cin.tie(0);
cin >> n >> q;
for (int i = 1; i <= n; i++) {
int a;
cin >> a;
if (a) s.insert(i);
}
while (q--) {
int a;
cin >> a;
if (a == 1) {
int b;
cin >> b;
if (s.count(b)) s.erase(b);
else s.insert(b);
}
else if (a == 2) {
int b;
cin >> b;
now = (now + b) % n;
if (now == 0) now = n;
}
else { // a==3
int cnt;
if (s.empty()) {
cout << -1 << '\n';
continue;
}
auto it = s.lower_bound(now);
if (it != s.end()) cnt = *it - now;
else cnt = *s.begin() + n - now;
cout << cnt << '\n';
}
}
}