verify/yosupo/data-structure/predecessor_problem.test.cpp
Depends on
Code
#define PROBLEM "https://judge.yosupo.jp/problem/predecessor_problem"
#include "template.hpp"
#include "data-structure/fast-set.hpp"
int main (){
cin . tie ( nullptr ) -> sync_with_stdio ( false );
int n , q ;
cin >> n >> q ;
string t ;
cin >> t ;
FastSet fs ( n );
for ( int i = 0 ; i < n ; i ++ ) if ( t [ i ] == '1' ) fs . insert ( i );
while ( q -- ){
int op , k ;
cin >> op >> k ;
if ( op == 0 ){
fs . insert ( k );
} else if ( op == 1 ){
fs . erase ( k );
} else if ( op == 2 ){
cout << fs . count ( k ) << " \n " ;
} else if ( op == 3 ){
int x = fs . next ( k );
if ( x == n ) x =- 1 ;
cout << x << " \n " ;
} else {
cout << fs . prev ( k ) << " \n " ;
}
}
}
#line 1 "verify/yosupo/data-structure/predecessor_problem.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/predecessor_problem"
#line 2 "template.hpp"
#include <bits/stdc++.h>
using namespace std ;
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define mt make_tuple
#define fi first
#define se second
#define ALL(a) a.begin(),a.end()
#define RALL(a) a.rbegin(),a.rend()
#define SORT(a) sort(ALL(a))
#define RSORT(a) sort(RALL(a))
#define REV(a) reverse(ALL(a))
#define UNI(a) a.erase(unique(ALL(a)),a.end())
#define SZ(a) (int)(a.size())
#define LB(a,x) (int)(lower_bound(ALL(a),x)-a.begin())
#define UB(a,x) (int)(upper_bound(ALL(a),x)-a.begin())
#define MIN(a) *min_element(ALL(a))
#define MAX(a) *max_element(ALL(a))
using ll = long long ;
using db = long double ;
using i128 = __int128_t ;
using u32 = uint32_t ;
using u64 = uint64_t ;
const int INF = INT_MAX / 2 ;
const ll LINF = LLONG_MAX / 4 ;
const db DINF = numeric_limits < db >:: infinity ();
const int MOD = 998244353 ;
const int MOD2 = 1000000007 ;
const db EPS = 1e-9 ;
const db PI = acos ( db ( - 1 ));
template < class T >
using PQ = priority_queue < T , vector < T > , greater < T >> ;
#define vv(T,a,n,...) vector<vector<T>> a(n,vector<T>(__VA_ARGS__))
#define vvv(T,a,n,m,...) vector<vector<vector<T>>> a(n,vector<vector<T>>(m,vector<T>(__VA_ARGS__)))
#define vvvv(T,a,n,m,k,...) vector<vector<vector<vector<T>>>> a(n,vector<vector<vector<T>>>(m,vector<vector<T>>(k,vector<T>(__VA_ARGS__))))
template < class T , class U >
bool chmin ( T & a , U b ){ return b < a ? a = b , 1 : 0 ;}
template < class T , class U >
bool chmax ( T & a , U b ){ return a < b ? a = b , 1 : 0 ;}
template < class T , class U >
T SUM ( const U & a ){ return accumulate ( ALL ( a ), T {});}
mt19937 rng ( chrono :: steady_clock :: now (). time_since_epoch (). count ());
mt19937_64 rng64 ( chrono :: steady_clock :: now (). time_since_epoch (). count ());
#line 3 "data-structure/fast-set.hpp"
/**
* Author: Teetat T.
* Date: 2026-04-16
* Description: Fast Set
*/
struct FastSet {
using u64 = uint64_t ;
int n , len ;
vector < u64 > t ;
FastSet (){}
FastSet ( int n ){ build ( n );}
int size (){ return n ;}
void build ( int _n ){
assert ( _n >= 0 );
n = _n ;
len = 1 ;
while (( len << 6 ) < n ) len <<= 6 ;
len /= 63 ;
t . resize ( len + ( n + 63 ) / 64 );
}
bool count ( int i ) const {
assert ( 0 <= i && i < n );
return t [ len + ( i >> 6 )] >> ( i & 63 ) & 1 ;
}
void insert ( int x ){
assert ( 0 <= x && x < n );
int i = len + ( x >> 6 ), j = x & 63 ;
while ( true ){
if ( t [ i ] >> j & 1ULL ) return ;
t [ i ] |= 1ULL << j ;
if ( ! i ) return ;
j = ( -- i ) & 63 ;
i >>= 6 ;
}
}
void erase ( int x ){
assert ( 0 <= x && x < n );
int i = len + ( x >> 6 ), j = x & 63 ;
while ( true ){
t [ i ] &=~ ( 1ULL << j );
if ( ! i || t [ i ]) return ;
j = ( -- i ) & 63 ;
i >>= 6 ;
}
}
int next ( int x ){
if ( x >= n ) return n ;
if ( x < 0 ) x = 0 ;
if ( count ( x )) return x ;
int i = len + ( x >> 6 ), j = x & 63 ;
while ( true ){
if ( t [ i ] & (( ~ 1ULL ) << j )){
j = __builtin_ctzll ( t [ i ] & (( ~ 1ULL ) << j ));
if ( i >= len ) return ( i - len ) * 64 + j ;
break ;
}
if ( ! i ) return n ;
j = ( -- i ) & 63 ;
i >>= 6 ;
}
i = i * 64 + j + 1 ;
while ( i < len ) i = i * 64 + __builtin_ctzll ( t [ i ]) + 1 ;
return ( i - len ) * 64 + __builtin_ctzll ( t [ i ]);
}
int prev ( int x ){
if ( x < 0 ) return - 1 ;
if ( x >= n ) x = n - 1 ;
if ( count ( x )) return x ;
int i = len + ( x >> 6 ), j = x & 63 ;
while ( true ){
if ( t [ i ] & (( 1ULL << j ) - 1ULL )){
j = 63 - __builtin_clzll ( t [ i ] & (( 1ULL << j ) - 1ULL ));
if ( i >= len ) return ( i - len ) * 64 + j ;
break ;
}
if ( ! i ) return - 1 ;
j = ( -- i ) & 63 ;
i >>= 6 ;
}
i = i * 64 + j + 1 ;
while ( i < len ) i = i * 64 + 64 - __builtin_clzll ( t [ i ]);
return ( i - len ) * 64 + 63 - __builtin_clzll ( t [ i ]);
}
};
#line 4 "verify/yosupo/data-structure/predecessor_problem.test.cpp"
int main (){
cin . tie ( nullptr ) -> sync_with_stdio ( false );
int n , q ;
cin >> n >> q ;
string t ;
cin >> t ;
FastSet fs ( n );
for ( int i = 0 ; i < n ; i ++ ) if ( t [ i ] == '1' ) fs . insert ( i );
while ( q -- ){
int op , k ;
cin >> op >> k ;
if ( op == 0 ){
fs . insert ( k );
} else if ( op == 1 ){
fs . erase ( k );
} else if ( op == 2 ){
cout << fs . count ( k ) << " \n " ;
} else if ( op == 3 ){
int x = fs . next ( k );
if ( x == n ) x =- 1 ;
cout << x << " \n " ;
} else {
cout << fs . prev ( k ) << " \n " ;
}
}
}
Back to top page