ikbal's blog

By ikbal, 9 years ago,

You have a sequence of numbers named a. You need to perform 2 queries :

1. Add x to all elements lie in range [L, R]

2. Ask for gcd(aL,aL + 1...aR)

• +49

 » 9 years ago, # |   +109 gcd(aL, aL + 1, ..., aR) = gcd(aL, aL + 1 - aL, ..., aR - aR - 1).Then use segment tree and in every vertex keep aL, aR and gcd(aL + 1 - aL, ..., aR - aR - 1). It seems to be enough to perform both queries.
•  » » 9 years ago, # ^ |   +11 Thank you for your approach.
•  » » » 9 years ago, # ^ |   +3 Can you provide a problem link? I have noticed this problem a long time ago. But I can't find a problem to to check whether my implement is OK or not. Thanks in advance.
•  » » » » 9 years ago, # ^ |   0 Thats interesting you did not saw this problem before since you are from china: NOI 2012 Magic Chessboard. This problem requires above observation.
•  » » 9 years ago, # ^ |   +3 I need to keep aR ?
•  » » » 9 years ago, # ^ |   0 When you want to find values for segment [L, R] from values for [L, M] and [M+1, R], you need to know value aM + 1 - aM, so you need to store value aM for segment [L, M].
•  » » » » 9 years ago, # ^ |   +2 You can store ai with Fenwick tree and have separate segment tree for ai + 1 - ai, eliminating need in range updates (I guess this is what author of previous comment means).
•  » » » » 9 years ago, # ^ | ← Rev. 3 →   +3 Yes, but: gcd(aL, ..., aR) = gcd(gcd(aL, ..., aM), gcd(aM + 1, ..., aR)) and gcd(aL, ..., aM) = gcd(aL, gcd(aL + 1 - aL, ..., aM - aM - 1)) and gcd(aM + 1, ..., aR) = gcd(aM + 1, gcd(aM + 2 - aM + 1, ..., aR - aR - 1)) , i can't use these facts?
•  » » » » » 9 years ago, # ^ | ← Rev. 2 →   +3 How do you calculate aM + 1 - aM without knowing aM?
•  » » » » » » 9 years ago, # ^ | ← Rev. 11 →   0 As pointed by MrDindows, there isn't aM + 1 - aM on my expression.
•  » » » » » » 9 years ago, # ^ | ← Rev. 2 →   -10 But there isn't this value in his expressions. Why does he have to calculate it? =)
•  » » » » » » » 9 years ago, # ^ |   0 He needs to calulate gcd(aL + 1 - aL,...aR - aR - 1) too, which contains aM + 1 - aM.
•  » » » » » » » » 9 years ago, # ^ |   +6 We can replace am + 1 - am with am + 1 - al because: gcd(..., am + 1 - am, ...) = gcd(..., (am + 1 - am) + (am - am - 1) + (am - 1 - am - 2) + ... + (al + 1 - al)) = gcd(..., am + 1 - al, ...)
 » 9 years ago, # |   +5 You can also keep two values aL and gcd(aL + 1 - aL, aL + 2 - aL, ..., aR - aL) in segment tree.(It uses the fact that gcd(aM + 1 - aL, X - aM + 1) = gcd(aM + 1 - aL, X - aL))
 » 8 years ago, # | ← Rev. 2 →   -29 I am solving this (https://www.codechef.com/problems/DGCD) question using HLD and I am getting TLE Here's my code (https://ideone.com/aUlGTZ)
•  » » 8 years ago, # ^ | ← Rev. 2 →   +8 I am solving this (http://www.spoj.com/problems/INVCNT/) question using D&C and I am getting AC Here's my code (http://ideone.com/yu7kcm)