ivolff

LabASD 3.B

Apr 8th, 2018
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.91 KB | None | 0 0
  1.  
  2. #include <iostream>
  3. #include <cmath>
  4. #include <queue>
  5.  
  6. using namespace std;
  7.  
  8. struct node{
  9.     node* from=NULL;
  10.     node* left=NULL;
  11.     node* right=NULL;
  12.     int value;
  13.     node(node *from){
  14.         this->from=from;
  15.     }
  16.     node();
  17. };
  18.  
  19.  
  20. node* head=new node();
  21.  
  22. int D=0;
  23. vector<int> base;
  24. vector<node*> base_nods;
  25.  
  26. int build_tree(node *curent,int deep,int cur){
  27.     node* left=new node(curent);
  28.     node* right=new node(curent);
  29.     int L,R;
  30.     if(cur!=deep){
  31.         curent->left=left;
  32.         curent->right=right;
  33.         L=build_tree(left,deep,cur+1);
  34.         R=build_tree(right,deep,cur+1);
  35.         curent->value=R+L;
  36.         return R+L;}
  37.     else {
  38.         base_nods.push_back(curent);
  39.         if(D<base.size()){
  40.             curent->value = base[D];
  41.         D++;}
  42.         else
  43.             curent->value = 0;
  44.         return curent->value;
  45.     }
  46. }
  47.  
  48. int left_summ(node* cur){
  49.     if(cur->from!=NULL){
  50.         if(cur->from->left!=cur)
  51.             return (cur->from->left->value)+left_summ(cur->from);
  52.         else
  53.             return left_summ(cur->from);}
  54.     else
  55.         return 0;
  56. }
  57.  
  58. int right_summ(node* cur){
  59.     if(cur->from!=NULL){
  60.         if(cur->from->right!=cur)
  61.             return (cur->from->right->value)+left_summ(cur->from);
  62.         else
  63.             return left_summ(cur->from);}
  64.     else
  65.         return 0;
  66. }
  67.  
  68.  
  69. int main() {
  70.     int n;
  71.     cin>>n;
  72.     for(int i=0;i<n;i++){
  73.         int tmp;
  74.         cin>>tmp;
  75.         base.push_back(tmp);
  76.     }
  77.     int deep=log2(n);
  78.     if(n!=pow(2,deep))
  79.         deep++;
  80.     build_tree(head,deep,0);
  81.     string command;
  82.     int a,b;
  83.     while(cin>>command){
  84.         if(command=="set"){
  85.             cin>>a>>b;
  86.             base_nods[a-1]->value=b;
  87.         }
  88.         else{
  89.             cin>>a>>b;
  90.             a=left_summ(base_nods[a-1]);
  91.             b=right_summ(base_nods[b-1]);
  92.         }
  93.     }
  94.     return 0;
  95. }
Advertisement
Add Comment
Please, Sign In to add comment