Skip to content

Instantly share code, notes, and snippets.

@madmongo1
Created September 9, 2017 11:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save madmongo1/e0be91e5feb46b7181eea3af534065c3 to your computer and use it in GitHub Desktop.
Save madmongo1/e0be91e5feb46b7181eea3af534065c3 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
struct IO
{
std::istream& input;
std::ostream& output;
std::ostream& error;
};
struct command_history;
struct command
{
command(int code, int l, int r)
: type(code)
, l(l)
, r(r) {}
void operator ()(command_history& history, std::vector<int>& A) const;
int type, l, r;
};
struct command_history
{
command_history(int capacity)
: store_()
{
store_.reserve(capacity);
}
void emplace(int code, int l, int r)
{
store_.emplace_back(code, l, r);
}
void execute(std::vector<int>& A, int l, int r)
{
for (int i = l; i <= r; ++i) {
store_[i - 1](*this, A);
}
}
void execute_top(std::vector<int>& A)
{
store_.back()(*this, A);
}
std::vector<command> store_;
};
void command::operator ()(command_history& history, std::vector<int>& A) const
{
if (type == 1) {
for (int i = l; i <= r; ++i) {
A[i - 1] += 1;
}
}
else {
history.execute(A, l, r);
}
}
void test_case(IO const& io, int n, int m)
{
command_history history(m);
auto A = std::vector<int>(n, 0);
for (int i = 1; i <= m; ++i) {
int code, l, r;
io.input >> code >> l >> r;
history.emplace(code, l, r);
history.execute_top(A);
}
const char *sep = "";
for (auto && x : A) {
io.output << sep << x;
sep = " ";
}
io.output << '\n';
}
void test_cases(IO const& io)
{
int T;
io.input >> T;
for (int test_index = 0; test_index < T; ++test_index) {
int n, m;
io.input >> n >> m;
test_case(io, n, m);
}
}
// set to 0 for submission
#define TEST_ALGORITHM 1
int main()
{
#if TEST_ALGORITHM
using namespace std::literals;
const char test_data[] = R"__(3
5 5
1 1 2
1 4 5
2 1 2
2 1 3
2 3 4
1 2
1 1 1
1 1 1
10 10
1 1 10
2 1 1
2 1 2
2 1 3
2 1 4
2 1 5
2 1 6
2 1 7
2 1 8
2 1 9)__";
auto test_is = std::istringstream(test_data);
test_cases(IO{test_is, std::cout, std::cerr});
#else
test_cases(IO{std::cin, std::cout, std::cerr});
#endif
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment