File tree Expand file tree Collapse file tree
verify/yosupo/data-structure Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ #pragma once
2+ #include " template.hpp"
3+
4+ /* *
5+ * Author: Teetat T.
6+ * Date: 2026-04-16
7+ * Description: Fast Set
8+ */
9+
10+ struct FastSet {
11+ using u64 = uint64_t ;
12+
13+ int n,len;
14+ vector<u64 > t;
15+
16+ FastSet (){}
17+ FastSet (int n){build (n);}
18+
19+ int size (){return n;}
20+ void build (int _n){
21+ assert (_n>=0 );
22+ n=_n;
23+ len=1 ;
24+ while ((len<<6 )<n)len<<=6 ;
25+ len/=63 ;
26+ t.resize (len+(n+63 )/64 );
27+ }
28+
29+ bool count (int i)const {
30+ assert (0 <=i&&i<n);
31+ return t[len+(i>>6 )]>>(i&63 )&1 ;
32+ }
33+ void insert (int x){
34+ assert (0 <=x&&x<n);
35+ int i=len+(x>>6 ),j=x&63 ;
36+ while (true ){
37+ if (t[i]>>j&1ULL )return ;
38+ t[i]|=1ULL <<j;
39+ if (!i)return ;
40+ j=(--i)&63 ;
41+ i>>=6 ;
42+ }
43+ }
44+ void erase (int x){
45+ assert (0 <=x&&x<n);
46+ int i=len+(x>>6 ),j=x&63 ;
47+ while (true ){
48+ t[i]&=~(1ULL <<j);
49+ if (!i||t[i])return ;
50+ j=(--i)&63 ;
51+ i>>=6 ;
52+ }
53+ }
54+ int next (int x){
55+ if (x>=n)return n;
56+ if (x<0 )x=0 ;
57+ if (count (x))return x;
58+ int i=len+(x>>6 ),j=x&63 ;
59+ while (true ){
60+ if (t[i]&((~1ULL )<<j)){
61+ j=__builtin_ctzll (t[i]&((~1ULL )<<j));
62+ if (i>=len)return (i-len)*64 +j;
63+ break ;
64+ }
65+ if (!i)return n;
66+ j=(--i)&63 ;
67+ i>>=6 ;
68+ }
69+ i=i*64 +j+1 ;
70+ while (i<len)i=i*64 +__builtin_ctzll (t[i])+1 ;
71+ return (i-len)*64 +__builtin_ctzll (t[i]);
72+ }
73+ int prev (int x){
74+ if (x<0 )return -1 ;
75+ if (x>=n)x=n-1 ;
76+ if (count (x))return x;
77+ int i=len+(x>>6 ),j=x&63 ;
78+ while (true ){
79+ if (t[i]&((1ULL <<j)-1ULL )){
80+ j=63 -__builtin_clzll (t[i]&((1ULL <<j)-1ULL ));
81+ if (i>=len)return (i-len)*64 +j;
82+ break ;
83+ }
84+ if (!i)return -1 ;
85+ j=(--i)&63 ;
86+ i>>=6 ;
87+ }
88+ i=i*64 +j+1 ;
89+ while (i<len)i=i*64 +64 -__builtin_clzll (t[i]);
90+ return (i-len)*64 +63 -__builtin_clzll (t[i]);
91+ }
92+ };
Original file line number Diff line number Diff line change 1+ #define PROBLEM " https://judge.yosupo.jp/problem/predecessor_problem"
2+ #include " template.hpp"
3+ #include " data-structure/fast-set.hpp"
4+
5+ int main (){
6+ cin.tie (nullptr )->sync_with_stdio (false );
7+ int n,q;
8+ cin >> n >> q;
9+ string t;
10+ cin >> t;
11+ FastSet fs (n);
12+ for (int i=0 ;i<n;i++)if (t[i]==' 1' )fs.insert (i);
13+ while (q--){
14+ int op,k;
15+ cin >> op >> k;
16+ if (op==0 ){
17+ fs.insert (k);
18+ }else if (op==1 ){
19+ fs.erase (k);
20+ }else if (op==2 ){
21+ cout << fs.count (k) << " \n " ;
22+ }else if (op==3 ){
23+ int x=fs.next (k);
24+ if (x==n)x=-1 ;
25+ cout << x << " \n " ;
26+ }else {
27+ cout << fs.prev (k) << " \n " ;
28+ }
29+ }
30+ }
You can’t perform that action at this time.
0 commit comments