#include <iostream>
#include <valarray>
template<typename T>
class Matrix {
std::valarray<T> data;
size_t columns;
public:
Matrix(size_t rows, size_t columns)
: data(rows*columns), columns(columns) {}
T operator()(size_t row, size_t col) const
{
return data[row + columns*col];
}
T& operator()(size_t row, size_t col)
{
return data[row + columns*col];
}
Matrix& operator+=(const Matrix& other)
{
data += other.data;
return *this;
}
};
template<typename T>
Matrix<T> operator+(Matrix<T> lhs, const Matrix<T>& rhs)
{
return lhs += rhs;
}
int main()
{
Matrix<int> m1(2,2);
m1(0,0) = 1; m1(0,1) = 2;
m1(1,0) = 3; m1(1,1) = 4;
Matrix<int> m2 = m1;
Matrix<int> m3 = m1 + m2;
std::cout << m3(0,0) << ' ' << m3(0,1) << '\n'
<< m3(1,0) << ' ' << m3(1,1) << '\n';
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8dmFsYXJyYXk+Cgp0ZW1wbGF0ZTx0eXBlbmFtZSBUPgpjbGFzcyBNYXRyaXggewogICAgc3RkOjp2YWxhcnJheTxUPiBkYXRhOwogICAgc2l6ZV90IGNvbHVtbnM7CiBwdWJsaWM6CiAgICBNYXRyaXgoc2l6ZV90IHJvd3MsIHNpemVfdCBjb2x1bW5zKQogICAgOiBkYXRhKHJvd3MqY29sdW1ucyksIGNvbHVtbnMoY29sdW1ucykge30KCiAgICBUIG9wZXJhdG9yKCkoc2l6ZV90IHJvdywgc2l6ZV90IGNvbCkgY29uc3QKICAgIHsKICAgICAgICByZXR1cm4gZGF0YVtyb3cgKyBjb2x1bW5zKmNvbF07CiAgICB9CgogICAgVCYgb3BlcmF0b3IoKShzaXplX3Qgcm93LCBzaXplX3QgY29sKQogICAgewogICAgICAgIHJldHVybiBkYXRhW3JvdyArIGNvbHVtbnMqY29sXTsKICAgIH0KCiAgICBNYXRyaXgmIG9wZXJhdG9yKz0oY29uc3QgTWF0cml4JiBvdGhlcikKICAgIHsKICAgICAgICBkYXRhICs9IG90aGVyLmRhdGE7CiAgICAgICAgcmV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