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)
№ | Пользователь | Рейтинг |
---|---|---|
1 | tourist | 3993 |
2 | jiangly | 3743 |
3 | orzdevinwang | 3707 |
4 | Radewoosh | 3627 |
5 | jqdai0815 | 3620 |
6 | Benq | 3564 |
7 | Kevin114514 | 3443 |
8 | ksun48 | 3434 |
9 | Rewinding | 3397 |
10 | Um_nik | 3396 |
Страны | Города | Организации | Всё → |
№ | Пользователь | Вклад |
---|---|---|
1 | cry | 167 |
2 | Um_nik | 163 |
3 | maomao90 | 162 |
3 | atcoder_official | 162 |
5 | adamant | 159 |
6 | -is-this-fft- | 158 |
7 | awoo | 155 |
8 | TheScrasse | 154 |
9 | Dominater069 | 153 |
10 | nor | 152 |
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)
Название |
---|
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)