Advertisement
Slayerfeed

Computer

May 6th, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.25 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3.  
  4. using namespace std;
  5.  
  6. int parent[100010];
  7. int main(){
  8.     int n , m;
  9.  
  10.     cin >> n >> m;
  11.     stack<int> s[n+2];
  12.     int j=1;
  13.     for(int i=1;i<=n;++i){
  14.         s[i].push(0);
  15.     }
  16.     int x ,y  ,z;
  17.     for(int i=1;i<=m;++i){
  18.         char a;
  19.         cin >> a;
  20.  
  21.         if(a=='='){
  22.             cin >> x >> y;
  23.             s[x].push(y);
  24.             parent[j]=x;
  25.             ++j;
  26.         }
  27.         else if(a=='+'){
  28.             cin >> x >> y >> z;
  29.             int u=s[x].top();
  30.             int v=s[y].top();
  31.             s[z].push(u+v);
  32.             parent[j]=z;
  33.             ++j;
  34.         }
  35.         else if(a=='-'){
  36.             cin >> x >> y >> z;
  37.             int u=s[x].top();
  38.             int v=s[y].top();
  39.             s[z].push(u-v);
  40.             parent[j]=z;
  41.             ++j;
  42.         }
  43.         else if(a=='C'){
  44.             cin >> x >> y;
  45.             s[y].push(s[x].top());
  46.             parent[j]=y;
  47.             ++j;
  48.         }
  49.         else if(a=='P'){
  50.             cin >> x;
  51.             cout << s[x].top() << "\n";
  52.         }
  53.         else if(a=='U'){
  54.             cin >> x;
  55.             while(x--){
  56.                 --j;
  57.                 s[parent[j]].pop();
  58.  
  59.             }
  60.         }
  61.     }
  62.  
  63.     return 0;
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement