Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import static java.lang.Integer.parseInt;
- class SUMSUM {
- private static long t[][] = new long[28][200005];
- private static int a[] = new int[100001];
- public static void main(String[] args) throws IOException {
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- int n, q;
- String s[];
- s = br.readLine().split("\\s");
- n = parseInt(s[0]);
- q = parseInt(s[1]);
- s = br.readLine().split("\\s");
- for (int i = 0; i < n; i++) {
- a[i] = parseInt(s[i]);
- update(i, a[i], n);
- }
- for (int i = 0; i < q; i++) {
- long ans = 0;
- s = br.readLine().split("\\s");
- if ("1".equals(s[0])) {
- int x = parseInt(s[1]);
- int p = parseInt(s[2]);
- update(--p, x, n);
- }
- else {
- long ones, zeros, effectivePairs ;
- int a, b;
- a = parseInt(s[2]);
- b = parseInt(s[3]);
- --a;
- --b;
- for (int j = 0; j < 28; j++) {
- effectivePairs = 0;
- ones = query(a, b, j, n);
- zeros = (b - a + 1) - ones;
- if ("OR".equals(s[1])) {
- effectivePairs = effectivePairs + ones * (ones - 1) / 2;
- effectivePairs = effectivePairs + (ones * zeros);
- } else if ("AND".equals(s[1])) {
- effectivePairs = ones * (ones - 1) / 2;
- } else {//xor
- effectivePairs = ones * zeros;
- }
- ans += (1 << j) * effectivePairs;
- }
- System.out.println(ans);
- }
- }
- }
- private static int query(int l, int r, int j, int n) {
- l += n;
- r += n;
- int ans = 0;
- for (; l <= r; l >>= 1, r >>= 1) {
- if ((l & 1) == 1) {
- ans += t[j][l++];
- }
- if ((r & 1) != 1) {
- ans += t[j][r--];
- }
- }
- return ans;
- }
- public static void update(int i, int x, int n) {
- i += n;
- for (int j = 0; j < 28; j++) {
- int currentBit = ((1 << j) & x) > 0 ? 1 : 0;
- t[j][i] = currentBit;
- for (int k=i/2; k > 0; k >>= 1) {
- t[j][k] = t[j][k << 1] + t[j][(k << 1) + 1];
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment