Guest User

SUMSUM

a guest
Apr 16th, 2018
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.63 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4.  
  5. import static java.lang.Integer.parseInt;
  6.  
  7. class SUMSUM {
  8.     private static long t[][] = new long[28][200005];
  9.  
  10.     private static int a[] = new int[100001];
  11.     public static void main(String[] args) throws IOException {
  12.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  13.         int n, q;
  14.         String s[];
  15.         s = br.readLine().split("\\s");
  16.         n = parseInt(s[0]);
  17.         q = parseInt(s[1]);
  18.         s = br.readLine().split("\\s");
  19.         for (int i = 0; i < n; i++) {
  20.             a[i] = parseInt(s[i]);
  21.             update(i, a[i], n);
  22.         }
  23.         for (int i = 0; i < q; i++) {
  24.             long ans = 0;
  25.             s = br.readLine().split("\\s");
  26.             if ("1".equals(s[0])) {
  27.                 int x = parseInt(s[1]);
  28.                 int p = parseInt(s[2]);
  29.                 update(--p, x, n);
  30.             }
  31.             else {
  32.                 long ones, zeros, effectivePairs ;
  33.                 int a, b;
  34.                 a = parseInt(s[2]);
  35.                 b = parseInt(s[3]);
  36.                 --a;
  37.                 --b;
  38.                 for (int j = 0; j < 28; j++) {
  39.                     effectivePairs = 0;
  40.                     ones = query(a, b, j, n);
  41.                     zeros = (b - a + 1) - ones;
  42.                     if ("OR".equals(s[1])) {
  43.                         effectivePairs = effectivePairs + ones * (ones - 1) / 2;
  44.                         effectivePairs = effectivePairs + (ones * zeros);
  45.                     } else if ("AND".equals(s[1])) {
  46.                         effectivePairs = ones * (ones - 1) / 2;
  47.                     } else {//xor
  48.                         effectivePairs = ones * zeros;
  49.                     }
  50.                     ans += (1 << j) * effectivePairs;
  51.                 }
  52.                 System.out.println(ans);
  53.             }
  54.         }
  55.     }
  56.  
  57.     private static int query(int l, int r, int j, int n) {
  58.         l += n;
  59.         r += n;
  60.         int ans = 0;
  61.         for (; l <= r; l >>= 1, r >>= 1) {
  62.             if ((l & 1) == 1) {
  63.                 ans += t[j][l++];
  64.             }
  65.             if ((r & 1) != 1) {
  66.                 ans += t[j][r--];
  67.             }
  68.         }
  69.         return ans;
  70.     }
  71.  
  72.     public static void update(int i, int x, int n) {
  73.         i += n;
  74.         for (int j = 0; j < 28; j++) {
  75.             int currentBit = ((1 << j) & x) > 0 ? 1 : 0;
  76.             t[j][i] = currentBit;
  77.             for (int k=i/2; k > 0; k >>= 1) {
  78.                 t[j][k] = t[j][k << 1] + t[j][(k << 1) + 1];
  79.             }
  80.         }
  81.     }
  82. }
Add Comment
Please, Sign In to add comment