Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <random>
- #include <vector>
- using namespace std;
- const long long MOD = 1e9 + 7, N = 2e3 + 7;
- mt19937 rnd;
- struct Node {
- int key, priority, size = 1;
- Node* left = nullptr;
- Node* right = nullptr;
- Node(int key) : key(key), priority(rnd()) {}
- };
- int get_size(Node* a) {
- if (a == nullptr) {
- return 0;
- }
- else {
- return a->size;
- }
- }
- Node* update_size(Node* a) {
- a->size = get_size(a->left) + 1 + get_size(a->right);
- return a;
- }
- pair<Node*, Node*> split(Node* root, int key) {
- if (root == nullptr) {
- return { nullptr, nullptr };
- }
- if (root->key <= key) {
- auto splitted = split(root->right, key);
- root->right = splitted.first;
- root = update_size(root);
- return { root, splitted.second };
- }
- auto splitted = split(root->left, key);
- root->left = splitted.second;
- root = update_size(root);
- return { splitted.first, root };
- };
- Node* merge(Node* a, Node* b) {
- if (a == nullptr) {
- return b;
- }
- if (b == nullptr) {
- return a;
- }
- if (a->priority < b->priority) {
- a->right = merge(a->right, b);
- a = update_size(a);
- return a;
- }
- else {
- b->left = merge(a, b->left);
- b = update_size(b);
- return b;
- }
- }
- Node* insert(Node* root, Node* newNode) {
- auto splitted = split(root, newNode->key);
- root = merge(merge(splitted.first, newNode), splitted.second);
- return root;
- }
- Node* erase(Node* root, int x) {
- auto splitted = split(root, x);
- auto splitted_less = split(splitted.first, x - 1);
- if (splitted_less.second != nullptr) {
- delete splitted_less.second;
- root = merge(splitted_less.first, splitted.second);
- return root;
- }
- else {
- return root;
- }
- }
- int find_lessandeq(Node* root, int x) {
- auto splitted = split(root, x);
- int ans = get_size(splitted.first);
- root = merge(splitted.first, splitted.second);
- return ans;
- }
- int find_less(Node* root, int x) {
- auto splitted = split(root, x-1);
- int ans = get_size(splitted.first);
- root = merge(splitted.first, splitted.second);
- return ans;
- }
- int find_moreandeq(Node* root, int x) {
- auto splitted = split(root, x-1);
- int ans = get_size(splitted.second);
- root = merge(splitted.first, splitted.second);
- return ans;
- }
- int find_more(Node* root, int x) {
- auto splitted = split(root, x);
- int ans = get_size(splitted.second);
- root = merge(splitted.first, splitted.second);
- return ans;
- }
- long long n, m, total_rasst, x, y;
- Node* root_x, *root_y;
- char s;
- int main()
- {
- root_x = nullptr;
- root_y = nullptr;
- cin >> n >> m;
- for (int i = 0; i < n; i++) {
- cin >> x >> y;
- total_rasst += abs(x) + abs(y);
- root_x = insert(root_x, new Node(x));
- root_y = insert(root_y, new Node(y));
- }
- x = 0; y = 0;
- for (int i = 0; i < m; i++) {
- cin >> s;
- if (s == 'S') {
- total_rasst += find_lessandeq(root_y, y);
- total_rasst -= find_more(root_y, y);
- y++;
- cout << total_rasst << "\n";
- }
- if (s == 'I') {
- total_rasst += find_lessandeq(root_x, x);
- total_rasst -= find_more(root_x, x);
- x++;
- cout << total_rasst << "\n";
- }
- if (s == 'J') {
- total_rasst += find_moreandeq(root_y, y);
- total_rasst -= find_less(root_y, y);
- y--;
- cout << total_rasst << "\n";
- }
- if (s == 'Z') {
- total_rasst += find_moreandeq(root_x, x);
- total_rasst -= find_less(root_x, x);
- x--;
- cout << total_rasst << "\n";
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement