@@ -8,7 +8,15 @@ using namespace std;
8
8
9
9
int n, k;
10
10
vector<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)
11
12
int 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
+
12
20
int tc;
13
21
cin >> tc;
14
22
while (tc--) {
@@ -38,16 +46,20 @@ int main() {
38
46
}
39
47
40
48
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
+
42
53
for (int i = 0 ; i < n; i++) {
43
54
int looking_for = 0 ;
44
- for (int j = 31 ; j >= 0 ; j--) {
55
+ for (int j = 30 ; j >= 0 ; j--) {
45
56
if ((1 << j) & k) { // must follow along differently
46
57
looking_for = looking_for | ((1 << j) ^ ((1 << j) & a[i]));
47
58
} else { // we can try to find someone who has a different version at this point
48
59
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 ;
51
63
best = min (best, i - idx + 1 );
52
64
}
53
65
// otherwise assume this bit will be the same
@@ -56,13 +68,14 @@ int main() {
56
68
}
57
69
58
70
// 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 ;
61
74
best = min (best, i - idx + 1 );
62
75
}
63
76
64
77
int mask = ~0 ;
65
- for (int j = 0 ; j < 32 ; j++) {
78
+ for (int j = 0 ; j < 31 ; j++) {
66
79
last_seen[j][a[i] & mask] = i;
67
80
mask = mask ^ (1 << j);
68
81
}
@@ -75,4 +88,4 @@ int main() {
75
88
}
76
89
}
77
90
return 0 ;
78
- }
91
+ }
0 commit comments