fork download
  1. #pragma GCC optimize("O2")
  2. #pragma GCC target("avx,avx2,fma")
  3. #include <bits/stdc++.h>
  4. using namespace std;
  5. using ll = long long;
  6. const int mod = 1e9 + 7;
  7. #define el endl
  8. #define int int64_t
  9. #define fi first
  10. #define se second
  11. #define file(x) freopen(x".inp", "r", stdin); freopen(x".out", "w", stdout)
  12. #define pb push_back
  13. #define gd() ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr)
  14. #define rizzler signed main()
  15. #define tc() int t; cin >> t; while(t--)
  16. void tle() {
  17. double time_taken = 1000.0 * clock() / CLOCKS_PER_SEC;
  18.  
  19. if (time_taken < 1000.0) {
  20. std::cerr << "Time: ";
  21. std::cerr << time_taken << "ms" << std::string(27, '\t');
  22. } else {
  23. std::cerr << "TLE Warning!\n";
  24. std::cerr << "Time: ";
  25. std::cerr << (time_taken / 1000.0) << "s" << std::string(27, '\n');
  26. }
  27. }
  28.  
  29.  
  30.  
  31.  
  32. rizzler{
  33. gd();
  34. tc(){
  35. int n; cin >> n;
  36. string s; getline(cin >> ws, s);
  37. vector<string> v;
  38. stack<int> st;
  39. stringstream ss(s);
  40. string word;
  41. while(ss >> word){
  42. v.pb(word);
  43. }
  44. for (int i = 0; i < v.size(); i++){
  45. if (isdigit(v[i].back())){
  46. st.push(stoll(v[i]));
  47. } else {
  48. auto top2 = st.top(); st.pop();
  49. auto top1 = st.top(); st.pop();
  50. if (v[i] == "+"){
  51. st.push(top1 + top2);
  52. } else if (v[i] == "-"){
  53. st.push(top1 - top2);
  54. } else if (v[i] == "*"){
  55. st.push(top1*top2);
  56. } else {
  57. st.push(top1/top2);
  58. }
  59. }
  60. }
  61. cout << st.top() << el;
  62.  
  63. }
  64.  
  65.  
  66. tle();
  67.  
  68. }
  69.  
Success #stdin #stdout #stderr 0s 5284KB
stdin
3
7
2 3 1 * + 9 –
7
8 7 5 * + 9 –
3
-10 -4 +
stdout
0
4
-14
stderr
Time: 4.401ms