#ifndef SPARSE_HPP
#define SPARSE_HPP

class NullElement {
    int index;
public:
    NullElement(int idx) : index(idx) {}
    int Index() const { return index; }
};

template <class T>
class SparseArray {
    struct Item {
        int index;
        T value;
        Item *next;
    };
    Item *first;
    T defval;
public:
    SparseArray(const T &dv) : first(0), defval(dv) {}
    ~SparseArray();
    class Interm {
        friend class SparseArray<T>;
        SparseArray<T> *master;
        int index;
        Interm(SparseArray<T> *a_master, int ind)
            : master(a_master), index(ind) {}
        T& Provide();
        void Remove();
    public:
        operator T() const;
        T operator=(const T& x);
        T operator=(const Interm& x);

        T operator+=(const T& x);
        T operator-=(const T& x);
        T operator*=(const T& x);
        T operator/=(const T& x);
        T operator%=(const T& x);

        T operator<<=(int n);
        T operator>>=(int n);

        T operator|=(const T& x);
        T operator&=(const T& x);
        T operator^=(const T& x);

        T operator++();
        T operator++(int);
        T operator--();
        T operator--(int);

        T operator->() const;
    };
    friend class Interm;

    Interm operator[](int idx) { return Interm(this, idx); }
    const T& operator[](int idx) const;

    int NonzeroCount() const;
private:
    SparseArray(const SparseArray<T>&) {}
    void operator=(const SparseArray<T>&) {}
};

template <class T>
SparseArray<T>::~SparseArray()
{
    while (first) {
        Item *tmp = first;
        first = first->next;
        delete tmp;
    }
}

template <class T>
const T& SparseArray<T>::operator[](int idx) const
{
    Item *tmp;
    for (tmp = first; tmp; tmp = tmp->next)
        if (tmp->index == idx)
            return tmp->value;
    return defval;
}

template <class T>
int SparseArray<T>::NonzeroCount() const
{
    int sum = 0;
    for (Item *tmp = first; tmp; tmp = tmp->next)
        sum++;
    return sum;
}

template <class T>
T& SparseArray<T>::Interm::Provide()
{
    Item *tmp;
    for (tmp = master->first; tmp; tmp = tmp->next)
        if (tmp->index == index)
            return tmp->value;
    tmp = new Item;
    tmp->index = index;
    tmp->value = master->defval;
    tmp->next = master->first;
    master->first = tmp;
    return tmp->value;
}

template<class T>
void SparseArray<T>::Interm::Remove()
{
    Item **tmp;
    for (tmp = &(master->first); *tmp; tmp = &(*tmp)->next)
        if ((*tmp)->index == index) {
            Item *to_delete = *tmp;
            *tmp = (*tmp)->next;
            delete to_delete;
            return;
        }
}

template <class T>
T SparseArray<T>::Interm::operator=(const T &x)
{
    if (x == master->defval)
        Remove();
    else
        Provide() = x;
    return x;
}

template <class T>
T SparseArray<T>::Interm::operator=(const Interm& x)
{
    T value = x;
    return *this = value;
}

template <class T>
T SparseArray<T>::Interm::operator+=(const T& x)
{
    T& location = Provide();
    location += x;
    T res = location;
    if (res == master->defval)
        Remove();
    return res;
}

template <class T>
T SparseArray<T>::Interm::operator-=(const T& x)
{
    T& location = Provide();
    location -= x;
    T res = location;
    if (res == master->defval)
        Remove();
    return res;
}

template <class T>
T SparseArray<T>::Interm::operator*=(const T& x)
{
    T& location = Provide();
    location *= x;
    T res = location;
    if (res == master->defval)
        Remove();
    return res;
}

template <class T>
T SparseArray<T>::Interm::operator/=(const T& x)
{
    T& location = Provide();
    location /= x;
    T res = location;
    if (res == master->defval)
        Remove();
    return res;
}

template <class T>
T SparseArray<T>::Interm::operator%=(const T& x)
{
    T& location = Provide();
    location %= x;
    T res = location;
    if (res == master->defval)
        Remove();
    return res;
}

template <class T>
T SparseArray<T>::Interm::operator<<=(int n)
{
    T& location = Provide();
    location <<= n;
    T res = location;
    if (res == master->defval)
        Remove();
    return res;
}

template <class T>
T SparseArray<T>::Interm::operator>>=(int n)
{
    T& location = Provide();
    location >>= n;
    T res = location;
    if (res == master->defval)
        Remove();
    return res;
}

template <class T>
T SparseArray<T>::Interm::operator|=(const T& x)
{
    T& location = Provide();
    location |= x;
    T res = location;
    if (res == master->defval)
        Remove();
    return res;
}

template <class T>
T SparseArray<T>::Interm::operator&=(const T& x)
{
    T& location = Provide();
    location &= x;
    T res = location;
    if (res == master->defval)
        Remove();
    return res;
}

template <class T>
T SparseArray<T>::Interm::operator^=(const T& x)
{
    T& location = Provide();
    location ^= x;
    T res = location;
    if (res == master->defval)
        Remove();
    return res;
}

template <class T>
T SparseArray<T>::Interm::operator++()
{
    T& location = Provide();
    ++location;
    T res = location;
    if (location == master->defval)
        Remove();
    return res;
}

template <class T>
T SparseArray<T>::Interm::operator++(int)
{
    T& location = Provide();
    T res = location;
    location++;
    if (location == master->defval)
        Remove();
    return res;
}

template <class T>
T SparseArray<T>::Interm::operator--()
{
    T& location = Provide();
    --location;
    T res = location;
    if (location == master->defval)
        Remove();
    return res;
}

template <class T>
T SparseArray<T>::Interm::operator--(int)
{
    T& location = Provide();
    T res = location;
    location--;
    if (location == master->defval)
        Remove();
    return res;
}

template <class T>
T SparseArray<T>::Interm::operator->() const
{
    T p = *this;
    if (!p)
        throw NullElement(index);
    return p;
}

template <class T>
SparseArray<T>::Interm::operator T() const
{
    Item *tmp;
    for (tmp = master->first; tmp; tmp = tmp->next)
        if (tmp->index == index)
            return tmp->value;
    return master->defval;
}

template <>
class SparseArray<bool> {
    int *array;
    int len;
    int capacity;
    int defval;
public:
    SparseArray(int dv = 0, int c = 64) : len(0), capacity(c), defval(dv)
        { array = new int[capacity]; }
    ~SparseArray() { delete[] array; };
    class Interm {
        friend class SparseArray<bool>;
        SparseArray *master;
        int index;
        Interm(SparseArray *a_master, int ind)
            : master(a_master), index(ind) {}
        int& Provide();
        void Remove();
    public:
        operator int() const;
        int operator=(const int& x);
    };
    friend class Interm;

    Interm operator[](int idx) { return Interm(this, idx); }
    const int& operator[](int idx) const;

    int NonzeroCount() const { return len; }
private:
    SparseArray(const SparseArray&) {}
    void operator=(const SparseArray&) {}
};

const int& SparseArray<bool>::operator[](int idx) const
{
    for (int i = 0; i < len; i++)
        if (i == idx)
            return array[i];
    return defval;
}

int& SparseArray<bool>::Interm::Provide()
{
    for (int i = 0; i < master->len; i++)
        if (i == index)
            return master->array[i];

    if (master->len == master->capacity) {
        int new_capacity = 2 * master->capacity;
        int *new_array = new int[new_capacity];
        master->capacity = new_capacity;
        for (int i = 0; i < master->len; i++)
            new_array[i] = master->array[i];
        delete[] master->array;
        master->array = new_array;
    }
    master->array[master->len] = master->defval;
    master->len++;
    return master->array[master->len-1];
}

void SparseArray<bool>::Interm::Remove()
{
    for (int i = 0; i < master->len; i++)
        if (i == index) {
            for (int j = i; j < master->len - 1; j++)
                master->array[j] = master->array[j+1];
            master->array[master->len - 1] = master->defval;
            break;
        }
}

int SparseArray<bool>::Interm::operator=(const int &x)
{
    if (x == master->defval)
        Remove();
    else
        Provide() = x;
    return x;
}

SparseArray<bool>::Interm::operator int() const
{
    for (int i = 0; i < master->len; i++)
        if (i == index)
            return master->array[i];
    return master->defval;
}

#endif
