fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define int long long
  4. #define vi vector<int>
  5. #define zip(i,a,b) for(int i=a;i<b;i++)
  6. #define rzip(i,a,b) for(int i=a;i>=b;i--)
  7. #define test int t;cin>>t; while(t--)
  8. #define en '\n'
  9.  
  10.  
  11. int maximumXOR(int n, int l, int r)
  12. {
  13. int x = 0;
  14. for (int i = log2(r); i >= 0; --i)
  15. {
  16. if (n & (1 << i))
  17. {
  18. if ((x > r) || (x + (1 << i) - 1 < l))
  19. x ^= (1 << i);
  20. }
  21. else
  22. {
  23. if ((x ^ (1 << i)) <= r)
  24. x ^= (1 << i);
  25. }
  26. }
  27. return n ^ x;
  28. }
  29.  
  30.  
  31.  
  32. int32_t main()
  33. {
  34. // i have already declared int as long long
  35. int n;
  36. cin >> n;
  37. int a[n];
  38. zip(i, 0, n)
  39. {
  40. cin >> a[i];
  41. }
  42. sort(a, a + n);
  43. vi pre(n);
  44. vi suff(n);
  45. pre[0] = a[0];
  46. zip(i, 1, n)
  47. {
  48. pre[i] = (pre[i - 1] ^ a[i]);
  49. }
  50. suff[n - 1] = a[n - 1];
  51. rzip(i, n - 2, 0)
  52. {
  53. suff[i] = (suff[i + 1] ^ a[i]);
  54. }
  55. int ans = suff[0];
  56. zip(i, 0, n - 2)
  57. {
  58. // as we can check only one element first take xor of pre[i]^suf[i+2] and change value accordingly.
  59. int x = pre[i];
  60. int y = suff[i + 2];
  61. x = (x ^ suff[i + 2]);
  62. int p = a[i + 1];
  63. ans = max(ans, maximumXOR(x, 0, p));
  64. }
  65. if (n > 1)
  66. {
  67. ans = max(ans, maximumXOR(suff[1], 0, a[0]));
  68. ans = max(ans, maximumXOR(pre[n - 2], 0, a[n - 1]));
  69. }
  70. cout << ans << en;
  71.  
  72. return 0;
  73. }
  74.  
  75.  
  76.  
Runtime error #stdin #stdout 0s 4308KB
stdin
Standard input is empty
stdout
Standard output is empty