SHARE
    TWEET
    Vince14

    /<> 12844 (lazy propagation xor)

    Oct 28th, 2023
    146
    0
    Never
    Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
    C++ 2.16 KB | Source Code | 0 0
    1. #include <iostream>
    2. #include <string>
    3. #include <cstring>
    4. #include <algorithm>
    5. #include <cmath>
    6. #include <vector>
    7. #include <set>
    8. #include <map>
    9. #include <stack>
    10. #include <queue>
    11. #include <deque>
    12. #include <unordered_map>
    13. #include <numeric>
    14. #include <iomanip>
    15. using namespace std;
    16. #define pii pair<long long, long long>
    17. #define ll long long
    18. #define FAST ios_base::sync_with_stdio(false); cin.tie(NULL)
    19. const long long dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
    20. const long long dl[2] = {1, -1};
    21. const long long MOD = 1000000007;
    22. const long long MAXN = 500005;
    23. int n, m;
    24. int arr[MAXN];
    25. int seg[MAXN * 4], lazy[MAXN * 4];
    26. void prop(int x, int s, int e){
    27. if(lazy[x] == 0) return;
    28. if((e - s) % 2 == 0) seg[x] ^= lazy[x];
    29. if(s == e){
    30. lazy[x] = 0;
    31. }
    32. else{
    33. lazy[x * 2] ^= lazy[x];
    34. lazy[x * 2 + 1] ^= lazy[x];
    35. lazy[x] = 0;
    36. }
    37. }
    38. void build(int x, int s, int e){
    39. if(s == e){
    40. seg[x] = arr[s];
    41. return;
    42. }
    43. int mid = (s + e) / 2;
    44. build(x * 2, s, mid);
    45. build(x * 2 + 1, mid + 1, e);
    46. seg[x] = seg[x * 2] ^ seg[x * 2 + 1];
    47. }
    48. void update(int x, int s, int e, int a, int b, int k){
    49. prop(x, s, e);
    50. if(b < s || e < a) return;
    51. if(a <= s && e <= b){
    52. lazy[x] ^= k;
    53. prop(x, s, e);
    54. return;
    55. }
    56. int mid = (s + e) / 2;
    57. update(x * 2, s, mid, a, b, k);
    58. update(x * 2 + 1, mid + 1, e, a, b, k);
    59. seg[x] = seg[x * 2] ^ seg[x * 2 + 1];
    60. }
    61. int query(int x, int s, int e, int a, int b){
    62. prop(x, s, e);
    63. if(b < s || e < a) return 0;
    64. if(a <= s and e <= b){
    65. return seg[x];
    66. }
    67. int mid = (s + e) / 2;
    68. return query(x * 2, s, mid, a, b) ^ query(x * 2 + 1, mid + 1, e, a, b);
    69. }
    70. int main() {
    71. FAST;
    72. cin >> n;
    73. for(int i = 1; i <= n; i++){
    74. cin >> arr[i];
    75. }
    76. build(1, 1, n);
    77. cin >> m;
    78. for(int q, i, j, k, l = 0; l < m; l++){
    79. cin >> q >> i >> j;
    80. i++; j++;
    81. if(q == 1){
    82. cin >> k;
    83. update(1, 1, n, i, j, k);
    84. }
    85. else{
    86. cout << query(1, 1, n, i, j) << "\n";
    87. }
    88. }
    89. }
    Advertisement
    Add Comment
    Please, Sign In to add comment
    Public Pastes
    We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
    Not a member of Pastebin yet?
    Sign Up, it unlocks many cool features!

    AltStyle によって変換されたページ (->オリジナル) /