bhupixb

XOR from L to R using BIT

Sep 12th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.54 KB | None | 0 0
  1. // KEEP IT SIMPLE //
  2. #include<bits/stdc++.h>
  3. using namespace std;
  4. #define int long long
  5. #define MAX 100
  6. const int inf = 9e18;
  7. const double pi = acos(-1.0);
  8. const int M = 1e9 + 7;
  9. const int N = 1e5 + 5;
  10. int gccd(int a, int b){ if(a == 0) return b; return gccd(b % a, a); }
  11. int power(int x, unsigned int y){ int temp; if( y == 0) return 1; temp = power(x, y/2);
  12. if (y%2 == 0) return temp * temp; else return x * temp * temp;
  13. }
  14. int res[1000005], n;
  15. int BITree[1000005];
  16. int getSum(int index)
  17. {
  18. int sum = 0;
  19. while (index>0)
  20. {
  21. sum ^= BITree[index];
  22. index -= index & (-index);
  23. }
  24. return sum;
  25. }
  26. void updateBIT(int index, int val)
  27. {
  28. while (index <= n)
  29. {
  30. BITree[index] ^= val;
  31. index += index & (-index);
  32. }
  33. }
  34. int32_t main()
  35. {
  36. ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);
  37. cin >> n;
  38. for(int i = 1 ; i <= n; i++)
  39. {
  40. cin >> res[i];
  41. updateBIT(i, res[i]);
  42. }
  43. int q;
  44. cin >> q;
  45. while(q--)
  46. {
  47. int l, r, pos;
  48. cin >> l >> r >> pos;
  49. int ans = getSum(r) ^ getSum(l - 1);
  50. int pro(0), p = ans;
  51. while(ans)
  52. {
  53. ans >>= 1;
  54. pro <<= 1;
  55. pro |= 1;
  56. }
  57. //cout << p << " " << pro << endl;
  58. if(p == pro)
  59. {
  60. cout << -1 << endl;
  61. }
  62. else
  63. {
  64. cout << (p ^ pro) << endl;
  65. updateBIT(pos, res[pos]);
  66. res[pos] = (p ^ pro);
  67. updateBIT(pos, res[pos]);
  68. }
  69. }
  70. return 0;
  71. }
Add Comment
Please, Sign In to add comment