2020-04-03 12:35:23
Problem Description:
A known number of columns, you need to perform the following two operations:
1. a number of sections each plus X
2. obtaining a number of value
inputs:
the original array is A. For convenience, A [0] is 0. The actual number of columns from the A [1] starts.
Operation given by the 4-tuple.
For each 4-tuple (a, b, c, d ):
If a = 0 requires A [b] -A [c] values have increased interval D (modified).
If required to give a value a = 1 A [b] a. Wherein c, d inoperative (query).
Output:
To reduce the amount of output data. 2 operation (query) Returns the value of all the exclusive OR (^).
Sample
Example 1:
输入:[0,1,2,3,4],[[1,1,0,0],[0,1,2,1],[1,2,0,0]] 输出:2 解释: 第一个操作返回A [1] = 1 第二个操作改变A为 [0,2,3,3,4] 第三个操作返回A [1] = 3 所以 1 ^ 3 = 2
Sample 2:
输入:[0,1],[[1,1,0,0]] 输出:1 解释:第一个操作返回A [1] = 1,答案为 1。
Precautions
- Sequence length <= 10000
- Operand <= 50000
Problem Solving:
int[] bit; public long intervalsAddAndGetValue(int[] A, int[][] operations) { int n = A.length; bit = new int[n]; for (int i = 1; i < n; i++) { update(i, A[i]); update(i + 1, -A[i]); } long res = 0; for (int[] op : operations) { if (op[0] == 0) { update(op[1], op[3]); update(op[2] + 1, -op[3]); } else { res ^= query(op[1]); } } return res; } private void update(int idx, int delta) { for (int i = idx; i < bit.length; i += (i & -i)) { bit[i] += delta; } } private int query(int idx) { int res = 0; for (int i = idx; i > 0; i -= (i & -i)) { res += bit[i]; } return res; }