341D - Iahub and Xors - CodeForces Solution


data structures *2500

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define mxn 1010
ll multree[mxn][mxn][2], addtree[mxn][mxn][2];

ll yo(ll x) { return (x % 2); }

ll query2(ll tree[mxn][mxn][2], ll x, ll y) {
  ll mul = 0, add = 0;
  for (ll i = y; i > 0; i -= i & -i) {
    mul ^= tree[x][i][0];
    add ^= tree[x][i][1];
  }
  return (mul * yo(y)) ^ add;
}

ll query1(ll x, ll y) {
  ll mul = 0, add = 0;
  for (ll i = x; i > 0; i -= i & -i) {
    mul ^= query2(multree, i, y);
    add ^= query2(addtree, i, y);
  }
  return (mul * yo(x)) ^ add;
}

ll query(ll x1, ll y1, ll x2, ll y2) {
  return (query1(x2, y2) ^ query1(x1 - 1, y2) ^ query1(x2, y1 - 1) ^
          query1(x1 - 1, y1 - 1));
}

void upd2(ll tree[mxn][mxn][2], ll x, ll y, ll mul, ll add) {
  for (ll i = x; i < mxn; i += i & -i) {
    for (ll j = y; j < mxn; j += j & -j) {
      tree[i][j][0] ^= mul;
      tree[i][j][1] ^= add;
    }
  }
}

void upd1(ll x, ll y1, ll y2, ll mul, ll add) {
  upd2(multree, x, y1, mul, mul * yo(y1 - 1));
  upd2(multree, x, y2, mul, mul * yo(y2));
  upd2(addtree, x, y1, add, add * yo(y1 - 1));
  upd2(addtree, x, y2, add, add * yo(y2));
}

void upd(ll x1, ll y1, ll x2, ll y2, ll val) {
  upd1(x1, y1, y2, val, val * yo(x1 - 1));
  upd1(x2, y1, y2, val, val * yo(x2));
}

int main() {
  ios_base::sync_with_stdio(false), cin.tie(NULL);
  ll i, j, k, n, m, tt, x1, y1, x2, y2, q, val;
  cin >> n;
  cin >> q;
  while (q--) {
    cin >> tt;
    if (tt == 2) {
      cin >> x1 >> y1 >> x2 >> y2 >> val;
      upd(x1, y1, x2, y2, val);
    } else {
      cin >> x1 >> y1 >> x2 >> y2;
      cout << query(x1, y1, x2, y2) << "\n";
    }
  }
  return 0;
}
// ofwKQlZUxNLTgCtMklnkDeTJaaEEvyjUUvCqdRuwNOntcquIBeZYJAhOojCOEllbRiSNCHdrBjufoOMChMNZvYXpZGXBQLToRXycLhqrbyCvOORMyHVVQWlvadWkcZlT


Comments

Submit
0 Comments
More Questions

1529A - Eshag Loves Big Arrays
19. Remove Nth Node From End of List
925. Long Pressed Name
1051. Height Checker
695. Max Area of Island
402. Remove K Digits
97. Interleaving String
543. Diameter of Binary Tree
124. Binary Tree Maximum Path Sum
1465. Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts
501A - Contest
160A- Twins
752. Open the Lock
1535A - Fair Playoff
1538F - Interesting Function
1920. Build Array from Permutation
494. Target Sum
797. All Paths From Source to Target
1547B - Alphabetical Strings
1550A - Find The Array
118B - Present from Lena
27A - Next Test
785. Is Graph Bipartite
90. Subsets II
1560A - Dislike of Threes
36. Valid Sudoku
557. Reverse Words in a String III
566. Reshape the Matrix
167. Two Sum II - Input array is sorted
387. First Unique Character in a String