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

Kundu and Tree

Kundu is true tree lover. Tree is a connected graph having N vertices and N-1 edges. Today when he got a tree, he colored each edge with one of either red(r) or black(b) color. He is interested in knowing how many triplets(a,b,c) of vertices are there , such that, there is atleast one edge having red color on all the three paths i.e. from vertex a to b, vertex b to c and vertex c to a . Note that

View Solution →

Super Maximum Cost Queries

Victoria has a tree, T , consisting of N nodes numbered from 1 to N. Each edge from node Ui to Vi in tree T has an integer weight, Wi. Let's define the cost, C, of a path from some node X to some other node Y as the maximum weight ( W ) for any edge in the unique path from node X to Y node . Victoria wants your help processing Q queries on tree T, where each query contains 2 integers, L and

View Solution →

Contacts

We're going to make our own Contacts application! The application must perform two types of operations: 1 . add name, where name is a string denoting a contact name. This must store name as a new contact in the application. find partial, where partial is a string denoting a partial name to search the application for. It must count the number of contacts starting partial with and print the co

View Solution →

No Prefix Set

There is a given list of strings where each string contains only lowercase letters from a - j, inclusive. The set of strings is said to be a GOOD SET if no string is a prefix of another string. In this case, print GOOD SET. Otherwise, print BAD SET on the first line followed by the string being checked. Note If two strings are identical, they are prefixes of each other. Function Descriptio

View Solution →

Cube Summation

You are given a 3-D Matrix in which each block contains 0 initially. The first block is defined by the coordinate (1,1,1) and the last block is defined by the coordinate (N,N,N). There are two types of queries. UPDATE x y z W updates the value of block (x,y,z) to W. QUERY x1 y1 z1 x2 y2 z2 calculates the sum of the value of blocks whose x coordinate is between x1 and x2 (inclusive), y coor

View Solution →

Direct Connections

Enter-View ( EV ) is a linear, street-like country. By linear, we mean all the cities of the country are placed on a single straight line - the x -axis. Thus every city's position can be defined by a single coordinate, xi, the distance from the left borderline of the country. You can treat all cities as single points. Unfortunately, the dictator of telecommunication of EV (Mr. S. Treat Jr.) do

View Solution →