2014牡丹江网络预选赛E题(线段树)zoj3813

Alternating Sum Time Limit: 2 Seconds       Memory Limit: 65536 KB

There is a digit string S with infinite length. In addition, S is periodic and it can be formed by concatenating infinite repetitions of a base string P. For example, if P = 3423537, then S = 3423537342353734235373423537...

Let's define the alternating sum on substrings of S. Assume Sl..r is a substring of S from index l to index r (all indexes are 1-based), then the alternating sum of Sl..r is:

G( l,  r) =  S l -  S l+1 +  S l+2 - ... + (-1) r-l S r

For example, S2..10 = 423537342, then G(2, 10) = 4 - 2 + 3 - 5 + 3 - 7 + 3 - 4 + 2 = -3.

Now, you are given the base string P and you have to do many operations. There are only two kinds of operations:

  • 1 x d: set Px to d, d is a single digit.
  • 2 l r: find the sum of G(i, j) that l <= i <= j <= r.

For each second operation, you should output the sum modulo 109 + 7.

Input

There are multiple test cases. The first line of input contains an integer T indicating the number of test cases. For each test case:

The first line contains a digit string P (1 <= length(P) <= 100000).

The second line contains an integer Q (1 <= Q <= 100000) indicating the number of operations. Each of the following Q lines is an operation in such format:

  • 1 x d (1 <= x <= length(P), 0 <= d <= 9)
  • 2 l r (1 <= l <= r <= 1018)

Output

For each "2 l r" operation, output an integer, indicating the sum modulo 109 + 7.

Sample Input

2
324242
4
2 1 1
2 1 4
1 3 7
2 3 4
324242
6
2 1 1
1 3 7
2 2 4
1 3 4
2 7 10
2 1 30

Sample Output

3
20
14
3
8
20
870

题意:RT

思路:感觉做这种题要很有耐心= =

            首先不能被G(i,j)吓到,像我这种人就是比赛的时候一看到这个就被吓到了,果断放弃了

            事实上赛后我再冷静的把一个区间里的SUM (G(i,j))写出来后发现有个很明显的规律

            SUM(G(i,j))= p[i]*(j-i+l) + p[i+2]*(j-(i+2)+1) +......+p[j]*1            (1)

            或SUM(G(i,j))= p[i]*(j-i+l) + p[i+2]*(j-(i+2)+1) +......+p[j-1]*2    (2)

            如果区间长度为奇数就是(1)式,为偶数则为(2)式

            看出这个规律以后要怎么搞呢,其实我觉得看出这个公式只是最基本的,后面的才叫恶心= =

            每次要查询[l,r]区间的SUM值,l和r可以很大,显然这也是有规律的

            不难想到一般求区间和可以转化为求SUM(G(1,r))-SUM(G(1,l-1))

            这样就转化为了求前缀和,这样显然对于这种恶心题来讲会好处理很多

            为了方便处理,将整个P序列直接复制一份到后面,现在可以用线段树维护整个2*P序列

            可以看出求SUM的式子求和不是累加连续的数,而是隔一个数加一次

            这样就需要将数的位置分奇偶分别求和,这两部分是不相干的

            接下来就是求前缀和自己慢慢搞吧,已经没啥了,就推一下简单的求和公式而已

            还是感觉这题有点恶心= =

你可能感兴趣的:(ACM,ZOJ)