fork(6) download
  1. #include <bits/stdc++.h>
  2. #include <ext/pb_ds/assoc_container.hpp>
  3. #include <ext/pb_ds/tree_policy.hpp>
  4.  
  5. using namespace std;
  6. using namespace __gnu_pbds;
  7.  
  8. #define fi first
  9. #define se second
  10. #define mp make_pair
  11. #define pb push_back
  12. #define fbo find_by_order
  13. #define ook order_of_key
  14.  
  15. typedef long long ll;
  16. typedef pair<ll,ll> ii;
  17. typedef vector<int> vi;
  18. typedef long double ld;
  19. typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;
  20. typedef set<int>::iterator sit;
  21. typedef map<int,int>::iterator mit;
  22. typedef vector<int>::iterator vit;
  23.  
  24. int a[300001];
  25.  
  26. vi st[1200001];
  27.  
  28. void combine(int id)
  29. {
  30. int l = id*2; int r = (l^1);
  31. st[id].resize(st[l].size()+st[r].size());
  32. merge(st[l].begin(),st[l].end(),st[r].begin(),st[r].end(),st[id].begin());
  33. }
  34.  
  35. void build(int id, int l, int r)
  36. {
  37. if(r-l<2)
  38. {
  39. st[id].pb(a[l]);
  40. return ;
  41. }
  42. int mid=(l+r)>>1;
  43. build(id*2,l,mid);
  44. build(id*2+1,mid,r);
  45. combine(id);
  46. }
  47.  
  48. const int LG = 20;
  49.  
  50. int solve(vi &vec, int v)
  51. {
  52. /*
  53. cout<<"VEC : ";
  54. for(int i = 0; i < vec.size(); i++)
  55. {
  56. cout<<vec[i]<<' ';
  57. }
  58. cout<<'\n';
  59. */
  60. int l = 0; int r = vec.size() - 1;
  61. int cur = 0;
  62. int ans = 0;
  63. for(int i = LG - 1; i >= 0; i--)
  64. {
  65. if(v&(1<<i))
  66. {
  67. int x = lower_bound(vec.begin()+l, vec.begin()+r+1, cur+(1<<i)) - vec.begin();
  68. if(l<=x-1)
  69. {
  70. r = min(r, x-1);
  71. ans+=(1<<i);
  72. }
  73. else
  74. {
  75. cur+=(1<<i);
  76. }
  77. }
  78. else
  79. {
  80. int x = lower_bound(vec.begin()+l, vec.begin()+r+1, cur+(1<<i)) - vec.begin();
  81. if(r>=x)
  82. {
  83. l = max(x,l);
  84. ans+=(1<<i);
  85. cur+=(1<<i);
  86. }
  87. else
  88. {
  89.  
  90. }
  91. }
  92. //cerr<<i<<' '<<l<<' '<<r<<' '<<ans<<'\n';
  93. }
  94. return ans;
  95. }
  96.  
  97. int query(int id, int l, int r, int ql, int qr, int v)
  98. {
  99. if(ql<=l&&r<=qr) return solve(st[id],v);
  100. if(qr<=l||r<=ql) return -1;
  101. int mid = (l+r)>>1;
  102. return max(query(id*2,l,mid,ql,qr,v),query(id*2+1,mid,r,ql,qr,v));
  103. }
  104.  
  105. int main()
  106. {
  107. ios_base::sync_with_stdio(0); cin.tie(0);
  108. int n; cin >> n;
  109. for(int i = 0; i < n; i++) cin>>a[i];
  110. build(1,0,n);
  111. int q; cin >> q;
  112. while(q--)
  113. {
  114. int l, r, x;
  115. cin>>l>>r>>x;
  116. l--; r--;
  117. cout<<query(1,0,n,l,r+1,x)<<'\n';
  118. }
  119. }
  120.  
Success #stdin #stdout 0.01s 46384KB
stdin
Standard input is empty
stdout
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1