Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <vector>
- #include <iostream>
- #include <iterator>
- #include <sstream>
- class SuperVector {
- public:
- SuperVector(int n, bool is_sum, int mod = 0) {
- size_ = n;
- capacity_ = n * 2;
- arr_ = new int[capacity_];
- for (int i = 0; i < n; ++i) {
- std::cin >> arr_[i];
- }
- mod_ = mod;
- is_operation_sum_ = is_sum;
- }
- ~SuperVector() {
- delete[] arr_;
- }
- void compute(int start, int end) {
- int sum = 0;
- if (is_operation_sum_) {
- for (int i = start; i < end + 1; ++i) {
- sum += arr_[i - 1];
- }
- std::cout << sum % mod_ << '\n';
- } else {
- for (int i = start; i < end + 1; ++i) {
- sum ^= arr_[i - 1];
- }
- std::cout << sum << '\n';
- }
- }
- size_t size() const {
- return size_;
- }
- void insert(size_t pos, int value) {
- --pos;
- int *temp = new int[capacity_];
- for (size_t i = 0; i < size_; ++i) {
- temp[i] = arr_[i];
- }
- if (size_ >= capacity_) {
- delete[] arr_;
- arr_ = new int[capacity_ * 2];
- ++size_;
- for (size_t i = 0; i < pos; ++i) {
- arr_[i] = temp[i];
- }
- arr_[pos] = value;
- for (size_t i = pos + 1; i < size_; ++i) {
- arr_[i] = temp[i - 1];
- }
- } else {
- for (size_t i = 0; i < pos; ++i) {
- arr_[i] = temp[i];
- }
- arr_[pos] = value;
- ++size_;
- for (size_t i = pos + 1; i < size_; ++i) {
- arr_[i] = temp[i - 1];
- }
- }
- delete[] temp;
- }
- void erase(size_t pos) {
- --pos;
- for (size_t i = pos; i < size_ - 1; ++i) {
- arr_[i] = arr_[i + 1];
- }
- --size_;
- }
- int at(size_t pos) {
- return arr_[--pos];
- }
- private:
- int *arr_;
- size_t size_;
- size_t capacity_;
- bool is_operation_sum_ = true;
- int mod_ = 0;
- };
- std::vector<std::string> split(const std::string &line, char delim) {
- std::stringstream ss(line);
- std::string item;
- std::vector<std::string> elems;
- while (std::getline(ss, item, delim)) {
- elems.push_back(item);
- }
- return elems;
- }
- int main() {
- std::ios_base::sync_with_stdio(false);
- std::cin.tie(nullptr);
- std::cout.tie(nullptr);
- std::string command;
- std::getline(std::cin, command);
- int n, m;
- std::cin >> n;
- SuperVector *super_vector;
- if (command == "^") {
- super_vector = new SuperVector(n, false);
- } else {
- int mod = std::stoi(split(command, ' ')[1]);
- super_vector = new SuperVector(n, true, mod);
- }
- std::cin >> m;
- for (int i = 0; i < m; ++i) {
- std::string line;
- if (i == 0) {
- std::getline(std::cin, line);
- }
- std::getline(std::cin, line);
- auto command_arr = split(line, ' ');
- if (command_arr[0] == "insert") {
- int pos, x;
- pos = std::stoi(command_arr[1]);
- x = std::stoi(command_arr[2]);
- super_vector->insert(pos, x);
- } else if (command_arr[0] == "erase") {
- super_vector->erase(std::stoi(command_arr[1]));
- } else if (command_arr[0] == "at") {
- std::cout << super_vector->at(std::stoi(command_arr[1])) << '\n';
- } else if (command_arr[0] == "compute") {
- int l, r;
- l = std::stoi(command_arr[1]);
- r = std::stoi(command_arr[2]);
- super_vector->compute(l, r);
- } else if (command_arr[0] == "size") {
- std::cout << super_vector->size() << '\n';
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement