Jaggu Playing with Balloons


Problem Statement :


Jaggu is a little kid and he likes playing with water balloons. He took 1 million ( 106 ) empty buckets and he filled the bucket with water balloons under the instruction of his sister Ishika.
His sister gives him two types of commands:

R pos1 pos2 which implies that jaggu needs to tell her what is the total number of water balloons in the bucket from pos1 to pos2 (both included).

U pos M plus which implies that he has to work like the function

Update(pos,M,plus)

void Update(int pos,int M,int plus)
{
    int N=1000000;  //1 million
    for (int i=1;i<=50;i++)
    {
        int back = pos
        for(int j=1;j<=1000;j++)
        {
            add M water ballons at bucket pos
            int s,in=__builtin_popcount(pos);
            for(int k=0;;k++)
            {
                s=pos+pow(2,k)
                if( __builtin_popcount(s) <= in )
                {
                    in = __builtin_popcount(s)
                    pos = s;
                    if(pos>N)       break;
                    add M water ballons at bucket pos
                }
            }
            pos = pos - N
        }
        pos = back+plus;
        if(pos>N) pos-=N;
    }
}
Jaggu is too lazy to put the water ballons in the bucket. Afraid that he might be caught for not doing what his sister told him to do so, he asks your help to provide correct answers for each of his sister's query. .

Input Format

First line contains Q, number of queries to follow.

Next Q line follows , which can be either an Update Query or Report Query.Each Update Query is followed by atleast 1 report query.

Output Format

For each report query , output the answer in a separate line.


Constraints

1 ≤ Q ≤ 2 * 105

1 ≤ pos1,pos2,pos ≤ 106

pos1 ≤ pos2

1 ≤ M ≤ 10

1 ≤ plus ≤ 999999



Solution :



title-img


                            Solution in C :

In    C++  :










#include<iostream>
#include<string>
using namespace std;

long long tree[1000001];
int bitcount[2000001];

void init()
{
for (int i = 0; i <= 2000000; i++)
for (int j = 0; j < 32; j++)
if (i&(1 << j))
bitcount[i]++;
}

void update(int idx, int val)
{
while (idx <= 1000000)
{
tree[idx] += val;
idx += (idx & -idx);
}
}

long long read(int idx)
{
long long sum = 0;
while (idx > 0)
{
sum += tree[idx];
idx -= (idx & -idx);
}
return sum;
}

void update(int pos, int M, int plus)
{
int N = 1000000;
for (int i = 1; i <= 50; i++)
{
int back = pos, j;
for (j = 1; j <= 1000; j++)
{
int s, in = bitcount[pos];
if (pos == 48576) break;
update(pos, M);
for (int k = 0;; k++)
{
s = pos + (k == 0 ? 1 : (1 << (k - 1)));
if (bitcount[s] <= in)
{
in = bitcount[s];
pos = s;
if (pos > N) break;
update(pos, M);
}
}
pos -= N;
}

update(48576, (1000 - j + 1) * M);
update(48640, (1000 - j + 1) * M);
update(49152, (1000 - j + 1) * M);
update(65536, (1000 - j + 1) * M);
update(131072, (1000 - j + 1) * M);
update(262144, (1000 - j + 1) * M);
update(524288, (1000 - j + 1) * M);

pos = back + plus;
if (pos > N) pos -= N;
}
}

int main()
{
int q;
cin >> q;
init();
while (q--)
{
char ch;
cin >> ch;
if (ch == 'U')
{
int pos, M, plus;
cin >> pos >> M >> plus;
update(pos, M, plus);
}
else
{
int pos1, pos2;
cin >> pos1 >> pos2;
long long sum1 = read(pos1 - 1);
long long sum2 = read(pos2);
cout << sum2 - sum1 << endl;
}
}
return 0;
}










In   Java  :








import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;

public class Solution {

    public static void solve(Input in, PrintWriter out) 
throws IOException {
        int n = 1000000;
        int[] next = new int[n];
        int[] next2 = new int[n];
        for (int i = n - 1; i >= 0; --i) {
            next[i] = i | (i + 1);
            if (next[i] >= n) {
                next2[i] = next[i] - n;
            } else {
                next2[i] = next2[next[i]];
            }
        }
        long[] f = new long[n];
        int qs = in.nextInt();
        for (int q = 0; q < qs; ++q) {
            if (in.next().equals("U")) {
                int pos = in.nextInt() - 1;
                long m = in.nextLong();
                int add = in.nextInt();
                for (int it = 0; it < 50; ++it) {
                    int x = (pos + it * add) % n;
                    int incs = 1000;
                    while (incs > 0) {
                        if (next2[x] == x) {
                            inc(x, m * incs, f);
                            incs = 0;
                        } else {
                            inc(x, m, f);
                            --incs;
                            x = next2[x];
                        }
                    }
                }
            } else {
                int pos1 = in.nextInt() - 1;
                int pos2 = in.nextInt() - 1;
                out.println(get(pos2, f) - get(pos1 - 1, f));
            }
        }
    }

    private static long get(int x, long[] f) {
        long r = 0;
        while (x >= 0) {
            r += f[x];
            x = (x & (x + 1)) - 1;
        }
        return r;
    }

    private static void inc(int x, long m, long[] f) {
        long add = m;
        while (x < f.length) {
            f[x] += add;
            add += m;
            x = x | (x + 1);
        }
    }

    public static void main(String[] args) throws IOException {
        PrintWriter out = new PrintWriter(System.out);
        solve(new Input(new BufferedReader(
new InputStreamReader(System.in))), out);
        out.close();
    }

    static class Input {
        BufferedReader in;
        StringBuilder sb = new StringBuilder();

        public Input(BufferedReader in) {
            this.in = in;
        }

        public Input(String s) {
            this.in = new BufferedReader(new StringReader(s));
        }

        public String next() throws IOException {
            sb.setLength(0);
            while (true) {
                int c = in.read();
                if (c == -1) {
                    return null;
                }
                if (" \n\r\t".indexOf(c) == -1) {
                    sb.append((char)c);
                    break;
                }
            }
            while (true) {
                int c = in.read();
                if (c == -1 || " \n\r\t".indexOf(c) != -1) {
                    break;
                }
                sb.append((char)c);
            }
            return sb.toString();
        }

        public int nextInt() throws IOException {
            return Integer.parseInt(next());
        }

        public long nextLong() throws IOException {
            return Long.parseLong(next());
        }

        public double nextDouble() throws IOException {
            return Double.parseDouble(next());
        }
    }
}











In    C  :








#include<stdio.h>

int i,j,k,n,m,pos,plus,pos1,pos2,q,t,back;
long long a[1111111];
char c;

int lowbit(int i) {
return i & -i;
}

void update(int index, int delta) {
for (int i = index; i <= 1000000; i += lowbit(i))
a[i] += delta;
}

long long sum(int index) {
long res = 0;
for (; index > 0; index -= lowbit(index))
res += a[index];
return res;
}

main()
{
scanf("%d",&q);
for (t=1; t<=q; t++)
{
scanf("%c",&c);
while (c!='R' && c!='U') scanf("%c",&c);
if (c=='R') scanf("%d%d",&pos1,&pos2); else scanf("%d%d%d",&pos,&m,&plus);
if (c=='R') printf("%lld\n",sum(pos2)-sum(pos1-1)); else
for (i=1; i<=50; i++)
{
back=pos;
while (pos<=1000000)
{
    update(pos,m);
    pos+=lowbit(pos);
}
pos-=1000000;
while (pos<=1000000)
{
    update(pos,m);
    pos+=lowbit(pos);
}
pos-=1000000;
while (pos<=1000000)
{
    update(pos,998*m);
    pos+=lowbit(pos);
}
pos=back+plus;
if (pos>1000000) pos-=1000000;
}
}
}
                        








View More Similar Problems

Reverse a doubly linked list

This challenge is part of a tutorial track by MyCodeSchool Given the pointer to the head node of a doubly linked list, reverse the order of the nodes in place. That is, change the next and prev pointers of the nodes so that the direction of the list is reversed. Return a reference to the head node of the reversed list. Note: The head node might be NULL to indicate that the list is empty.

View Solution →

Tree: Preorder Traversal

Complete the preorder function in the editor below, which has 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's preorder traversal as a single line of space-separated values. Input Format Our test code passes the root node of a binary tree to the preOrder function. Constraints 1 <= Nodes in the tree <= 500 Output Format Print the tree's

View Solution →

Tree: Postorder Traversal

Complete the postorder function in the editor below. It received 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's postorder traversal as a single line of space-separated values. Input Format Our test code passes the root node of a binary tree to the postorder function. Constraints 1 <= Nodes in the tree <= 500 Output Format Print the

View Solution →

Tree: Inorder Traversal

In this challenge, you are required to implement inorder traversal of a tree. Complete the inorder function in your editor below, which has 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's inorder traversal as a single line of space-separated values. Input Format Our hidden tester code passes the root node of a binary tree to your $inOrder* func

View Solution →

Tree: Height of a Binary Tree

The height of a binary tree is the number of edges between the tree's root and its furthest leaf. For example, the following binary tree is of height : image Function Description Complete the getHeight or height function in the editor. It must return the height of a binary tree as an integer. getHeight or height has the following parameter(s): root: a reference to the root of a binary

View Solution →

Tree : Top View

Given a pointer to the root of a binary tree, print the top view of the binary tree. The tree as seen from the top the nodes, is called the top view of the tree. For example : 1 \ 2 \ 5 / \ 3 6 \ 4 Top View : 1 -> 2 -> 5 -> 6 Complete the function topView and print the resulting values on a single line separated by space.

View Solution →