Mishki Playing Games

Mishki loves playing games, so she asked her friend Hacker to join her in the Game of Stones. In this game, they have N sets of stones, numbered from 1 to N. Each set consists of Ai stones, where 

1iN.
In each turn, the player can select any of the set containing at least 1 stone and have to reduce it to half of the present number of stones, i.e Ai/ 2 (Integer Division) from it, and in the case of a single stone, he/she has to empty the set by removing it.
As the game is really interesting, both will play this game on Q days. On each day, they will some select sets of stones numbered from l to r, where 1lrN and being a lover of the games, every day Mishki will be the first player to take the turn.
The one who won't be able to play his/her turn in the game (i.e no stones left in the selected set of stones), will lose the game.
You need to tell the winner of the game each day if both the player will play optimally and take their turn alternatively.

Example:

Input:  n = 3, q = 2, a = [4,2,3], queries = {{1,2},{2,3}}

Output:

Mishki Hacker

Approach:

C++

#include <bits/stdc++.h>
using namespace std;

int countBits(int n)
{
    int res = 0;
    while (n)
    {
        res++;
        n = n / 2;
    }
    return res;
}
int arr[1000001];
int tree[2000010];
void build(int nodeint startint end)
{
    if (start == end)
    {
        tree[node] = countBits(arr[start]);
    }
    else
    {
        int mid = (start + end) / 2;
        build(2 * nodestartmid);
        build(2 * node + 1mid + 1end);
        tree[node] = tree[2 * node] + tree[2 * node + 1];
    }
}
int query(int nodeint startint endint lint r)
{
    if (start > r || l > end)
        return 0;
    if (start >= l && end <= r)
        return tree[node];
    else
    {
        int mid = (start + end) / 2;
        int p1 = query(2 * nodestartmidlr);
        int p2 = query(2 * node + 1mid + 1endlr);
        return p1 + p2;
    }
}

void miskiPlayingGames(int nint q
vector<vector<int>> &queries)
{

    for (int i = 0i < qi++)
    {
        int l = queries[i][0]r = queries[i][1];

        int ans = query(11nlr);
        if (ans & 1)
            cout << "Mishki\n";
        else
            cout << "Hacker\n";
    }
}
int main()
{

    int n = 3q = 2;
    int a[] = {423};
    for (int i = 1i <= ni++)
        arr[i] = a[i - 1];
    build(11n);
    vector<vector<int>> queries = {{12}, {23}};

    miskiPlayingGames(nqqueries);

    return 0;
}


No comments:

Post a Comment