Guest User

Untitled

a guest
Jul 15th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.66 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <iostream>
  5.  
  6. int z, n, p, r, f, d, num;
  7. int i;
  8.  
  9. struct Node {
  10.     char sign;
  11.     Node *father, *brother, *leftchild;
  12.     int result;
  13. } *head, *anc, *wsk;
  14.  
  15. struct V {
  16.     Node *pointer;
  17. } *Vals;
  18.  
  19.  
  20.  
  21. Node *build(Node* vater) {
  22.     Node *wsk;
  23.     char c;
  24.     Node *node = NULL;
  25.     scanf("%c", &c);
  26.     if(c == '(') {
  27.         scanf("%c", &c);
  28.         node = new Node;
  29.         node->sign = c;
  30.         node->father = vater;
  31.         node->leftchild = NULL;
  32.         node->brother = NULL;
  33.  
  34.         node->leftchild = build(node);
  35.         wsk = node->leftchild;
  36.         wsk->brother = build(node);
  37.         while(wsk->brother != NULL) {
  38.             wsk = wsk->brother;
  39.             wsk->brother = build(node);
  40.         }
  41.        
  42.     }
  43.     else if(c == 'x') {
  44.         node = new Node;
  45.         node->father = vater;
  46.         node->leftchild = NULL;
  47.         node->brother = NULL;
  48.         node->sign = c;
  49.         Vals[num++].pointer = node;
  50.     }
  51.  
  52.     return node;
  53. }
  54.  
  55.  
  56. int count(Node *node) {
  57.     int res = 0;
  58.     Node *wsk;
  59.  
  60.     if(node->leftchild == NULL)
  61.         return node->result;
  62.     else {
  63.         if(node->sign == '+') {
  64.             res = 0;
  65.             wsk = node->leftchild;
  66.             while(wsk != NULL) {
  67.                 res += count(wsk);
  68.                 wsk = wsk->brother;
  69.             }
  70.             node->result = res;
  71.             return res;
  72.         }
  73.         else if(node->sign == '-') {
  74.             res = (-1) * count(node->leftchild);
  75.             node->result = res;
  76.             return res;
  77.         }
  78.         else if(node->sign == '>') {
  79.             wsk = node->leftchild;
  80.             res = count(wsk);
  81.             wsk = wsk->brother;
  82.             while(wsk != NULL) {
  83.                 if(count(wsk) > res) res = wsk->result;
  84.                 wsk = wsk->brother;
  85.             }
  86.             node->result = res;
  87.         }
  88.         else if(node->sign == '<') {
  89.             wsk = node->leftchild;
  90.             res = count(wsk);
  91.             wsk = wsk->brother;
  92.             while(wsk != NULL) {
  93.                 if(count(wsk) < res) res = wsk->result;
  94.                 wsk = wsk->brother;
  95.             }
  96.             node->result = res;
  97.         }
  98.     }
  99.    
  100.     return res;
  101. }
  102.  
  103. void update(Node *node, int u) {
  104.     int temp, temp2;
  105.     Node *wsk, *pointer;
  106.     temp = node->result;
  107.     node->result = u;
  108.     wsk = node;
  109.     while(wsk->father != NULL) {
  110.         if(wsk->father->sign == '+') {
  111.             temp2 = wsk->father->result;
  112.             if(u-temp == 0) return;
  113.             wsk->father->result += u - temp;
  114.             temp = temp2;
  115.             u = wsk->father->result;
  116.         }
  117.         if(wsk->father->sign == '-') {
  118.             temp = wsk->father->result;
  119.             wsk->father->result = (-1) * u;
  120.             u = wsk->father->result;
  121.         }
  122.         if(wsk->father->sign == '>') {
  123.             temp2 = wsk->father->result;
  124.             if(u > wsk->father->result) {
  125.                 temp = temp2;
  126.                 wsk->father->result = u;
  127.                 u = wsk->father->result;
  128.             }
  129.             else if(u < wsk->father->result && temp < wsk->father->result) {
  130.                 temp = wsk->father->result;
  131.                 u = temp;
  132.                 return;
  133.             }
  134.             else {
  135.                 temp = wsk->father->result;
  136.                 pointer = wsk->father->leftchild;
  137.                 u = pointer->result;
  138.                 pointer = pointer->brother;
  139.                 while(pointer != NULL) {
  140.                     if(pointer->result > u) u = pointer->result;
  141.                     pointer = pointer->brother;
  142.                 }
  143.                 if(u==temp) return;
  144.                 wsk->father->result = u;
  145.             }
  146.         }
  147.         if(wsk->father->sign == '<') {
  148.             temp2 = wsk->father->result;
  149.             if(u < wsk->father->result) {
  150.                 temp = temp2;
  151.                 wsk->father->result = u;
  152.                 u = wsk->father->result;
  153.             }
  154.             else if(u > wsk->father->result && temp > wsk->father->result) {
  155.                 temp = wsk->father->result;
  156.                 u = temp;
  157.                 return;
  158.             }
  159.             else {
  160.                 temp = wsk->father->result;
  161.                 pointer = wsk->father->leftchild;
  162.                 u = pointer->result;
  163.                 pointer = pointer->brother;
  164.                 while(pointer != NULL) {
  165.                     if(pointer->result < u) u = pointer->result;
  166.                     pointer = pointer->brother;
  167.                 }
  168.                 if(u==temp) return;
  169.                 wsk->father->result = u;
  170.             }
  171.  
  172.         }
  173.         wsk = wsk->father;
  174.     }
  175. }
  176.  
  177. void remove(Node *node) {
  178.     Node *wsk1 = node->leftchild, *wsk2 = node->brother;
  179.     delete node;
  180.     if(wsk1 != NULL) remove(wsk1);
  181.     if(wsk2 != NULL) remove(wsk2);
  182.    
  183. }
  184.  
  185. int main() {
  186.     char c;
  187.     scanf("%d", &z);
  188.     for (int j = 0; j < z; ++j) {
  189.        scanf("%d %d", &p, &r);
  190.        if(p==0) continue;
  191.        Vals = new V[p];
  192.  
  193.        num = 0;
  194.        i = 0;
  195.        scanf("%c", &c);
  196.        while(c == '\n' || c == ' ')
  197.            scanf("%c", &c);
  198.        ungetc(c, stdin);
  199.        head = build(NULL);
  200.  
  201.        for(int i = 0; i < p; i++) {
  202.            scanf("%d", &Vals[i].pointer->result);
  203.        }
  204.  
  205.        count(head);
  206.        printf("%d\n", head->result);
  207.  
  208.        for(int i = 0; i < r; i++) {
  209.            scanf("%d %d", &d, &f);
  210.            if(f != Vals[d].pointer->result) update(Vals[d].pointer, f);
  211.            printf("%d\n", head->result);
  212.        }
  213.  
  214.        remove(head);
  215.        delete [] Vals;
  216.        
  217.     }
  218.     return 0;
  219. }
Add Comment
Please, Sign In to add comment