线段树训练
线段树训练
GSS3 - Can you answer these queries III
题面翻译
\(n\) 个数,\(q\) 次操作
操作0 x y
把\(A_x\)
修改为\(y\)
操作1 l r
询问区间\([l,
r]\) 的最大子段和
感谢 @Edgration 提供的翻译
题目描述
You are given a sequence A of N (N <= 50000) integers between
-10000 and 10000. On this sequence you have to apply M (M <= 50000)
operations:
modify the i-th element in the sequence or for given x y print max{Ai +
Ai+1 + .. + Aj | x<=i<=j<=y }.
输入格式
The first line of input contains an integer N. The following line
contains N integers, representing the sequence A1..AN.
The third line contains an integer M. The next M lines contain the
operations in following form:
0 x y: modify Ax into y (|y|<=10000).
1 x y: print max{Ai + Ai+1 + .. + Aj | x<=i<=j<=y }.
输出格式
For each query, print an integer as the problem required.
样例 #1
样例输入 #1
1 | 4 |
样例输出 #1
1 | 6 |
1 |
|
interval GCD
【题目】
给定一个长度为N的数列A,以及M条指令,每条指令可能是以下两种之一: 1、“C l r d”,表示把 A[l],A[l+1],…,A[r] 都加上 d。 2、“Q l r”,表示询问 A[l],A[l+1],…,A[r] 的最大公约数(GCD)。 对于每个询问,输出一个整数表示答案。
区间gcd再加区间修改。
一般求gcd的时候辗转相除法。
gcd(x,y)=gcd(x,y-x)
那么可以把这个公式推到3个项。
gcd(x,y,z)=gcd(x,y-x,z-y)
可以看出来这是一个差分数列。
原数列为a[],差分数列为b[]。
这样就可以用线段树在b[]上单点修改,l加上d,r+1减去d。
1 |
|