top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How to solve this problem using c++ with operations in log complexities for both update and count?

+2 votes
386 views

suppose we have an array of N natural numbers and asks him to solve the following queries:-
Query a:- modify the element present at index i to x.
Query b:- count the number of even numbers in range l to r inclusive.
Query c:- count the number of odd numbers in range l to r inclusive.

input:
First line of the input contains the number N. Next line contains N natural numbers.Next line contains an integer Q followed by Q queries.
a x y - modify the number at index x to y.
b x y - count the number of even numbers in range l to r inclusive.
c x y - count the number of odd numbers in range l to r inclusive.
I tried to solve using simple arrays but it isn't doing well for big constraints so I thought to use other DS with efficient algorithm so please explain appropriate algorithm.Thanks in advance.

posted Oct 6, 2016 by Shivam Kumar Pandey

Looking for an answer?  Promote on:
Facebook Share Button Twitter Share Button LinkedIn Share Button

Similar Questions
+5 votes

Josephus Problem talks about a problem where there are people standing in a circle waiting to be executed. The counting out begins at some point in the circle and proceeds around the circle in a fixed direction. In each step, a certain number of people are skipped and the next person is executed. The elimination proceeds around the circle (which is becoming smaller and smaller as the executed people are removed), until only the last person remains, who is given freedom.

+3 votes

A person has two piles of stones with him, one has n1 stones and the other has n2 stones. Fired up by boredom, he invented a game with the two piles.

  1. Before the start of the game person chooses an integer m.
  2. In the j-th move: He chooses a number xj such that 1 ≤ xj ≤ m, and removes xj stones from both the piles (this is only possible when both the piles have ≥ xj stones).
  3. The number chosen must be unique over all the moves in the game. That is, for all k < j, xj ≠ xk.
  4. The game stops when person is unable to make any more moves.
    Note: Person wants to make the moves in such a way that the sum of the number of stones remaining in the two piles is minimized.

Please help person find this.

Input
The first line of input contains an integer T denoting the number of test cases.
Each test case consists of 1 line with three integers — n1, n2 and m — separated by single spaces.

Output
For each test case, output a single line containing the minimum sum of the number of stones of two piles.

+3 votes

How to find shortest path in a multistage graph using dynamic programming, C/C++ code would be helpful?

+2 votes

convert a number m to n with minimum operations. The operations allowed were -1 and *2.

For Eg : 4 and 6. Answer is 2.
1st operation : -1 -> 4-1 = 3.
2nd operation : * -> 3 * 2 =6.

+3 votes

Given a 2d array, u need to sort the 2 diagonals independently.
And the elements in diagonal should remain in diagonal only.

INPUT =>
24 2 3 4 7

6 13 8 5 10

11 12 9 14 15

16 21 18 25 20

17 22 23 24 1

OUTPUT =>
1 2 3 4 5

6 7 8 9 10

11 12 13 14 15

16 17 18 19 20

21 22 23 24 25

...