Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <set>
- #include <map>
- #include <algorithm>
- #include <iomanip>
- #include <cmath>
- #include <queue>
- #pragma GCC optimize("O3")
- const long long mod1=998244353;
- const long long mod2=1000000007;
- long long pow(int a,int b) {
- if (b==0 || a==1) return 1;
- if (b%2==0) { long long k=pow(a,b/2); return (k*k); }
- else { long long k=pow(a,b/2); return k*k*a; }
- }
- long long powmod(long long a,long long b,long long mod) {
- if (b==0 || a==1) {if (mod==1) return 0; else return 1; }
- if (b%2==0) { long long k=powmod(a,b/2,mod); return (k*k)%mod; }
- else {long long k=powmod(a,b/2,mod); return ( (k*k) %mod *a)% mod; }
- }
- long long gcd(long long a, long long b) {
- if (a==0) return b;
- if (b==0) return a;
- if (a>b) return gcd(a%b,b); else return gcd(b%a,a);
- }
- int prime(int p) { // 1 - простое
- for (int i=2;i*i<=p;i++) {
- if (p%i==0 && i<p) return 0;
- }
- return 1;
- }
- long long sqr(long long i) {
- return i*i;
- }
- using namespace std;
- int main() {
- ios_base::sync_with_stdio(0);
- cin.tie(0);
- cout.tie(0);
- /* --------- */
- int a;
- cin>>a;
- vector <int> ans;
- vector <pair<int,pair<int,int>>> cur;
- int ser11=-1,ser21=-1;
- int ser12=0,ser22=0;
- int last1=0;
- int last2=0;
- int first1=0;
- int first2=0;
- int t=0;
- int pushes=0;
- for (int i=0;i<a;i++) {
- char q;
- cin>>q;
- if (q=='-') { ans.push_back(cur[first2].first);
- // if (i==5) { cout<<t; return 0; }
- if (t==1) {first1=-1; last1=-1; first2=-1; last2=-1; }
- else {
- // if (t==2) cout<<first2<<" "<<last1;
- //cout<<first2<<" ";
- first2=cur[first2].second.second;
- // cout<<first2<<"\n";
- first1=cur[first2].first;
- if (t%2==0) {
- ser11=ser21;
- ser12=ser22;
- ser21=cur[ser21].second.second;
- ser22=cur[ser21].first;
- }
- }
- t--;
- } else {
- if (q=='+') {int w; cin>>w;
- if (t==0) { first1=w; first2=pushes; last1=pushes; last2=w; t++;
- cur.push_back({w, {last1, -1}}); pushes++;} else {
- cur[last1].second.second=pushes;
- // cout<<last1;
- cur.push_back({w, {last1, -1}});
- last1=pushes;
- last2=w;
- t++;
- if (t==2) {ser11=first2; ser21=last1; ser12=cur[ser11].first; ser22=cur[ser21].first; }
- pushes++;
- if (t%2==1) {ser11=ser21; ser12=ser22;
- ser21=cur[ser21].second.second;
- ser22=cur[ser21].first;
- }
- }
- }
- else { int w; cin>>w;
- if (t==0) { cur.push_back({w, {-1, -1}}); first1=w; first2=pushes; last1=pushes; last2=w; pushes++; t++; } else
- 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
- if (t%2==0) {
- cur.push_back({w, {ser11,ser21}});
- cur[ser11].second.second=pushes;
- cur[ser21].second.first=pushes;
- ser11=pushes;
- ser12=w; pushes++; t++;
- } else {
- cur.push_back({w, {ser11,ser21}});
- cur[ser11].second.second=pushes;
- cur[ser21].second.first=pushes;
- ser21=pushes;
- ser22=w;
- pushes++;
- t++;
- }
- }
- }
- //if (i==2) {cout<<first2<<" "<<last1<<"\n"; }
- // if (i==4) return 0;
- //if (i==6) return 0;
- }
- for (int i=0;i<ans.size();i++) cout<<ans[i]<<"\n";
- /* --------- */
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement