From ff3b5b87541bf1049a258fd22ddcbb517910fb24 Mon Sep 17 00:00:00 2001 From: omagdy7 Date: Wed, 27 Sep 2023 19:29:33 +0300 Subject: Solved 4 problems in the last div3(Round900) --- contests/Round900/E/main.cpp | 177 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 177 insertions(+) create mode 100755 contests/Round900/E/main.cpp (limited to 'contests/Round900/E/main.cpp') diff --git a/contests/Round900/E/main.cpp b/contests/Round900/E/main.cpp new file mode 100755 index 0000000..b91130f --- /dev/null +++ b/contests/Round900/E/main.cpp @@ -0,0 +1,177 @@ +#include +#include +using namespace std; + +using ll = long long; +using pii = pair; +using vpi = vector; +using vi = vector; +using vll = vector; +using mpii = map; +using mpll = map; +using db = long double; + +#define pb push_back +#define all(x) (x).begin(), (x).end() +#define rall(x) (x).rbegin(), (x).rend() +#define lb lower_bound +#define ub upper_bound +#define make_unique(x) \ + sort(all((x))); \ + (x).resize(unique(all((x))) - (x).begin()) +#define ceil(a, b) ((a) + (b)-1) / (b) + +const int MOD = (int)1e9 + 7; +const db PI = acos((db)-1); +const int dx[4]{1, 0, -1, 0}; +const int dy[4]{0, 1, 0, -1}; + +template +ostream &operator<<(ostream &os, const pair &p); +template ostream &operator<<(ostream &os, const vector &vec); +template +ostream &operator<<(ostream &os, const map &m); +template +ostream &operator<<(ostream &os, const unordered_map &m); +template ostream &operator<<(ostream &os, const set &s); +template +ostream &operator<<(ostream &os, const unordered_set &s); + +template +ostream &operator<<(ostream &os, const pair &p) { + os << "(" << p.first << ", " << p.second << ")"; + return os; +} + +template ostream &operator<<(ostream &os, const vector &vec) { + os << "{"; + for (size_t i = 0; i < vec.size(); ++i) { + if (i > 0) + os << ", "; + os << vec[i]; + } + os << "}\n"; + return os; +} + +template +ostream &operator<<(ostream &os, const map &m) { + os << "{"; + for (const auto &p : m) { + os << p.first << ": " << p.second << ", "; + } + os << "}"; + return os; +} + +template +ostream &operator<<(ostream &os, const unordered_map &m) { + os << "{"; + for (const auto &p : m) { + os << p.first << ": " << p.second << ", "; + } + os << "}"; + return os; +} + +template ostream &operator<<(ostream &os, const set &s) { + int i = 0; + os << "{"; + for (const auto &e : s) { + if (i > 0) + os << ", "; + os << e; + i++; + } + os << "}"; + return os; +} + +template +ostream &operator<<(ostream &os, const unordered_set &s) { + int i = 0; + os << "{"; + for (const auto &e : s) { + if (i > 0) + os << ", "; + os << e; + i++; + } + os << "}"; + return os; +} + +void dbg() { cerr << "\n"; } + +template void dbg(T val, TS... vals) { + cerr << val << " "; + dbg(vals...); +} + +/* stuff you should look for: + --------------------------- + * special cases (n=1?) + * int overflow, array bounds + * do smth instead of nothing and stay organized + * WRITE STUFF DOWN + * DON'T GET STUCK ON ONE APPROACH + */ + +const int bits = 32; + +int get_number(int l, int r, vector &mp, vi &v) { + int ans = 0; + if (l == r) + return v[l]; + for (int i = 0; i < bits; i++) { + if (mp[r][i] - mp[l][i] == r - l) { + ans |= (1 << i); + } + } + return ans; +} + +void solve() { + int n; + cin >> n; + vi v(n); + for (auto &x : v) { + cin >> x; + } + vector mp(n + 1, vi(bits, 0)); + for (int i = 0; i < n; i++) { + for (int j = 0; j < bits; j++) { + mp[i + 1][j] += (((1 << j) & v[i]) > 0) + mp[i][j]; + } + } + int q; + cin >> q; + for (int i = 0; i < q; i++) { + int l, k; + cin >> l >> k; + ll lo = l - 1, hi = n; + while (hi - lo > 1) { + ll mid = (lo + hi) / 2; + if (get_number(l - 1, mid + 1, mp, v) < k) { + hi = mid; + } else { + lo = mid; + } + } + if (get_number(l - 1, hi - 1, mp, v) < k) { + cout << -1 << " \n"[i == q - 1]; + } else { + cout << hi << " \n"[i == q - 1]; + } + } +} + +int main() { + ios_base::sync_with_stdio(false); + cin.tie(NULL); + int tt; + cin >> tt; + while (tt--) { + solve(); + } +} -- cgit v1.2.3