fork download
  1. #include <bits/stdc++.h>
  2. #include <ext/pb_ds/assoc_container.hpp>
  3. #include <ext/pb_ds/tree_policy.hpp>
  4. using namespace std;
  5. using namespace __gnu_pbds; // needed for tree + PBDS
  6.  
  7. #define pb push_back
  8. #define all(x) x.begin(), x.end()
  9. typedef long long ll;
  10. const ll INF = (ll)1e18;
  11. const ll MOD = 1000000000+7 ;
  12. const double eps = 1e-9 ;
  13. template <class T>
  14. using ordered_set = tree<
  15. T,
  16. null_type,
  17. less<T>,
  18. rb_tree_tag,
  19. tree_order_statistics_node_update
  20. >;
  21.  
  22. void solve() {
  23. ll q ; cin >> q ;
  24. // cout << q << endl ;
  25. ll total = 0 , previous = 0 ;
  26. ll last = 0 , sz = 0 ;
  27. bool reversed = false ;
  28. deque < ll > dq ;
  29. while (q -- ) {
  30. // cout << "hh" << endl ;
  31.  
  32. int s ; cin >> s ;
  33. // cout << s <<"jj" << endl ;
  34. ll neww = 0 ;
  35. if (s == 1 ) {
  36. if (reversed) {
  37. last = dq.front();
  38. }
  39. else last = dq.back() ;
  40. neww =previous +(total-last) - last * (sz-1) ;
  41. previous = neww ;
  42. cout << neww << endl ;
  43. if (reversed ) {
  44. ll x = dq.front() ;dq.pop_front() ;dq.push_back(x) ;
  45. }
  46. else {
  47. ll x = dq.back() ; dq.pop_back() ; dq.push_front(x) ;
  48. }
  49. }
  50. else if (s == 2 ) {
  51. neww = (sz +1 )* (total) - previous ;
  52. cout << neww << endl ;
  53. previous = neww ;
  54. reversed^=1 ;
  55. }
  56. else {
  57. ll k ; cin >> k ;
  58. sz++ ;
  59. neww = previous + k * sz ;
  60. if (reversed ) {
  61. dq.push_front(k) ;
  62. }
  63. else dq.push_back(k) ;
  64. total += k ;
  65. previous = neww ;
  66. cout << neww << endl ;
  67. }
  68. }
  69. }
  70.  
  71.  
  72.  
  73. int main() {
  74. ios::sync_with_stdio(false);
  75. cin.tie(nullptr);
  76. #ifndef ONLINE_JUDGE
  77. freopen("input.txt", "r", stdin);
  78. freopen("output.txt", "w", stdout);
  79. #endif
  80. int t = 1 ; cin >> t;
  81. int maxi = 200000 + 5;
  82.  
  83. while (t--) solve();
  84. return 0;
  85. }
Success #stdin #stdout 0.01s 5324KB
stdin
Standard input is empty
stdout
Standard output is empty