您的位置:首页 > 其它

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_iA​i​​ anddecrease all the other elements of AA by \large\frac{s}{N-1}​N−1​​s​​.Query: given an index i find the value of A_iA​i​​.

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≤10​5​​ 1 \leqQ \leq 10^51≤Q≤10​5​​ 0 \leqA_i \leq 10000≤A​i​​≤1000 1 \leqs_i \leq 10001≤s​i​​≤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]这个范围内进行了修改
InputOutput
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));
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: