# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
998827 | nguyennh | XOR (IZhO12_xor) | C++14 | 19 ms | 3420 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 el '\n'
using namespace std;
const int MN = 1e5 + 5;
const int N = 3e5 + 5;
const int LG = 31;
int a[N];
struct Trie{
struct Node{
int child[2] , min_pos;
} node[MN];
int cur;
Trie () : cur(0){
memset(node[0].child , -1 , sizeof(node[cur].child));
node[0].min_pos = INT_MAX;
}
int new_node(){
cur++;
memset(node[cur].child , -1 , sizeof(node[cur].child));
node[cur].min_pos = INT_MAX;
return cur;
}
void add(int x , int pos){
int root = 0;
for ( int i = LG ; i >= 0 ; i-- ){
int c = x >> i & 1;
if (node[root].child[c] == -1) node[root].child[c] = new_node();
root = node[root].child[c];
node[root].min_pos = min(node[root].min_pos , pos);
}
}
int query(int x , int k){
int root = 0 , ans = INT_MAX;
for ( int i = LG ; i >= 0 ; i-- ){
int bitx = x >> i & 1;
int bitk = k >> i & 1;
if (!bitk){
if (node[root].child[bitx ^ 1] != -1){
int new_root = node[root].child[bitx ^ 1];;
ans = min(ans , node[new_root].min_pos);
}
if (!node[root].child[bitx]) return ans;
root = node[root].child[bitx];
}
else {
if (node[root].child[bitx ^ 1] == -1) return ans;
root = node[root].child[bitx ^ 1];
}
}
ans = min(ans , node[root].min_pos);
return ans;
}
};
int32_t main (){
ios_base::sync_with_stdio(0);
cin.tie(0);
int n , k;
cin >> n >> k;
for ( int i = 1 ; i <= n ; i++ ) cin >> a[i];
vector<int64_t> pref(n + 5);
for ( int i = 1 ; i <= n ; i++ ) pref[i] = pref[i - 1] ^ a[i];
Trie tr;
int ans = 0 , pos = INT_MAX;
for ( int i = 1 ; i <= n ; i++ ){
tr.add(pref[i - 1] , i - 1);
int left_bound = tr.query(pref[i] , k);
if (left_bound == INT_MAX) continue;
if (ans < i - left_bound){
ans = i - left_bound;
pos = left_bound + 1;
}
else if (ans == i - left_bound){
pos = min(pos , left_bound + 1);
}
}
cout << pos << " " << ans;
}
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |