@@ -8,7 +8,15 @@ using namespace std;
88
99int n, k;
1010vector<int > a;
11+ unordered_map<int , int > last_seen[31 ]; // last_seen[i][j] is the last seen index of j (masking out the least significant i bits)
1112int main () {
13+ for (int i = 0 ; i < 31 ; i++) {
14+ last_seen[i].reserve (1e4 ); // deal with the hacking issue...
15+ }
16+ ios::sync_with_stdio (0 );
17+ cin.tie (0 );
18+ cout.tie (0 );
19+
1220 int tc;
1321 cin >> tc;
1422 while (tc--) {
@@ -38,16 +46,20 @@ int main() {
3846 }
3947
4048 int best = 1e9 ;
41- unordered_map<int , int > last_seen[32 ]; // last_seen[i][j] is the last seen index of j (masking out the least significant i bits)
49+ for (int i = 0 ; i < 31 ; i++) {
50+ last_seen[i].clear ();
51+ }
52+
4253 for (int i = 0 ; i < n; i++) {
4354 int looking_for = 0 ;
44- for (int j = 31 ; j >= 0 ; j--) {
55+ for (int j = 30 ; j >= 0 ; j--) {
4556 if ((1 << j) & k) { // must follow along differently
4657 looking_for = looking_for | ((1 << j) ^ ((1 << j) & a[i]));
4758 } else { // we can try to find someone who has a different version at this point
4859 looking_for = looking_for | ((1 << j) ^ ((1 << j) & a[i]));
49- if (last_seen[j].contains (looking_for)) {
50- int idx = last_seen[j][looking_for];
60+ auto it = last_seen[j].find (looking_for);
61+ if (it != last_seen[j].end ()) {
62+ int idx = it->second ;
5163 best = min (best, i - idx + 1 );
5264 }
5365 // otherwise assume this bit will be the same
@@ -56,13 +68,14 @@ int main() {
5668 }
5769
5870 // also check non-masked
59- if (last_seen[0 ].contains (looking_for)) {
60- int idx = last_seen[0 ][looking_for];
71+ auto it = last_seen[0 ].find (looking_for);
72+ if (it != last_seen[0 ].end ()) {
73+ int idx = it->second ;
6174 best = min (best, i - idx + 1 );
6275 }
6376
6477 int mask = ~0 ;
65- for (int j = 0 ; j < 32 ; j++) {
78+ for (int j = 0 ; j < 31 ; j++) {
6679 last_seen[j][a[i] & mask] = i;
6780 mask = mask ^ (1 << j);
6881 }
@@ -75,4 +88,4 @@ int main() {
7588 }
7689 }
7790 return 0 ;
78- }
91+ }
0 commit comments