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

来源:互联网 发布:端口类型 路由器 编辑:程序博客网 时间:2024/03/29 22:15
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(lr) = Sl - Sl+1 + Sl+2 - ... + (-1)r-lSr

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:

  • x d: set Px to dd is a single digit.
  • l r: find the sum of G(ij) 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:

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

Output

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

Sample Input

232424242 1 12 1 41 3 72 3 432424262 1 11 3 72 2 41 3 42 7 102 1 30

Sample Output

320143820870

题意: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的式子求和不是累加连续的数,而是隔一个数加一次

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

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

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

0 0