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. Linux基础命令---gzip

    gzip gzip通过Lempel-ziv算法来压缩文件,压缩的时候保留每个文件的所有者.权限.修改时间.对于符号链接,gzip将会忽略它. 如果压缩的文件名对其文件系统来说太长,则gzip将截断它. ...

  2. Linux基础命令---bc

    bc bc是一种算数语言,其语法和c语言类似,可以交互执行.通过命令行选项可以获得一个标准的数学库.如果请求,在处理任何文件之前定义数学库.BC从处理所有文件的代码开始.命令行中列出的文件按所列顺序排 ...

  3. 05: python中的函数

    1.1 python 函数   1.函数作用 定义: 函数是指将一组语句的集合通过一个名字(函数名)封装起来,要想执行这个函数,只需调用其函数名即可 特性: 减少重复代码 使程序变的可扩展 使程序变得 ...

  4. 04: Dom

    目录: 参考W3school 1.1 获取标签:直接选择器和间接选择器 1.2 操作标签 1.3 DOM其他操作 1.4 DOM绑定事件的三种方法 1.5 DOM可以绑定的事件 1.1 获取标签:直接 ...

  5. Android灯光系统--通知灯深入分析

    Android灯光系统--通知灯深入分析 通知的类别 声音 振动 闪灯 APP如何发出通知灯请求 getSystemService(获得通知服务) 构造notification 类别 其他参数(颜色, ...

  6. git下载速度太慢【学习笔记】

    使用了sshFQ的伙伴添加这个配置下载速度有极大的提升. git config --global http.proxy 'socks5://127.0.0.1:1080'

  7. Linux多线程--使用信号量同步线程【转】

    本文转载自:http://blog.csdn.net/ljianhui/article/details/10813469 信号量.同步这些名词在进程间通信时就已经说过,在这里它们的意思是相同的,只不过 ...

  8. Ansible 入门指南 - 学习总结

    概述 这周在工作中需要去修改 nginx 的配置,发现了同事在使用 ansible 管理者系统几乎所有的配置,从数据库的安装.nginx 的安装及配置.于是这周研究起了 ansible 的基础用法.回 ...

  9. MySQL命令行导出、导入数据库,备份数据库表

    MySQL导出数据库/数据表 1.首先,将你MySQL安装目录,例如C:\Program Files\MySQL\MySQL Server 5.7\bin添加到你的系统环境变量PATH中: 2.导出数 ...

  10. 获取Spring项目配置文件元素

    在开发中有时候要获取配置文件里的值,通常可以利用如下方式来读取: public class PropertyUtil { private static Properties p = new Prope ...