URAL 2062 Ambitious Experiment (树状数组)

During several decades, scientists from planet Nibiru are working to create an engine that would allow spacecrafts to fall into hyperspace and move there with superluminal velocity. To check whether their understanding of properties of hyperspace is right, scientists have developed the following experiment.
A chain of  n particles is placed in hyperspace. Positions of particles in the chain are numbered from 1 to n. Initially,  ith particle has charge  a  i.
According to the current theory, if particle number  i got special radiation with power  d, oscillations would spread by hyperspace and increase by  d charge of particles with numbers  i, 2  i, 3  i and so on (i.e. with numbers divisible by  i).
Using a special device, scientists can direct the radiation of the same power at a segment of adjacent particles. For example, suppose that initially there were 6 particles with zero charges, and scientists have sent radiation with power five to particles with numbers 2 and 3. Then charge of 2nd, 3rd, and 4th particles will increase to five, and charge of 6th particle will increase to ten (the oscillations will reach it twice). Charge of other particles won’t change.
Charge of particles can’t change without impact of the device.
During the experiment, the scientists plan to perform actions of the following types:
  1. Measure current charge of the particle number i.
  2. Direct radiation with power d at particles with numbers from l to r inclusive.
Your program will be given a list of performed actions. For every action of the first type the program should output value of expected charge of the particle calculated in accordance with the current theory described above.
If the expected charges of the particles coincide with charges measured during the experiment, it will turn out that scientists’ understanding of hyperspace is right, and they will be able to start building of the hyperdrives. Then inhabitants of Nibiru will finally meet their brothers from Earth in just a few years!
Input
The first line contains a single integer  n — number of particles (1 ≤  n ≤ 3 · 10  5).
The second line contains  n integers  a  i separated by spaces — initial charges of the particles (0 ≤  a  i ≤ 10 6).
The third line contains a single integer  q — number of actions in the experiment (1 ≤  q ≤ 3 · 10  5).
Each of the following  q lines contain two or four integers — a description of the next action in one of the following formats:
  • i — measure current charge of the particle number i (1 ≤ i ≤ n).
  • l r d — direct radiation with power d at particles with numbers from l to r inclusive (1 ≤ l ≤ r ≤ n, 0 ≤ d ≤ 106).
Output
For each query output the expected charge of the  ith particle.
Example
input output
3
1 2 3
2
2 1 3 5
1 2
12
6
1 2 1 4 5 6
5
2 2 4 2
1 3
1 4
2 3 5 1
1 5
3
8
6

对于要求的点他的答案贡献由他的因子产生且因子在区间[l,r]中。对于一个查询ans=a[p]+sigma(sum(j)) (j为p的约数,sum为数组1到j的和)区间修改时仅需修改区间端点值:c[l]+=v,c[r+1]-=v

#include<iostream>
using namespace std;
typedef long long LL;
LL a[300005],c[300005];
int n;
int lowbit(int x)  
{  
    return x&(-x);  
}  
void add(int x,int w)  
{  
    while(x<=n)  
    {  
        c[x]+=w;  
        x+=lowbit(x);  
    }  
}  
LL sum(int x)  
{  
    LL tot=0;  
    while(x)  
    {  
        tot+=c[x];  
        x-=lowbit(x);  
    }  
    return tot;  
}  
int main()
{
	while(~scanf("%d",&n))
	{
		for(int i=1;i<=n;i++)
		{
			scanf("%d",&a[i]);
		}
		int q;
		scanf("%d",&q);
		int type;
		while(q--)
		{
			scanf("%d",&type);
			if(type==2)
			{
				int l,r,v;
				scanf("%d%d%d",&l,&r,&v);
				add(l,v);
				add(r+1,-v);
			}
			else
			{
				int ask;
				scanf("%d",&ask);
				long long ans=a[ask];
				for(int i=1;i*i<=ask;i++)
				{
					if(ask%i==0)
					{
						ans+=sum(i);
						if(i!=ask/i) 
						ans+=sum(ask/i);
					}
				}
				cout<<ans<<endl;
			}
		}
	}
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值