Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9173779
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:38:37+00:00 2026-06-17T16:38:37+00:00

The link to the question is UVA – 1394 : And There Was One

  • 0

The link to the question is UVA – 1394 : And There Was One.
The naive algorithm is to scan the entire array and marking the kth element on each iteration stopping at the last : this takes O(n^2) time.
I have searched for an alternative algorithm and came across a chinese blog which pointed me to segment trees using lazy propogation as a solution for O(N lgN) time.
Having studied segment trees I tried forming an algorithm for O(N lgN) time but to no avail.


My questions are :
1. Can a segment tree be used to get the desired runnig time?
2. If yes, enlighten me as to how to use them.
3. Are segment trees and “zkw” segment trees one and the same? – he mentions zkw segment trees in the blog.
UPDATE: The above problem is an example of the Josephus problem..

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T16:38:38+00:00Added an answer on June 17, 2026 at 4:38 pm
    1. Yes it can.

    2. You can see the description of the data structure below, here I’ll just hint how to use it in the given problem.
      The population we’re representing is obviously the circle of stones. We start with all of the N stones being alive, and at each step kill the appropriate stone in our tree.
      Only need to know which element we’re currently at (I think it’s appropriate to call it m). The high-level algorithm (I leave the details to you) is as follows (P is our data structure):

      While P.size > 1:
        P.toggle(m) // remove the m-th stone
        m = P.kth(next stone to be killed)
      

      P.size in the code above is simply the number of all remaining stones.
      In the description below, it corresponds to tree[1].

      Note: The symbol k used in the data structure is different from the one in the problem input that represents jump distance.

    3. Pretty much. I haven’t seen that name before, but the code looks the same as what I’ve seen people call a population tree.

      The population tree is a simple way to use the segment tree. You have N elements each having two possible states: 1 for alive and 0 for dead. The tree supports two operations:

      • Toggle the state of element numbered i.
      • Return the index of the k-th (in size of its index) living element.

      To clarify the second operation let’s say that the set of living elements is {1, 2, 4, 7}.
      If N = 8, that corresponds to the state array 01101001 (element 0 is dead, element 1 is alive, element 3 is alive, and so on).

      So, how to implement this?
      As usual, leafs of the tree correspond to the array. That is, the i-th leaf has value 1 if the i-th element is alive, and 0 otherwise.
      Each internal node remembers the sum of the values of its children.

      To toggle the state of an element, we toggle the value of the corresponding leaf, and then fix on the path from that leaf to the root:

      const int size = 1 << 18; // 2^17 elements, the rest are internal nodes
      int tree[size]; // number of living elements in the subtree of a node
      
      void toggle(int i) {
        tree[i + size / 2] ^= 1; // toggle the leaf
        for (i /= 2; i > 0; i /= 2)
          tree[i] = tree[2 * i] + tree[2 * i + 1];
      }
      

      Note: a common way of labeling nodes is to have the root equal to 1, and recursively,
      the children of a node n are 2n and 2n+1.

      To find the k-th living element we can think recursively:
      We’re currently at node n, and are looking for the k-th living element its subtree (the subtree of a node is the tree rooted at that node).
      If n‘s left child 2n has k or more living elements, set n = 2n.
      Otherwise, we’ll obviously go to the right child, that is set n = 2n+1.
      In that case, we’re no longer looking for the k-th living element of the subtree.
      Because we skipped all the living elements of the left child, we subtract that count from k. Here we’re looking at the living elements 1-based, for simplicity.

      The basic thinking here might be recursive, but the description hints that doing it iteratively should also be quite simple:

      int kth(int k) {
        ++k; // because this method looks at elements 1-based
        int current_node = 1; // start at the root
        while (current_node < size / 2) {
          if (tree[2 * current_node] >= k) 
            current_node = 2 * current_node; // descend into the left child
          else {
            k -= tree[2 * current_node]; // fix k
            current_node = 2 * current_node + 1; // descend into the right child
          }
        }
      }
      

      These two functions are what makes that segment tree a population tree.

    This was a data structure question, so the described idea uses a data structure.
    However, I’d like to mention that the problem is known as the Josephus problem, and has alternative solutions, so you might be interested in looking it up.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There is already a similar question( link ).The thing is I don't understand what
Just a follow up question to this one here => link Is it possible
I already read the link checker question posted in SO. If there are more
Just a basic html link question. I have an intranet setup, and I need
Similar to this question: link However I have already mastered that. My problem is
This is a link to a question I had asked two days back, How
Link to the site in question . You can look at the source for
My question is related to this link stackoverflow ques In essence repeating the figure
From the link ( which is mentioned in Question ) my question is that
The problem below is ralated to my previous question Converting static link library to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.