Advertisement
Guest User

Untitled

a guest
Jan 18th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.76 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <set>
  4. #include <map>
  5. #include <algorithm>
  6. #include <iomanip>
  7. #include <cmath>
  8. #include <queue>
  9. #pragma GCC optimize("O3")
  10. const long long mod1=998244353;
  11. const long long mod2=1000000007;
  12. long long pow(int a,int b) {
  13. if (b==0 || a==1) return 1;
  14.  
  15. if (b%2==0) { long long k=pow(a,b/2); return (k*k); }
  16. else { long long k=pow(a,b/2); return k*k*a; }
  17. }
  18. long long powmod(long long a,long long b,long long mod) {
  19. if (b==0 || a==1) {if (mod==1) return 0; else return 1; }
  20.  
  21. if (b%2==0) { long long k=powmod(a,b/2,mod); return (k*k)%mod; }
  22. else {long long k=powmod(a,b/2,mod); return ( (k*k) %mod *a)% mod; }
  23. }
  24. long long gcd(long long a, long long b) {
  25. if (a==0) return b;
  26. if (b==0) return a;
  27. if (a>b) return gcd(a%b,b); else return gcd(b%a,a);
  28. }
  29. int prime(int p) { // 1 - простое
  30. for (int i=2;i*i<=p;i++) {
  31. if (p%i==0 && i<p) return 0;
  32. }
  33. return 1;
  34. }
  35. long long sqr(long long i) {
  36. return i*i;
  37. }
  38. using namespace std;
  39.  
  40. int main() {
  41. ios_base::sync_with_stdio(0);
  42. cin.tie(0);
  43. cout.tie(0);
  44.  
  45. /* --------- */
  46.  
  47. int a;
  48. cin>>a;
  49. vector <int> ans;
  50. vector <pair<int,pair<int,int>>> cur;
  51. int ser11=-1,ser21=-1;
  52. int ser12=0,ser22=0;
  53. int last1=0;
  54. int last2=0;
  55. int first1=0;
  56. int first2=0;
  57. int t=0;
  58. int pushes=0;
  59. for (int i=0;i<a;i++) {
  60. char q;
  61. cin>>q;
  62. if (q=='-') { ans.push_back(cur[first2].first);
  63. // if (i==5) { cout<<t; return 0; }
  64. if (t==1) {first1=-1; last1=-1; first2=-1; last2=-1; }
  65. else {
  66. // if (t==2) cout<<first2<<" "<<last1;
  67. //cout<<first2<<" ";
  68. first2=cur[first2].second.second;
  69. // cout<<first2<<"\n";
  70. first1=cur[first2].first;
  71. if (t%2==0) {
  72. ser11=ser21;
  73. ser12=ser22;
  74. ser21=cur[ser21].second.second;
  75. ser22=cur[ser21].first;
  76. }
  77. }
  78.  
  79. t--;
  80.  
  81. } else {
  82. if (q=='+') {int w; cin>>w;
  83.  
  84. if (t==0) { first1=w; first2=pushes; last1=pushes; last2=w; t++;
  85. cur.push_back({w, {last1, -1}}); pushes++;} else {
  86. cur[last1].second.second=pushes;
  87. // cout<<last1;
  88. cur.push_back({w, {last1, -1}});
  89. last1=pushes;
  90. last2=w;
  91. t++;
  92. if (t==2) {ser11=first2; ser21=last1; ser12=cur[ser11].first; ser22=cur[ser21].first; }
  93. pushes++;
  94.  
  95.  
  96. if (t%2==1) {ser11=ser21; ser12=ser22;
  97. ser21=cur[ser21].second.second;
  98. ser22=cur[ser21].first;
  99. }
  100. }
  101. }
  102.  
  103.  
  104.  
  105.  
  106.  
  107. else { int w; cin>>w;
  108.  
  109. if (t==0) { cur.push_back({w, {-1, -1}}); first1=w; first2=pushes; last1=pushes; last2=w; pushes++; t++; } else
  110. if (t==1) {cur.push_back({w, {first2, -1}}); last1=pushes; cur[first2].second.second=last1; ser11=first2; last2=w; ser21=last1; pushes++; t++; } else
  111. if (t%2==0) {
  112. cur.push_back({w, {ser11,ser21}});
  113.  
  114. cur[ser11].second.second=pushes;
  115.  
  116. cur[ser21].second.first=pushes;
  117. ser11=pushes;
  118. ser12=w; pushes++; t++;
  119. } else {
  120. cur.push_back({w, {ser11,ser21}});
  121. cur[ser11].second.second=pushes;
  122. cur[ser21].second.first=pushes;
  123. ser21=pushes;
  124. ser22=w;
  125. pushes++;
  126. t++;
  127. }
  128.  
  129. }
  130. }
  131.  
  132. //if (i==2) {cout<<first2<<" "<<last1<<"\n"; }
  133. // if (i==4) return 0;
  134. //if (i==6) return 0;
  135. }
  136.  
  137. for (int i=0;i<ans.size();i++) cout<<ans[i]<<"\n";
  138. /* --------- */
  139. return 0;
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement