#ifndef SPARSE_HPP
#define SPARSE_HPP

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

#define SPARSE_ASSIGN(TYPE, OP, ARG)                                    \
        TYPE operator OP(ARG x)                                         \
            { TYPE v = *this; v OP x; return *this = v; }

#define SPARSE_STEP(TYPE, OP)                                           \
        TYPE operator OP() { TYPE v = *this; OP v; return *this = v; }  \
        TYPE operator OP(int)                                           \
            { TYPE old = *this; TYPE v = old; v OP; *this = v; return old; }

template <class T>
class SparseArray {
    struct Item {
        int index;
        T value;
        Item *next;
        Item(int idx, const T& val, Item *nxt)
            : index(idx), value(val), next(nxt) {}
    };
    Item *first;
    T defval;
    T *Find(int idx) const;
    void Remove(int idx);
    const T& Get(int idx) const;
    void Set(int idx, const T& x);
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) {}
    public:
        operator T() const { return master->Get(index); }
        T operator=(const T& x) { master->Set(index, x); return x; }
        T operator=(const Interm& x) { return *this = T(x); }

        SPARSE_ASSIGN(T, +=, const T&)
        SPARSE_ASSIGN(T, -=, const T&)
        SPARSE_ASSIGN(T, *=, const T&)
        SPARSE_ASSIGN(T, /=, const T&)
        SPARSE_ASSIGN(T, %=, const T&)
        SPARSE_ASSIGN(T, |=, const T&)
        SPARSE_ASSIGN(T, &=, const T&)
        SPARSE_ASSIGN(T, ^=, const T&)
        SPARSE_ASSIGN(T, <<=, int)
        SPARSE_ASSIGN(T, >>=, int)
        SPARSE_STEP(T, ++)
        SPARSE_STEP(T, --)

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

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

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

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

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

template <class T>
void SparseArray<T>::Set(int idx, const T& x)
{
    if (x == defval) {
        Remove(idx);
        return;
    }
    T *found = Find(idx);
    if (found)
        *found = x;
    else
        first = new Item(idx, x, first);
}

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::operator->() const
{
    T p = *this;
    if (!p)
        throw NullElement(index);
    return p;
}

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(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); }

        SPARSE_ASSIGN(bool, |=, bool)
        SPARSE_ASSIGN(bool, &=, bool)
        SPARSE_ASSIGN(bool, ^=, bool)
    };
    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>&) {}
};

#undef SPARSE_ASSIGN
#undef SPARSE_STEP

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
