algorithm - Fenwick Tree Java -


I tried to apply the fenwick tree in java, but I can not find the desired result here is my code: / P>

  Import java.io. *; Import java.util. *; Import java.math. *; Class Fenwick 1 {Public Stable Int N; Public stable long [] A; Public Stable Zero Main (String [] AGR throws IOException {BufferedReader br = New BufferedReader (New InputStreamReader (System.in)); N = integer PRIIENT (Breadline ()); A = new tall [N]; String [] str = br.readLine (). the division (""); (I.e. ++) for i   

When I input:

10 1 2 3 4 5 6 7 8 9 10 < P> I get:

13
19

Then increase the increment work But the query (4) is cumulative amount up to index 4 i.e.

(1 + 2 + 13 + 4 + 5) = 25

You do not start it properly instead:

 For  (int i = 0; i & lt; N; i ++) {a [i] = Long. Parcelong (str [i]);}   

should be:

 For  (int i = 0; I & lt; N; I ++) {increment (i, (int) long parseLong (str [i])); }   

Because a [i] should accumulate a cumulative sum, not a single element.

If you also have an initial array element, you can create another array:

  long [] initA = new tall [N]; For (int i = 0; i & lt; n; i ++) {initA [i] = long.more than (str [ii]); Increment (i, (int) initA [i]); }    

Comments

Popular posts from this blog

java - ImportError: No module named py4j.java_gateway -

python - Receiving "KeyError" after decoding json result from url -

.net - Creating a new Queue Manager and Queue in Websphere MQ (using C#) -