fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. using ll = long long;
  4.  
  5. void solve() {
  6. int n, q;
  7. cin >> n >> q;
  8.  
  9. vector<int> w(n);
  10. for (int i = 0; i < n; i++) cin >> w[i];
  11.  
  12. vector<int> threshold(n + 1, INT_MAX); // Stores min x for each score
  13. int last = 0; // Last processed position
  14.  
  15. for (int i = 0; i < n; i++) {
  16. int curr_x = w[i];
  17. int score = 1;
  18.  
  19. // Start eating from i-th position
  20. while (i - score >= 0 && curr_x >= w[i - score]) {
  21. curr_x ^= w[i - score]; // Update weight after eating
  22. score++; // Increase the score
  23. }
  24.  
  25. // Record the minimum x required to achieve this score
  26. threshold[score] = min(threshold[score], w[i]);
  27. }
  28.  
  29. // Fill in the minimum threshold for each score in increasing order
  30. for (int i = 1; i <= n; i++) {
  31. threshold[i] = min(threshold[i], threshold[i - 1]);
  32. }
  33.  
  34. // Answer queries using binary search
  35. while (q--) {
  36. int x;
  37. cin >> x;
  38.  
  39. // Find max score where threshold[score] <= x
  40. int score = upper_bound(threshold.begin(), threshold.end(), x) - threshold.begin() - 1;
  41. cout << score << "\n";
  42. }
  43. }
  44.  
  45. int main() {
  46. ios::sync_with_stdio(false);
  47. cin.tie(nullptr);
  48.  
  49. int t;
  50. cin >> t;
  51. while (t--) {
  52. solve();
  53. }
  54.  
  55. return 0;
  56. }
  57.  
Success #stdin #stdout 0s 5292KB
stdin
3
1 1
5
6
4 8
1 5 4 11
8
9
10
11
12
13
14
15
10 9
10 4 3 9 7 4 6 1 9 4
2
6
5
6
9
8
6
2
7
stdout
1
4
4
4
4
4
4
4
4
10
10
10
10
10
10
10
10
10