結果
問題 | No.911 ラッキーソート |
ユーザー |
![]() |
提出日時 | 2019-10-19 14:34:25 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 292 ms / 2,000 ms |
コード長 | 5,875 bytes |
コンパイル時間 | 1,181 ms |
コンパイル使用メモリ | 123,000 KB |
実行使用メモリ | 12,080 KB |
最終ジャッジ日時 | 2024-06-27 04:11:11 |
合計ジャッジ時間 | 8,986 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 46 |
ソースコード
#include <iostream>#include <algorithm>#include <bitset>#include <map>#include <queue>#include <set>#include <stack>#include <string>#include <cstring>#include <utility>#include <vector>#include <complex>#include <valarray>#include <fstream>#include <cassert>#include <cmath>#include <functional>#include <iomanip>#include <numeric>#include <climits>#include <random>#define _overload(a, b, c, d, ...) d#define _rep1(X, A, Y) for (int (X) = (A);(X) <= (Y);++(X))#define _rep2(X, Y) for (int (X) = 0;(X) < (Y);++(X))#define rep(...) _overload(__VA_ARGS__, _rep1, _rep2)(__VA_ARGS__)#define rrep(X, Y) for (int (X) = Y-1;(X) >= 0;--(X))#define all(X) (X).begin(),(X).end()#define len(X) ((int)(X).size())#define mod(n, m) (((n)%(m)+(m))%m)#define fi first#define sc secondusing namespace std;typedef long long ll;typedef unsigned long long ull;typedef pair<int, int> Pii;typedef pair<ll, ll> Pll;const int INFINT = 1 << 30; // 1.07x10^ 9const ll INFLL = 1LL << 60; // 1.15x10^18const double EPS = 1e-10;const int MOD = 1000000007;const int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};const int MAX_N = 200000;int N;ll L, R;ll A[MAX_N];int bitmap[61];void set_bitmap();ll max_x();ll min_x();int main() {cin >> N >> L >> R;rep(i, N) cin >> A[i];set_bitmap();ll ma = max_x();ll mi = min_x();if (ma < L) {cout << 0 << endl;} else if (mi > R) {cout << 0 << endl;} else {ll mi_bit = 0, ma_bit = 0;for (int i = 61; i >= 0; --i) {if (bitmap[i] == 2) {mi_bit <<= 1; ma_bit <<= 1;mi_bit |= ((mi>>i)&1);ma_bit |= ((ma>>i)&1);}}cout << ma_bit - mi_bit + 1 << endl;}return 0;}ll max_x() {ll res = 0;for (int i = 61; i >= 0; --i) {if (bitmap[i] == 1) {res += 1LL << i;}}if (res > R) return -1;for (int i = 61; i >= 0; --i) {if (bitmap[i] == 2) {ll tmp = res;tmp += 1LL << i;if (tmp <= R) {res = tmp;}}}return res;}ll min_x() {ll res = 0;for (int i = 61; i >= 0; --i) {if (bitmap[i] == 1) {res += 1LL << i;}}if (res >= L) return res;for (int i = 61; i >= 0; --i) {if (bitmap[i] == 2) {ll tmp = res;tmp += 1LL << i;if (tmp < L) res = tmp;}}int first_zero_bit = -1;for (int i = 0; i <= 61; ++i) {if ((bitmap[i] == 2) && ((res >> i) & 1) == 0) {first_zero_bit = i;break;}}if (first_zero_bit < 0) return INFLL;res += 1ll << first_zero_bit;for (int i = 0; i < first_zero_bit; ++i) {if (bitmap[i] == 2) {res -= 1ll << i;}}return res;}void set_bitmap() {vector<Pii> section;section.emplace_back(0, N);for (ll i = 61; i >= 0; --i) {vector<Pii> tmp_section;bool must_flag = false;bool must_not_flag = false;for (ll j = 0; j < len(section); ++j) {ll lb = section[j].fi;ll ub = section[j].sc;bool all_zero = true;for (ll k = lb; k < ub; ++k) {if (((A[k] >> i) & 1) == 1) {all_zero = false;break;}}if (all_zero) {tmp_section.emplace_back(Pii(lb, ub));continue;}bool all_one = true;for (ll k = lb; k < ub; ++k) {if (((A[k] >> i) & 1) == 0) {all_one = false;break;}}if (all_one) {tmp_section.emplace_back(Pii(lb, ub));continue;}ll first_one_index = 0;if (((A[lb] >> i) & 1) == 0) {for (ll k = lb; k < ub; ++k) {if (((A[k] >> i) & 1) == 1) {first_one_index = k;break;}}bool valid_section = true;for (ll k = first_one_index; k < ub; ++k) {if (((A[k] >> i) & 1) == 0) {valid_section = false;break;}}if (!valid_section) {cout << 0 << endl;exit(0);}must_not_flag = true;} else {for (ll k = lb; k < ub; ++k) {if (((A[k] >> i) & 1) == 0) {first_one_index = k;break;}}bool valid_section = true;for (ll k = first_one_index; k < ub; ++k) {if (((A[k] >> i) & 1) == 1) {valid_section = false;break;}}if (!valid_section) {cout << 0 << endl;exit(0);}must_flag = true;}tmp_section.emplace_back(lb, first_one_index);tmp_section.emplace_back(first_one_index, ub);}if (must_not_flag && must_flag) {cout << 0 << endl;exit(0);} else if (must_flag) {bitmap[i] = 1;} else if (must_not_flag) {bitmap[i] = 0;} else {bitmap[i] = 2;}section = tmp_section;}}