#include <bits/stdc++.h> using namespace std; #ifndef LOCAL #pragma GCC optimize("O3") #endif #define fi first #define se second #define pii pair<int,int> #define mp make_pair #define endl '\n' #define sp <<" "<< #define eb emplace_back #define MOD 1000000007 #define gcd(a,b) __gcd(a,b) #define lcm(a,b) (a*(b/gcd(a,b))) #define all(a) (a).begin(),(a).end() #define rall(a) (a).rbegin(),(a).rend() using ll = long long; #define vec vector template <class T> void print_v(vector<T> &v) { cout << "{"; for (auto x : v) cout << x << ","; cout << "}\n"; } template <class T> void print_m(vector<vector<T>> &m) { for (auto v : m) print_v(v); cout << '\n'; } #define fora(a) for(auto e:a) #define it(i,s,e) for(long long int i=s;i<e;i++) #define ita(i,s,e) for(long long int i=s;i<=e;i++) #define itr(i,e,s) for(long long int i=e-1;i>=s;i--) #define urs(r...)typename decay<decltype(r)>::type #define rep(i,n)for(urs(n)i=0;i<(n);++i) int n, m; vec<pair<ll, ll>> curr; vec<pair<ll, ll>> next_batch; const int UNDETERMINED = 0; const int READY = 1; const int NOT_READY = 2; void set_status(pair<ll, ll> &s, int idx, int status) { switch (status) { case UNDETERMINED: s.fi &= ~((ll)1 << idx); s.se &= ~((ll)1 << idx); break; case READY: s.fi |= ((ll)1 << idx ); s.se &= ~((ll)1 << idx); break; case NOT_READY: s.fi &= ~((ll) 1 << idx); s.se |= ((ll) 1 << idx ); break; } } int determine_status(pair<ll, ll> s, int i) { if ((s.fi & ( (ll)1 << i )) >> i) return READY; if ((s.se & ( (ll)1 << i )) >> i) return NOT_READY; return UNDETERMINED; } void print_status(pair<ll, ll> &s) { cout << s.fi sp s.se << endl; rep (idx, n+10) { char c; switch (determine_status(s, idx)) { case UNDETERMINED: c = '?'; break; case READY: c = '1'; break; case NOT_READY: c = '0'; break; } cout << c; } cout << endl; } void compute_continuations(pair<ll, ll> s, int a, int b) { int a_status = determine_status(s, a); int b_status = determine_status(s, b); auto s1 = s; switch ((a_status << 2) + b_status) { case (UNDETERMINED << 2) + UNDETERMINED: set_status(s, a, NOT_READY); set_status(s, b, NOT_READY); next_batch.eb(s); set_status(s1, a, READY); set_status(s1, b, READY); next_batch.eb(s1); break; case (UNDETERMINED << 2) + NOT_READY: set_status(s, a, NOT_READY); set_status(s, b, UNDETERMINED); next_batch.eb(s); break; case (NOT_READY << 2) + UNDETERMINED: case (NOT_READY << 2) + NOT_READY: case (NOT_READY << 2) + READY: case (READY << 2) + READY: case (UNDETERMINED << 2) + READY: next_batch.eb(s); break; case (READY << 2) + UNDETERMINED: set_status(s, a, UNDETERMINED); set_status(s, b, READY); next_batch.eb(s); break; case (READY << 2) + NOT_READY: set_status(s, a, NOT_READY); set_status(s, b, READY); next_batch.eb(s); break; } } vec<int> seg_counts(400); void fill_counts_for_len(int max_len) { if (max_len > 0) { for (int l = 1; l<=max_len; l++) { seg_counts[l] += max_len - l + 1; } } } void calc_segments(pair<ll, ll> s) { //cout << s.fi sp s.se << endl; if (s.fi == 0) { int len = 0; for (int i=0; i<n; i++) { if (determine_status(s, i) == UNDETERMINED) { len++; continue; } // NOT_READY fill_counts_for_len(len); len = 0; } fill_counts_for_len(len); } else { //cout << s.fi << endl; int rightmost = log2((s.fi & ~ ( s.fi - 1 ))); int leftmost = 63-__builtin_clzll(s.fi); int rightmost_poss = rightmost; int leftmost_poss = leftmost; for (int power = rightmost; power < leftmost; power++) { if (determine_status(s, power) == NOT_READY) return; } for (int power = rightmost-1; power >= 0; power--) { if (determine_status(s, power) == UNDETERMINED) { rightmost_poss = power; } else { break; } } for (int power = leftmost+1; power < n; power++) { if (determine_status(s, power) == UNDETERMINED) { leftmost_poss = power; } else { break; } } // print_status(s); // cout << s.fi << endl; // cout << "basic " << rightmost sp leftmost << endl; // cout << "poss " << rightmost_poss sp leftmost_poss << endl; int min_len = leftmost-rightmost + 1; int max_len = leftmost_poss-rightmost_poss + 1; for (int len=min_len; len<=max_len; len++) { int max_left = min(leftmost_poss, rightmost+len-1); int max_right = max(rightmost_poss, leftmost-len+1); int range_len = max_left - max_right + 1; // cout << len sp min_len sp max_len << endl; // cout << len sp range_len-len+1 << endl; seg_counts[len] += range_len-len+1; } } // for (int i=1; i<=n; i++) { // cout << seg_counts[i] << " "; // } // cout << endl; } void solve() { cin >> n >> m; curr.eb(mp(0,0)); rep (_, m) { int a, b; cin >> a >> b; for (auto s: curr) { compute_continuations(s, a-1, b-1); } swap (curr, next_batch); next_batch.clear(); } for (auto x: curr) { calc_segments(x); } for (int i=1; i<=n; i++) { cout << seg_counts[i] % 2 << " "; } cout << endl; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); solve(); return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 | #include <bits/stdc++.h> using namespace std; #ifndef LOCAL #pragma GCC optimize("O3") #endif #define fi first #define se second #define pii pair<int,int> #define mp make_pair #define endl '\n' #define sp <<" "<< #define eb emplace_back #define MOD 1000000007 #define gcd(a,b) __gcd(a,b) #define lcm(a,b) (a*(b/gcd(a,b))) #define all(a) (a).begin(),(a).end() #define rall(a) (a).rbegin(),(a).rend() using ll = long long; #define vec vector template <class T> void print_v(vector<T> &v) { cout << "{"; for (auto x : v) cout << x << ","; cout << "}\n"; } template <class T> void print_m(vector<vector<T>> &m) { for (auto v : m) print_v(v); cout << '\n'; } #define fora(a) for(auto e:a) #define it(i,s,e) for(long long int i=s;i<e;i++) #define ita(i,s,e) for(long long int i=s;i<=e;i++) #define itr(i,e,s) for(long long int i=e-1;i>=s;i--) #define urs(r...)typename decay<decltype(r)>::type #define rep(i,n)for(urs(n)i=0;i<(n);++i) int n, m; vec<pair<ll, ll>> curr; vec<pair<ll, ll>> next_batch; const int UNDETERMINED = 0; const int READY = 1; const int NOT_READY = 2; void set_status(pair<ll, ll> &s, int idx, int status) { switch (status) { case UNDETERMINED: s.fi &= ~((ll)1 << idx); s.se &= ~((ll)1 << idx); break; case READY: s.fi |= ((ll)1 << idx ); s.se &= ~((ll)1 << idx); break; case NOT_READY: s.fi &= ~((ll) 1 << idx); s.se |= ((ll) 1 << idx ); break; } } int determine_status(pair<ll, ll> s, int i) { if ((s.fi & ( (ll)1 << i )) >> i) return READY; if ((s.se & ( (ll)1 << i )) >> i) return NOT_READY; return UNDETERMINED; } void print_status(pair<ll, ll> &s) { cout << s.fi sp s.se << endl; rep (idx, n+10) { char c; switch (determine_status(s, idx)) { case UNDETERMINED: c = '?'; break; case READY: c = '1'; break; case NOT_READY: c = '0'; break; } cout << c; } cout << endl; } void compute_continuations(pair<ll, ll> s, int a, int b) { int a_status = determine_status(s, a); int b_status = determine_status(s, b); auto s1 = s; switch ((a_status << 2) + b_status) { case (UNDETERMINED << 2) + UNDETERMINED: set_status(s, a, NOT_READY); set_status(s, b, NOT_READY); next_batch.eb(s); set_status(s1, a, READY); set_status(s1, b, READY); next_batch.eb(s1); break; case (UNDETERMINED << 2) + NOT_READY: set_status(s, a, NOT_READY); set_status(s, b, UNDETERMINED); next_batch.eb(s); break; case (NOT_READY << 2) + UNDETERMINED: case (NOT_READY << 2) + NOT_READY: case (NOT_READY << 2) + READY: case (READY << 2) + READY: case (UNDETERMINED << 2) + READY: next_batch.eb(s); break; case (READY << 2) + UNDETERMINED: set_status(s, a, UNDETERMINED); set_status(s, b, READY); next_batch.eb(s); break; case (READY << 2) + NOT_READY: set_status(s, a, NOT_READY); set_status(s, b, READY); next_batch.eb(s); break; } } vec<int> seg_counts(400); void fill_counts_for_len(int max_len) { if (max_len > 0) { for (int l = 1; l<=max_len; l++) { seg_counts[l] += max_len - l + 1; } } } void calc_segments(pair<ll, ll> s) { //cout << s.fi sp s.se << endl; if (s.fi == 0) { int len = 0; for (int i=0; i<n; i++) { if (determine_status(s, i) == UNDETERMINED) { len++; continue; } // NOT_READY fill_counts_for_len(len); len = 0; } fill_counts_for_len(len); } else { //cout << s.fi << endl; int rightmost = log2((s.fi & ~ ( s.fi - 1 ))); int leftmost = 63-__builtin_clzll(s.fi); int rightmost_poss = rightmost; int leftmost_poss = leftmost; for (int power = rightmost; power < leftmost; power++) { if (determine_status(s, power) == NOT_READY) return; } for (int power = rightmost-1; power >= 0; power--) { if (determine_status(s, power) == UNDETERMINED) { rightmost_poss = power; } else { break; } } for (int power = leftmost+1; power < n; power++) { if (determine_status(s, power) == UNDETERMINED) { leftmost_poss = power; } else { break; } } // print_status(s); // cout << s.fi << endl; // cout << "basic " << rightmost sp leftmost << endl; // cout << "poss " << rightmost_poss sp leftmost_poss << endl; int min_len = leftmost-rightmost + 1; int max_len = leftmost_poss-rightmost_poss + 1; for (int len=min_len; len<=max_len; len++) { int max_left = min(leftmost_poss, rightmost+len-1); int max_right = max(rightmost_poss, leftmost-len+1); int range_len = max_left - max_right + 1; // cout << len sp min_len sp max_len << endl; // cout << len sp range_len-len+1 << endl; seg_counts[len] += range_len-len+1; } } // for (int i=1; i<=n; i++) { // cout << seg_counts[i] << " "; // } // cout << endl; } void solve() { cin >> n >> m; curr.eb(mp(0,0)); rep (_, m) { int a, b; cin >> a >> b; for (auto s: curr) { compute_continuations(s, a-1, b-1); } swap (curr, next_batch); next_batch.clear(); } for (auto x: curr) { calc_segments(x); } for (int i=1; i<=n; i++) { cout << seg_counts[i] % 2 << " "; } cout << endl; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); solve(); return 0; } |