CS Academy Round #30 (Div. 2 only) C.Constant Sum(树状数组,区间修改,单点查询模板)
2017-05-25 20:28
471 查看
Constant Sum
Time limit: 1000 msMemory limit: 128 MBYou have an array AA of NN integers.You have to perform QQ operationsof two types:Update: given two integers ii and ss,add ss to A_iAi anddecrease all the other elements of AA by \large\frac{s}{N-1}N−1s.Query: given an index i find the value of A_iAi.Standard input
The first line contains two integers NN and QQ.The second line contains the NN elementsof AA.Each of the following QQ linesdescribes one operation. An update consists of three integers 0\i\ s0 i s,while a query consists of two inters 1\i1 i.Standard output
For each query print the answer on a distinct line.Constraints and notes
2 \leqN\leq 10^52≤N≤105 1 \leqQ \leq 10^51≤Q≤105 0 \leqA_i \leq 10000≤Ai≤1000 1 \leqs_i \leq 10001≤si≤1000 An answer is considered correct if the absolute difference between it and the official answer is less than 10^{-6}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 #include<iostream> #include<cstdio> #include<stdio.h> #include<cstring> #include<cstdio> #include<climits> #include<cmath> #include<vector> #include <bitset> #include<algorithm> #include <queue> #include<map> #define inf 9999999; using namespace std; int n,T; double a[100005]; int lowbit(int x) { return x&(-x); } void update(int x,double y) { while(x<=n) { a[x]+=y; x+=lowbit(x); } } double query(int x) { double ans=0; while(x>0) { ans+=a[x]; x-=lowbit(x); } return ans; } int main() { int i,k; int x; double y; cin>>n>>T; for(i=1;i<=n;i++) { cin>>y; update(i,y); update(i+1,-y); } while(T--) { cin>>k; if(k==0) { cin>>x>>y; update(x,y); update(x+1,-y); update(1,-y/(n-1)); update(x,y/(n-1)); update(x+1,-y/(n-1)); } else { cin>>x; printf("%0.10lf\n",query(x)); } } } |
相关文章推荐
- P3374 【模板】树状数组 1 单点修改与区间查询
- 树状数组模板(max,min,区间和,区间修改,单点查询)
- (模板)树状数组 (区间修改,单点查询)
- 树状数组模板2——区间修改,单点查询
- 树状数组模板1——单点修改区间查询
- hdu 2642 二维树状数组 单点更新区间查询 模板水题
- 【codevs1080】【树状数组】 单点修改 区间查询
- POJ-2763 Housewife Wind (树链剖分 入门题 树状数组 单点修改 区间查询)
- HDU 1556-Color the ball(树状数组-区间修改 单点查询)
- 【codevs1081】【树状数组】区间修改 单点查询
- 模板(线段树 + 树状数组 + 区间修改 + 区间查询)eg:POJ 3468 - A Simple Problem with Integers
- 树状数组 区间修改,单点查询;
- 树状数组(二叉索引树 BIT Fenwick树) *【一维基础模板】(查询区间和+修改更新)
- 树状数组单点修改区间查询
- hdu 2642二维树状数组 单点更新区间查询 模板题
- Matrix 二维树状数组 区间修改+单点查询
- POJ 2155-Matrix(二维树状数组-区间修改 单点查询)
- 【原创】【数据结构】一维树状数组的基本操作(单点修改,区间查询) (HDU1166 敌兵布阵)
- 树状数组(单点修改区间查询、区间修改单点查询、区间修改区间查询)
- 树状数组模板(区间更新单点查询)