Constant Sum
Time limit:
1000 ms
Memory limit: 128 MB
Memory limit: 128 MB
You have an array A of N integers. You have to perform Q operations of two types:
- Update: given two integers i and s, add s to Ai and decrease all the other elements of A by N−1s.
- Query: given an index i find the value of Ai.
Standard input
The first line contains two integers N and Q.
The second line contains the N elements of A.
Each of the following Q lines describes one operation. An update consists of three integers 0 i s, while a query consists of two inters 1 i.
Standard output
For each query print the answer on a distinct line.
Constraints and notes
- 2≤N≤105
- 1≤Q≤105
- 0≤Ai≤1000
- 1≤si≤1000
- An answer is considered correct if the absolute difference between it and the official answer is less than 10−6.
题目大意:在n个数字的数组中进行一下两种操作
1.在i位置增加s,同时减少除他外其他每个点s/(n-1)
2.查询i点的值
解题思路:这道题是一道典型的用树状数组进行区间修改单点查询的题
在这种题的背景下,对i这个位置增加x进行单点修改的时候,要将i之后的所有位置减去-x,这样可保证在单点查询的时候,直接查询i即可
这是与之前正常的区间查询单点修改不同的,同时在区间修改的时候,若是[x,y]这个区间修改,则在x这个位置进行加值操作,在y+1这个位置进行减小操作,这样就可以保证在[x,y]这个范围内进行了修改
Input | Output |
---|---|
4 6 1 2 4 3 0 2 6 1 1 0 1 3 1 3 0 1 2 1 4 | -1.0000000000 1.0000000000 -0.6666666667
|