#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;
    T *Find(int idx) const;             /* the stored value, or 0 */
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& Push();
        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>
T *SparseArray<T>::Find(int idx) const
{
    Item *tmp;
    for (tmp = first; tmp; tmp = tmp->next)
        if (tmp->index == idx)
            return &tmp->value;
    return 0;
}

template <class T>
const T& SparseArray<T>::operator[](int idx) const
{
    T *found = Find(idx);
    if (found)
        return *found;
    return defval;
}

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

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::Push()
{
    Item *tmp = new Item;
    tmp->index = index;
    tmp->value = master->defval;
    tmp->next = master->first;
    master->first = tmp;
    return tmp->value;
}

template <class T>
T& SparseArray<T>::Interm::Provide()
{
    T *found = master->Find(index);
    if (found)
        return *found;
    return Push();
}

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
{
    T *found = master->Find(index);
    if (found)
        return *found;
    return master->defval;
}

template <>
class SparseArray<bool> {
    int *indices;
    int len, capacity;
    bool defval;
    int Find(int idx) const;
    void Add(int idx);
    void Remove(int idx);
    bool Get(int idx) const;
    void Set(int idx, bool x);
public:
    SparseArray(const bool &dv)
        : indices(0), len(0), capacity(0), defval(dv) {}
    ~SparseArray() { delete[] indices; }
    class Interm {
        friend class SparseArray<bool>;
        SparseArray<bool> *master;
        int index;
        Interm(SparseArray<bool> *a_master, int ind)
            : master(a_master), index(ind) {}
    public:
        operator bool() const { return master->Get(index); }
        bool operator=(bool x) { master->Set(index, x); return x; }
        bool operator=(const Interm& x) { return *this = bool(x); }
    };
    friend class Interm;

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

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

inline int SparseArray<bool>::Find(int idx) const
{
    for (int i = 0; i < len; i++)
        if (indices[i] == idx)
            return i;
    return -1;
}

inline void SparseArray<bool>::Add(int idx)
{
    if (Find(idx) != -1)
        return;
    if (len == capacity) {
        int new_capacity = capacity ? 2 * capacity : 16;
        int *new_indices = new int[new_capacity];
        for (int i = 0; i < len; i++)
            new_indices[i] = indices[i];
        delete[] indices;
        indices = new_indices;
        capacity = new_capacity;
    }
    indices[len] = idx;
    len++;
}

inline void SparseArray<bool>::Remove(int idx)
{
    int pos = Find(idx);
    if (pos == -1)
        return;
    indices[pos] = indices[len - 1];
    len--;
}

inline bool SparseArray<bool>::Get(int idx) const
{
    bool stored = Find(idx) != -1;
    return stored ? !defval : defval;
}

inline void SparseArray<bool>::Set(int idx, bool x)
{
    if (x == defval)
        Remove(idx);
    else
        Add(idx);
}

#endif
