You have a sequence of numbers named a. You need to perform 2 queries :
Add x to all elements lie in range [L, R]
Ask for gcd(aL,aL + 1...aR)
# | User | Rating |
---|---|---|
1 | tourist | 4009 |
2 | jiangly | 3823 |
3 | Benq | 3738 |
4 | Radewoosh | 3633 |
5 | jqdai0815 | 3620 |
6 | orzdevinwang | 3529 |
7 | ecnerwala | 3446 |
8 | Um_nik | 3396 |
9 | ksun48 | 3390 |
10 | gamegame | 3386 |
# | User | Contrib. |
---|---|---|
1 | cry | 165 |
2 | maomao90 | 164 |
3 | Um_nik | 163 |
4 | atcoder_official | 160 |
4 | adamant | 160 |
6 | -is-this-fft- | 158 |
7 | awoo | 157 |
8 | TheScrasse | 154 |
8 | Dominater069 | 154 |
8 | nor | 154 |
You have a sequence of numbers named a. You need to perform 2 queries :
Add x to all elements lie in range [L, R]
Ask for gcd(aL,aL + 1...aR)
Name |
---|
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.
Thank you for your approach.
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.
Thats interesting you did not saw this problem before since you are from china: NOI 2012 Magic Chessboard. This problem requires above observation.
I need to keep aR ?
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].
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).
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?
How do you calculate aM + 1 - aM without knowing aM?
As pointed by MrDindows, there isn't aM + 1 - aM on my expression.
But there isn't this value in his expressions. Why does he have to calculate it? =)
He needs to calulate gcd(aL + 1 - aL,...aR - aR - 1) too, which contains aM + 1 - aM.
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, ...)
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))
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)
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)