Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- #include <iostream>
- int z, n, p, r, f, d, num;
- int i;
- struct Node {
- char sign;
- Node *father, *brother, *leftchild;
- int result;
- } *head, *anc, *wsk;
- struct V {
- Node *pointer;
- } *Vals;
- Node *build(Node* vater) {
- Node *wsk;
- char c;
- Node *node = NULL;
- scanf("%c", &c);
- if(c == '(') {
- scanf("%c", &c);
- node = new Node;
- node->sign = c;
- node->father = vater;
- node->leftchild = NULL;
- node->brother = NULL;
- node->leftchild = build(node);
- wsk = node->leftchild;
- wsk->brother = build(node);
- while(wsk->brother != NULL) {
- wsk = wsk->brother;
- wsk->brother = build(node);
- }
- }
- else if(c == 'x') {
- node = new Node;
- node->father = vater;
- node->leftchild = NULL;
- node->brother = NULL;
- node->sign = c;
- Vals[num++].pointer = node;
- }
- return node;
- }
- int count(Node *node) {
- int res = 0;
- Node *wsk;
- if(node->leftchild == NULL)
- return node->result;
- else {
- if(node->sign == '+') {
- res = 0;
- wsk = node->leftchild;
- while(wsk != NULL) {
- res += count(wsk);
- wsk = wsk->brother;
- }
- node->result = res;
- return res;
- }
- else if(node->sign == '-') {
- res = (-1) * count(node->leftchild);
- node->result = res;
- return res;
- }
- else if(node->sign == '>') {
- wsk = node->leftchild;
- res = count(wsk);
- wsk = wsk->brother;
- while(wsk != NULL) {
- if(count(wsk) > res) res = wsk->result;
- wsk = wsk->brother;
- }
- node->result = res;
- }
- else if(node->sign == '<') {
- wsk = node->leftchild;
- res = count(wsk);
- wsk = wsk->brother;
- while(wsk != NULL) {
- if(count(wsk) < res) res = wsk->result;
- wsk = wsk->brother;
- }
- node->result = res;
- }
- }
- return res;
- }
- void update(Node *node, int u) {
- int temp, temp2;
- Node *wsk, *pointer;
- temp = node->result;
- node->result = u;
- wsk = node;
- while(wsk->father != NULL) {
- if(wsk->father->sign == '+') {
- temp2 = wsk->father->result;
- if(u-temp == 0) return;
- wsk->father->result += u - temp;
- temp = temp2;
- u = wsk->father->result;
- }
- if(wsk->father->sign == '-') {
- temp = wsk->father->result;
- wsk->father->result = (-1) * u;
- u = wsk->father->result;
- }
- if(wsk->father->sign == '>') {
- temp2 = wsk->father->result;
- if(u > wsk->father->result) {
- temp = temp2;
- wsk->father->result = u;
- u = wsk->father->result;
- }
- else if(u < wsk->father->result && temp < wsk->father->result) {
- temp = wsk->father->result;
- u = temp;
- return;
- }
- else {
- temp = wsk->father->result;
- pointer = wsk->father->leftchild;
- u = pointer->result;
- pointer = pointer->brother;
- while(pointer != NULL) {
- if(pointer->result > u) u = pointer->result;
- pointer = pointer->brother;
- }
- if(u==temp) return;
- wsk->father->result = u;
- }
- }
- if(wsk->father->sign == '<') {
- temp2 = wsk->father->result;
- if(u < wsk->father->result) {
- temp = temp2;
- wsk->father->result = u;
- u = wsk->father->result;
- }
- else if(u > wsk->father->result && temp > wsk->father->result) {
- temp = wsk->father->result;
- u = temp;
- return;
- }
- else {
- temp = wsk->father->result;
- pointer = wsk->father->leftchild;
- u = pointer->result;
- pointer = pointer->brother;
- while(pointer != NULL) {
- if(pointer->result < u) u = pointer->result;
- pointer = pointer->brother;
- }
- if(u==temp) return;
- wsk->father->result = u;
- }
- }
- wsk = wsk->father;
- }
- }
- void remove(Node *node) {
- Node *wsk1 = node->leftchild, *wsk2 = node->brother;
- delete node;
- if(wsk1 != NULL) remove(wsk1);
- if(wsk2 != NULL) remove(wsk2);
- }
- int main() {
- char c;
- scanf("%d", &z);
- for (int j = 0; j < z; ++j) {
- scanf("%d %d", &p, &r);
- if(p==0) continue;
- Vals = new V[p];
- num = 0;
- i = 0;
- scanf("%c", &c);
- while(c == '\n' || c == ' ')
- scanf("%c", &c);
- ungetc(c, stdin);
- head = build(NULL);
- for(int i = 0; i < p; i++) {
- scanf("%d", &Vals[i].pointer->result);
- }
- count(head);
- printf("%d\n", head->result);
- for(int i = 0; i < r; i++) {
- scanf("%d %d", &d, &f);
- if(f != Vals[d].pointer->result) update(Vals[d].pointer, f);
- printf("%d\n", head->result);
- }
- remove(head);
- delete [] Vals;
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment