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(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

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

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

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

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

时间: 2024-10-10 13:03:44

2014牡丹江网络预选赛E题(线段树)zoj3813的相关文章

2014牡丹江网络预选赛D题(状压DP)zoj3812

We Need Medicine Time Limit: 10 Seconds      Memory Limit: 65536 KB      Special Judge A terrible disease broke out! The disease was caused by a new type of virus, which will lead to lethal lymphoedema symptom. For convenience, it was named LL virus.

2014牡丹江网络预选赛F题(隐式图BFS暴搜)zoj3814

Sawtooth Puzzle Time Limit: 10 Seconds      Memory Limit: 65536 KB Recently, you found an interesting game called Sawtooth Puzzle. This is a single-player game played on a grid with 3 x 3 cells. Each cell contains a part of an image. Besides, each ed

2014牡丹江网络预选赛B题(找规律)zoj3810

A Volcanic Island Time Limit: 2 Seconds      Memory Limit: 65536 KB      Special Judge An underwater volcano has erupted massively in somewhere of the deep Atlantis Ocean. This large eruption led to the birth of a new volcanic island, which had a sha

2014牡丹江网络预选赛I题(字符串hash+简单DP)zoj3817

Chinese Knot Time Limit: 2 Seconds      Memory Limit: 65536 KB      Special Judge Chinese knot is a decorative handicraft that began as a form of Chinese folk artifact in the Tang and Song Dynasty in China. It was later popularized in the Ming. Alice

hdu 4031 2011成都赛区网络赛A题 线段树 ***

就是不知道时间该怎么处理,想了好久,看了别人的题解发现原来是暴力,暴力也很巧妙啊,想不出来的那种  -_-! 1 #include<cstdio> 2 #include<iostream> 3 #include<algorithm> 4 #include<cstring> 5 #include<cmath> 6 #include<queue> 7 #include<map> 8 using namespace std; 9

ZOJ-3811 Untrusted Patrol DFS 2014牡丹江网络赛C题

n个点,m条双向边,k个传感器.其中有l个传感器记录到了第一次到达的时间顺序,求是否有可能检查了所有的顶点. 首先判断l,l<k一定是不行的.然后按照传感器的时间顺序dfs,先从第一个传感器位置搜索dfs搜到所有的到达传感器的位置结束,如果这个传感器被标记为可访问,则从这个传感器继续搜索下一层传感器,否则是不能满足时间顺序的.最后还要判断整个图的连通性,所有的顶点都要可以到达的. #include <iostream> #include <cstdio> #include &

HDU6447 YJJ&#39;s Salesman 2018中国大学生程序设计竞赛 - 网络选拔赛1010 离散化+线段树+DP

YJJ's Salesman Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 253    Accepted Submission(s): 62 Problem Description YJJ is a salesman who has traveled through western country. YJJ is always on

经典算法题每日演练——第十二题 线段树

原文:经典算法题每日演练--第十二题 线段树 这一篇我们来看树状数组的加强版线段树,树状数组能玩的线段树一样可以玩,而且能玩的更好,他们在区间求和,最大,平均 等经典的RMQ问题上有着对数时间的优越表现. 一:线段树 线段树又称"区间树”,在每个节点上保存一个区间,当然区间的划分采用折半的思想,叶子节点只保存一个值,也叫单元节点,所 以最终的构造就是一个平衡的二叉树,拥有CURD的O(lgN)的时间. 从图中我们可以清楚的看到[0-10]被划分成线段的在树中的分布情况,针对区间[0-N],最多有

2014 summer 知识点总结1之线段树

HDU 1166 [题意]: n个阵营一字排开,每个初始有a[i]个人.现有两种操作: Q a b 查询[a,b]之间总人数并输出 A/S a b 在a号位添加/删除b个人 [分析]:最基本的单点更新和区间查询,维护节点信息sum[o] [代码]: 1 #include <iostream> 2 #include <string.h> 3 #include <stdio.h> 4 5 using namespace std; 6 7 int numv[50005<