ural 2062 Ambitious Experiment

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:

  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 · 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:

  • 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.

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 }

时间: 2024-10-10 14:03:22

ural 2062 Ambitious Experiment的相关文章

URAL 2062 Ambitious Experiment(树状数组)

题目地址:http://acm.timus.ru/problem.aspx?space=1&num=2062 思路:对于x位置上的数,对其有贡献的数为位置为x的因数且属于修改区间[l,r]的数.所以对于查询操作ans=a[p]+sigma(sum(j)) (j为p的约数).区间修改时仅需修改区间端点值:c[l]+=d,c[r+1]-=d(当数x在某个无重复区间[l,r]时,每次求前x项的和时已包含该区间即已加上该数贡献,当x>r时,求前x项和时,该区间和为0,即数x贡献为0). #inclu

Ural 1081 Binary Lexicographic Sequence(DP)

题目地址:Ural 1081 先用dp求出每个长度下的合法序列(开头为1)的个数.然后求前缀和.会发现正好是一个斐波那契数列.然后每次判断是否大于此时长度下的最少个数,若大于,说明这一位肯定是1,若小于,则肯定是0.就这样不断输出出来即可. 代码如下: #include <iostream> #include <cstdio> #include <string> #include <cstring> #include <stdlib.h> #in

URAL 1684. Jack&#39;s Last Word KMP

题目来源:URAL 1684. Jack's Last Word 题意:输入a b 把b分成若干段 每一段都是a的前缀 思路:b为主串 然后用a匹配b 记录到b的i位置最大匹配的长度 然后分割 分割的时候要从后往前 如果a = abac b = abab 那么如果从前往后 首先覆盖了aba 然后b就不能覆盖了 从后往前就可以了 首先覆盖ab 下一次还是ab 因为已经记录了到i位置的最大匹配长度 根据长度从末尾倒退 每次倒退的时候只要是最大的匹配的长度 因为如果在某一次的递推 记录的最大匹配的前缀

ural 1272. Non-Yekaterinburg Subway

1272. Non-Yekaterinburg Subway Time limit: 1.0 secondMemory limit: 64 MB A little town started to construct a subway. The peculiarity of the town is that it is located on small islands, some of them are connected with tunnels or bridges. The mayor is

ural 1273. Tie

1273. Tie Time limit: 1.0 secondMemory limit: 64 MB The subway constructors are not angels. The work under the ground and… Well, they are not angels. And where have you seen angels? It is all in a lifetime! Show me first somebody who has never… and t

ural 1269. Obscene Words Filter

1269. Obscene Words Filter Time limit: 0.5 secondMemory limit: 8 MB There is a problem to check messages of web-board visitors for the obscene words. Your elder colleagues commit this problem to you. You are to write a program, which check if there i

ural 1218. Episode N-th: The Jedi Tournament

1218. Episode N-th: The Jedi Tournament Time limit: 1.0 secondMemory limit: 64 MB Decided several Jedi Knights to organize a tournament once. To know, accumulates who the largest amount of Force. Brought each Jedi his lightsaber with him to the tourn

ural 1217. Unlucky Tickets

1217. Unlucky Tickets Time limit: 1.0 secondMemory limit: 64 MB Strange people live in Moscow! Each time in the bus, getting a ticket with a 6-digit number, they try to sum up the first half of digits and the last half of digits. If these two sums ar

ural 1219. Symbolic Sequence

1219. Symbolic Sequence Time limit: 1.0 secondMemory limit: 64 MB Your program is to output a sequence of 1 000 000 lowercase Latin letters. This sequence should satisfy the following restrictions: Every letter occurs not more than 40 000 times in th