ikbal's blog

By ikbal, 10 years ago, In English

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)

  • Vote: I like it
  • +49
  • Vote: I do not like it

»
10 years ago, # |
  Vote: I like it +109 Vote: I do not like it

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.

  • »
    »
    10 years ago, # ^ |
      Vote: I like it +11 Vote: I do not like it

    Thank you for your approach.

    • »
      »
      »
      10 years ago, # ^ |
        Vote: I like it +3 Vote: I do not like it

      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.

      • »
        »
        »
        »
        10 years ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        Thats interesting you did not saw this problem before since you are from china: NOI 2012 Magic Chessboard. This problem requires above observation.

  • »
    »
    10 years ago, # ^ |
      Vote: I like it +3 Vote: I do not like it

    I need to keep aR ?

    • »
      »
      »
      10 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      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].

      • »
        »
        »
        »
        10 years ago, # ^ |
          Vote: I like it +2 Vote: I do not like it

        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).

      • »
        »
        »
        »
        10 years ago, # ^ |
        Rev. 3   Vote: I like it +3 Vote: I do not like it

        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?

        • »
          »
          »
          »
          »
          10 years ago, # ^ |
          Rev. 2   Vote: I like it +3 Vote: I do not like it

          How do you calculate aM + 1 - aM without knowing aM?

          • »
            »
            »
            »
            »
            »
            10 years ago, # ^ |
            Rev. 11   Vote: I like it 0 Vote: I do not like it

            As pointed by MrDindows, there isn't aM + 1 - aM on my expression.

          • »
            »
            »
            »
            »
            »
            10 years ago, # ^ |
            Rev. 2   Vote: I like it -10 Vote: I do not like it

            But there isn't this value in his expressions. Why does he have to calculate it? =)

            • »
              »
              »
              »
              »
              »
              »
              10 years ago, # ^ |
                Vote: I like it 0 Vote: I do not like it

              He needs to calulate gcd(aL + 1 - aL,...aR - aR - 1) too, which contains aM + 1 - aM.

              • »
                »
                »
                »
                »
                »
                »
                »
                10 years ago, # ^ |
                  Vote: I like it +6 Vote: I do not like it

                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, ...)

»
10 years ago, # |
  Vote: I like it +5 Vote: I do not like it

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))

»
9 years ago, # |
Rev. 2   Vote: I like it -29 Vote: I do not like it

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)