2062. Ambitious Experiment
Time limit: 3.0 second
Memory limit: 128 MB
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 ai.
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, 2i, 3iand 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:
- Measure current charge of the particle number i.
- 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 · 105).
The second line contains n integers ai separated by spaces — initial charges of the particles (0 ≤ ai ≤ 106).
The third line contains a single integer q — number of actions in the experiment (1 ≤ q ≤ 3 · 105).
Each of the following q lines contain two or four integers — a description of the next action in one of the following formats:
- 1 i — measure current charge of the particle number i (1 ≤ i ≤ n).
- 2 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.
Samples
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 |
Problem Author: Alexey Danilyuk (prepared by Nikita Sivukhin)
Problem Source: Ural Regional School Programming Contest 2015
Tags: data structures (hide tags for unsolved problems)
Difficulty: 478
题意:给n个数,两种操作:1、询问x的值,2、修改l~r的值,对于每个l<=i<=r的i,都对 i,2i,3i....这些点加d
分析:首先很容易想到分块做法
对于每个点x,它对于其他点(2x,3x,....)的贡献都是一样的
那么对于每个查询的点x,对它有贡献的点就是是他的因数的那些点
那么修改就分段sqrt(n)暴力修改
查询就sqrt(x)枚举约数,查询到每一个贡献点的值加起来
1 /** 2 Create By yzx - stupidboy 3 */ 4 #include <cstdio> 5 #include <cstring> 6 #include <cstdlib> 7 #include <cmath> 8 #include <deque> 9 #include <vector> 10 #include <queue> 11 #include <iostream> 12 #include <algorithm> 13 #include <map> 14 #include <set> 15 #include <ctime> 16 #include <iomanip> 17 using namespace std; 18 typedef long long LL; 19 typedef double DB; 20 #define MIT (2147483647) 21 #define INF (1000000001) 22 #define MLL (1000000000000000001LL) 23 #define sz(x) ((int) (x).size()) 24 #define clr(x, y) memset(x, y, sizeof(x)) 25 #define puf push_front 26 #define pub push_back 27 #define pof pop_front 28 #define pob pop_back 29 #define mk make_pair 30 31 inline int getInt() 32 { 33 int ret = 0; 34 char ch = ‘ ‘; 35 bool flag = 0; 36 while(!(ch >= ‘0‘ && ch <= ‘9‘)) 37 { 38 if(ch == ‘-‘) flag ^= 1; 39 ch = getchar(); 40 } 41 while(ch >= ‘0‘ && ch <= ‘9‘) 42 { 43 ret = ret * 10 + ch - ‘0‘; 44 ch = getchar(); 45 } 46 return flag ? -ret : ret; 47 } 48 49 const int N = 300010, M = 560; 50 int n, m; 51 LL block[N / M + 1][M], tag[N / M + 1], arr[N]; 52 53 inline int getBlockIndex(int x) 54 { 55 return x / M; 56 } 57 58 inline int getIndex(int x) 59 { 60 return x % M; 61 } 62 63 inline void input() 64 { 65 cin >> n; 66 for(int i = 0; i < n; i++) cin >> arr[i]; 67 } 68 69 inline LL work(int x) 70 { 71 int b = getBlockIndex(x), idx = getIndex(x); 72 return block[b][idx] + tag[b]; 73 } 74 75 inline LL query(int x) 76 { 77 x++; 78 LL ret = 0; 79 for(int i = 1; i * i <= x; i++) 80 if(x % i == 0) 81 { 82 ret += work(i - 1); 83 if(x / i != i) ret += work(x / i - 1); 84 } 85 return ret; 86 } 87 88 inline void change(int l, int r, int d) 89 { 90 int left = getBlockIndex(l), right = getBlockIndex(r); 91 for(int i = left + 1; i <= right - 1; i++) tag[i] += d; 92 if(left < right) 93 { 94 for(int i = getIndex(l); i < M; i++) block[left][i] += d; 95 for(int i = 0; i <= getIndex(r); i++) block[right][i] += d; 96 } 97 else 98 { 99 for(int i = getIndex(l); i <= getIndex(r); i++) 100 block[left][i] += d; 101 } 102 } 103 104 inline void solve() 105 { 106 for(cin >> m; m--; ) 107 { 108 int opt, l, r, x; 109 cin >> opt; 110 if(opt == 1) 111 { 112 cin >> x; 113 x--; 114 cout << query(x) + arr[x] << "\n"; 115 } 116 else 117 { 118 cin >> l >> r >> x; 119 l--, r--; 120 change(l, r, x); 121 } 122 } 123 } 124 125 int main() 126 { 127 ios::sync_with_stdio(0); 128 input(); 129 solve(); 130 return 0; 131 }