Cerinta completa

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

Sample Input

2
U 692778 7 291188
R 636916 747794

Sample Output

378 

Explanation

Follow the code above to get the answer.

Note

  1. Input is randomly generated.

  2. __builtin_popcount(x) gives the number of set bits in binary representation of x.

  3. pow(2,k) denotes 2 raised to k , i.e. exponentiation of 2.

Timelimit is 3 times the timelimit mentioned here


Limbajul de programare folosit: cpp14

Cod:

#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<assert.h>
using namespace std;
#define MaxN 1000000
#define rep1 50
#define rep2 1000
#define MaxVal MaxN
typedef long long int ll;
int special[]={1,983041,999425,999937,1000001};
int bouncedat[]={48576,15808,448,64};
ll tree[MaxN+1];
ll read(int pos)
{
    ll sum=0;
    while(pos){
        sum = sum + tree[pos];
        pos-=(pos&(-pos));
    }
    return sum;
}
void update(int pos,ll v)
{
    ll b=v;
    while(pos<=MaxVal){
        tree[pos]+=v;
        v+=b;
        pos+=(pos&(-pos));
    }
}
int main()
{
    int N=MaxN,Q,x,y,plus,j,counter[4],where;
    char ch;
    scanf("%d",&Q);
    for(int i=0;i<Q;i++){
        scanf(" %c%d%d",&ch,&x,&y);
        if(ch=='U'){
            where=0;
            scanf("%d",&plus);
            memset(counter,0,sizeof(counter));
            for(j=1;j<=rep1;j++){
                update(x,y);
                if(x>=special[where] && x<special[where+1]) counter[where]++;
                else{
                    while(1){
                        where++;
                        if(where==4)    where=0;
                        if(x>=special[where] && x<special[where+1]){
                            counter[where]++;
                            break;
                        }
                    }
                }
                x=x+plus;
                if(x>N) x=x-N;
            }
            for(j=0;j<4;j++){
                if(counter[j]!=0)
                update(bouncedat[j],y*(counter[j]));
            }
            update(bouncedat[0],(ll)y*(ll)(rep2-2)*(ll)rep1);
        }
        else    printf("%lld\n",read(y)-read(x-1));
    }
    return 0;
}

Scor obtinut: 1.0

Submission ID: 464653910

Link challenge: https://www.hackerrank.com/challenges/jagia-playing-with-numbers/problem

Jaggu Playing with Balloons