# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
571414 | BackNoob | XOR (IZhO12_xor) | C++14 | 197 ms | 60424 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define endl '\n'
#define MASK(i) (1LL << (i))
#define ull unsigned long long
#define ld long double
#define pb push_back
#define all(x) (x).begin() , (x).end()
#define BIT(x , i) ((x >> (i)) & 1)
#define TASK "task"
#define sz(s) (int) (s).size()
using namespace std;
const int mxN = 5e5 + 227;
const int inf = 1e9 + 277;
const int mod = 24211007;
const ll infll = 1e18 + 7;
const int base = 200;
const int LOG = 29;
template <typename T1, typename T2> bool minimize(T1 &a, T2 b) {
if (a > b) {a = b; return true;} return false;
}
template <typename T1, typename T2> bool maximize(T1 &a, T2 b) {
if (a < b) {a = b; return true;} return false;
}
struct Node{
Node* child[2];
int minpos;
Node()
{
child[0] = child[1] = NULL;
minpos = inf;
}
};
Node* root = new Node();
void add(int x, int idx)
{
Node* p = root;
for(int i = 29; i >= 0; i--) {
int nxt = BIT(x, i);
if(p -> child[nxt] == NULL)
p -> child[nxt] = new Node();
p = p -> child[nxt];
minimize(p -> minpos, idx);
}
}
int n;
int k;
int a[mxN];
int pre[mxN];
int timer = 0;
pair<int, int> ans = {-1, -1};
int calc(int x)
{
// if(timer == 3) cout << x << endl;
int res = inf;
Node* p = root;
for(int j = LOG; j >= 0; j--) {
if(x & (1 << j)) {
if(!(k & (1 << j))) {
if(p -> child[0] != NULL)
minimize(res, p -> child[0] -> minpos);
if(p -> child[1] == NULL) return res;
p = p -> child[1];
// cout << 1;
}
else {
if(p -> child[0] == NULL) return res;
p = p -> child[0];
// cout << 0;
}
}
else {
if(k & (1 << j)) {
if(p -> child[1] == NULL) return res;
p = p -> child[1];
// cout << 1;
}
else {
if(p -> child[1] != NULL)
minimize(res, p -> child[1] -> minpos);
if(p -> child[0] == NULL) return res;
p = p -> child[0];
// cout << 0;
}
}
}
minimize(res, p -> minpos);
return res;
}
void solve()
{
cin >> n >> k;
for(int i = 1; i <= n; i++) cin >> a[i];
for(int i = 1; i <= n; i++) pre[i] = pre[i - 1] ^ a[i];
add(pre[0], 0);
for(int i = 1; i <= n; i++) {
++timer;
int x = calc(pre[i]);
// cout << endl;
if(x != inf) {
if((ans.se - ans.fi + 1) < i - x + 1) {
ans.fi = x;
ans.se = i;
}
if((ans.se - ans.fi + 1) == i - x + 1) {
if(ans.fi > x) {
ans.fi = x;
ans.se = i;
}
}
}
add(pre[i], i);
}
cout << ans.fi + 1 << ' ' << (ans.se - ans.fi - 1 + 1);
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
//freopen(TASK".in" , "r" , stdin);
//freopen(TASK".out" , "w" , stdout);
int tc = 1;
// cin >> tc;
while(tc--) {
solve();
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |