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
这题花了三个小时没做出来,可能是最近有点脑残。。刚开始用树状数组做了tle,又改成线段树,发现线段树还是tle,最后又改成树状数组。。有一点技巧
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<cstdio>
#include<iomanip>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define pi acos(-1)
#define ll long long
#define mod 1000000007 using namespace std; const int N=+,maxn=+,inf=1e9+; ll a[N],m[N];
void add(int i,int v)
{
while(i<=N){
m[i]+=v;
i+=i&(-i);
}
}
ll sum(int i)
{
ll ans=;
while(i>){
ans+=m[i];
i-=i&(-i);
}
return ans;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie();
int n;
cin>>n;
memset(m,,sizeof m);
for(int i=;i<=n;i++)cin>>a[i];
int k;
cin>>k;
while(k--){
int p,l,r,d;
cin>>p;
if(p==)
{
cin>>d;
ll ans=;
for(int i=;i*i<=d;i++)
{
if(d%i==)
{
ans+=sum(i);
if(i*i!=d)ans+=sum(d/i);
}
}
cout<<ans+a[d]<<endl;
}
else
{
cin>>l>>r>>d;
add(l,d);
add(r+,-d);
}
}
return ;
}

ural Ambitious Experiment 树状数组的更多相关文章

  1. Ural 2062:Ambitious Experiment(树状数组 || 分块)

    http://acm.timus.ru/problem.aspx?space=1&num=2062 题意:有n个数,有一个值,q个询问,有单点询问操作,也有对于区间[l,r]的每个数i,使得n ...

  2. URAL 2062 树状数组

    一个长度为n的数组 每次对lr区间进行修改 如果要修改i 则对i i*2 i*3...都修改 最后单点查询值 思想是利用树状数组维护每一个区间的更新值 查询的时候得出这个点的所有因子的查询值的和 加上 ...

  3. ural 1989(树状数组+多项式hash)

    题意:给出一个字符串.有两种操作,一个是p a b,问字符串从位置a到位置b的子串是否是一个回文子串.还有一个操作 c a b,把字符串位置a的字符替换为b. 题解:由于字符串长度为1e5且问的次数也 ...

  4. HDU1541 树状数组

    Stars Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submi ...

  5. POJ 2352Stars 树状数组

    Stars Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 42898   Accepted: 18664 Descripti ...

  6. POJ-2352 Stars 树状数组

    Stars Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 39186 Accepted: 17027 Description A ...

  7. hdu 1541/poj 2352:Stars(树状数组,经典题)

    Stars Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submi ...

  8. Stars(树状数组或线段树)

    Stars Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 37323 Accepted: 16278 Description A ...

  9. POJ2352Stars【树状数组】

    Stars Description Astronomers often examine star maps where stars are represented by points on a pla ...

随机推荐

  1. 人工智能范畴及深度学习主流框架,谷歌 TensorFlow,IBM Watson认知计算领域IntelligentBehavior介绍

    人工智能范畴及深度学习主流框架,谷歌 TensorFlow,IBM Watson认知计算领域IntelligentBehavior介绍 ================================ ...

  2. python之路----hashlib模块

    在平时生活中,有很多情况下,你在不知不觉中,就用到了hashlib模块,比如:注册和登录认证注册和登录认真过程,就是把注册用的账户密码进行:加密 --> 解密 的过程,在加密.解密过程中,用的了 ...

  3. ~/.bashrc文件写错, 导致Linux全部命令丢失

    问题 今天写bashrc文件的时候, 不小心把PATH结尾带错了,当时不知道,直接就source了, 后来出来的时候发现命令全部提示找不到了... 解决 重新赋予环境变量PATH就行 export P ...

  4. JAVA I/O(六)多路复用IO

    在前边介绍Socket和ServerSocket连接交互的过程中,读写都是阻塞的.套接字写数据时,数据先写入操作系统的缓存中,形成TCP或UDP的负载,作为套接字传输到目标端,当缓存大小不足时,线程会 ...

  5. arm linux下启动ftp服务

    1.环境: /home/jello # uname -aLinux  3.10.0 #2 SMP Mon Mar 6 17:52:09 CST 2017 armv7l GNU/Linux 2.使用tc ...

  6. MacBook PRO蓝牙无法搜索设备

    背景 经常把MacBook合上盖子就塞进包里,用时打开盖子就继续操作,偶尔会出现刚刚还在用的罗技蓝牙鼠标,重新打开笔记本后就连接不上了,而且也无法搜索到周边的蓝牙设备. 解决方案 快捷键:Option ...

  7. 分布式系统一致性算法Raft

    Raft 算法也是一种少数服从多数的算法,在任何时候一个服务器可以扮演以下角色之一:Leader:负责 Client 交互 和 log 复制,同一时刻系统中最多存在一个Follower:被动响应请求 ...

  8. JavaScript:Object属性方法

    Object的属性(firebug中没有找到) var pro={ city:"shanghai", list:[,,,,] } var Person=function(name, ...

  9. 三羊献瑞|2015年蓝桥杯B组题解析第三题-fishers

    三羊献瑞 观察下面的加法算式: 祥 瑞 生 辉 三 羊 献 瑞 三 羊 生 瑞 气 (如果有对齐问题,可以参看[图1.jpg]) 其中,相同的汉字代表相同的数字,不同的汉字代表不同的数字. 请你填写& ...

  10. 委托的begininvoke

    http://blog.csdn.net/cml2030/article/details/2172854 http://blog.163.com/weizhiyong_111/blog/static/ ...