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 | 3985 |
2 | jiangly | 3814 |
3 | jqdai0815 | 3682 |
4 | Benq | 3529 |
5 | orzdevinwang | 3526 |
6 | ksun48 | 3517 |
7 | Radewoosh | 3410 |
8 | hos.lyric | 3399 |
9 | ecnerwala | 3392 |
9 | Um_nik | 3392 |
# | User | Contrib. |
---|---|---|
1 | cry | 169 |
2 | maomao90 | 162 |
2 | Um_nik | 162 |
4 | atcoder_official | 160 |
5 | djm03178 | 158 |
6 | -is-this-fft- | 157 |
7 | adamant | 155 |
8 | Dominater069 | 154 |
8 | awoo | 154 |
10 | luogu_official | 151 |
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)