Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- struct Node {
- Node(int left, int right) : left_bound(left), right_bound(right) {}
- Node* left_son = nullptr;
- Node* right_son = nullptr;
- int left_bound = 0;
- int right_bound = 0;
- int sum = 0;
- };
- Node* MakeNode(int left, int right) { // ДО индексируется с 1
- Node* node = new Node(left, right);
- if (left != right) {
- int middle = (left + right) / 2;
- node->left_son = MakeNode(left, middle);
- node->right_son = MakeNode(middle + 1, right);
- node->sum = node->left_son->sum + node->right_son->sum;
- } else {
- node->sum = 0;
- }
- return node;
- }
- Node* set(Node* node, int position, int new_value) {
- if (node->left_son) {
- if (position <= node->left_son->right_bound) {
- Node* new_node = new Node(position, position);
- new_node->right_son = node->right_son;
- new_node->left_son = set(node->left_son, position, new_value);
- new_node->sum = new_node->right_son->sum + new_node->left_son->sum;
- new_node->left_bound = node->left_bound;
- new_node->right_bound = node->right_bound;
- return new_node;
- } else {
- Node* new_node = new Node(position, position);
- new_node->left_son = node->left_son;
- new_node->right_son = set(node->right_son, position, new_value);
- new_node->sum = new_node->left_son->sum + new_node->right_son->sum;
- new_node->left_bound = node->left_bound;
- new_node->right_bound = node->right_bound;
- return new_node;
- }
- } else {
- Node* new_node = new Node(position, position);
- new_node->sum = new_value;
- return new_node;
- }
- }
- //int get_r(Node* node, int k, bool* is_not_here) { // k начинается с 1
- // if (k > node->sum) {
- // *is_not_here = true;
- // return 0;
- // }
- // if (!node->left_son) { return node->left_bound; }
- // if (node->left_son->sum >= k) {
- // return get_r(node->left_son, k, is_not_here);
- // } else {
- // return get_r(node->right_son, k - node->left_son->sum, is_not_here);
- // }
- //}
- int main() {
- int n, q;
- std::cin >> n >> q;
- std::vector<int> input;
- // for (int i = 0; i < n; ++i) {
- // int x;
- // std::cin >> x;
- // input.push_back(x);
- // }
- std::vector<Node*> versions;
- // std::set<int> used;
- // std::map<int, int> int_to_ind;
- // std::vector<int> numbers(input.size(), 0);
- // numbers[input.size() - 1] = 1;
- // int_to_ind[input[input.size() - 1]] = input.size() - 1;
- // used.insert(input[input.size() - 1]);
- Node* root = MakeNode(1, n);
- versions.push_back(root);
- for (int i = 1; i <= q; ++i) {
- int m, p, x;
- std::cin >> m >> p >> x;
- Node* new_version = set(versions[m], p + 1, x); // +- 1
- versions.emplace_back(new_version);
- // new_version = set(new_version, l + 1, 1);
- }
- int sum = 0;
- for (auto elem : versions) {
- sum += elem->sum;
- }
- std::cout << sum;
- // for (int l = input.size() - 2; l >= 0; --l) {
- // if (used.contains(input[l])) {
- // Node* new_version = set(versions.back(), int_to_ind[input[l]] + 1, 0);
- // new_version = set(new_version, l + 1, 1);
- // int_to_ind[input[l]] = l;
- // versions.push_back(new_version);
- // } else {
- // int_to_ind[input[l]] = l;
- // used.insert(input[l]);
- // Node* new_version = set(versions.back(), l + 1, 1);
- // versions.push_back(new_version);
- // }
- // }
- // int q = 0;
- // std::cin >> q;
- // int p = 0;
- // for (int i = 0; i < q; ++i) {
- // int x, y;
- // std::cin >> x >> y;
- // int l = ((x + p) % n) + 1;
- // int k = ((y + p) % m) + 1;
- //
- // bool is_not_there = false;
- // int result = get_r(versions[versions.size() - l], k, &is_not_there);
- // if (is_not_there) {
- // std::cout << 0 << std::endl;
- // p = 0;
- // } else {
- // std::cout << result << std::endl;
- // p = result;
- // }
- // }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement