Wednesday, August 6, 2014

Solution for Timus 1992. CVS


/*
 * 1992.cpp
 *
 *  Created on: Aug 5, 2014
 *      Author: ghooo
 */

#include <cstring>
#include <map>
#include <deque>
#include <queue>
#include <stack>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <algorithm>
#include <vector>
#include <set>
#include <complex>
#include <list>
#include <climits>
#include <cctype>
#include <bitset>
//#include <windows.h>

using namespace std;

#define pb push_back
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define sz(v) ((ll)v.size())
#define rep(i,m) for(int i=0;i<(int)(m);++i)
#define repe(i,n,m) for(int i=n;i<=(int)(m);++i)
#define rep2(i,n,m) for(int i=n;i<(int)(m);i++)
#define For(it,c) for(__typeof(c.begin()) it=c.begin();it!=c.end();++it)
#define reset(a,b) memset(a,b,sizeof(a))
#define mp make_pair
#define dot(a,b) ((conj(a)*(b)).X)
#define X real()
#define Y imag()
#define length(V) (hypot((V).X,(V).Y))
#define vect(a,b) ((b)-(a))
#define cross(a,b) ((conj(a)*(b)).imag())
#define normalize(v) ((v)/length(v))
#define rotate(p,about,theta) ((p-about)*exp(point(0,theta))+about)
#define pointEqu(a,b) (comp(a.X,b.X)==0 && comp(a.Y,b.Y)==0)
#define clrq(x) while(!x.empty()) x.pop();
#define clrvv(v) rep(i,sz(v))v[i].clear();
#define debug(x) cerr << #x << ": " << x << endl;
#define debugv(v) cerr << #v << ": ";For(it,v)cerr <<(*it)<<", "; cerr<<endl;

typedef stringstream ss;
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<pii> vpii;
typedef vector<string> vs;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<vector<int> > vii;
typedef long double ld;
typedef complex<double> point;
typedef pair<point, point> segment;
typedef pair<double, point> circle;
typedef vector<point> polygon;
typedef unsigned long long ull;
typedef long long ll;

//==============================================================
// handling triples
typedef pair<ll,pair<ll,ll> > triple;
#define tfirst first
#define tsecond second.first
#define tthird second.second
#define mt(x,y,z) mp(x,mp(y,z))
//---------------------------------------------------------------

const int oo = (int) 1e9;
const double PI = 2 * acos(0.0);
const double eps = 1e-9;
vector<int> cloneProgram, cloneCancel;
vector<int> programHistory, programHistoryPrev;
vector<int> cancelHistory, cancelHistoryPrev;
void learn(int cl, int prog){
 programHistory.pb(prog);
 programHistoryPrev.pb(cloneProgram[cl]);
 cloneProgram[cl] = sz(programHistory)-1;
 cloneCancel[cl] = -1;
}
void rollback(int cl){
 cancelHistory.pb(cloneProgram[cl]);
 cancelHistoryPrev.pb(cloneCancel[cl]);
 cloneCancel[cl] = sz(cancelHistory)-1;

 cloneProgram[cl] = programHistoryPrev[cloneProgram[cl]];
}
void relearn(int cl){
 int lastProgram = cancelHistory[cloneCancel[cl]];

 cloneProgram[cl] = lastProgram;

 cloneCancel[cl] = cancelHistoryPrev[cloneCancel[cl]];

}
void clone(int cl){
 cloneProgram.pb(cloneProgram[cl]);
 cloneCancel.pb(cloneCancel[cl]);
}
void check(int cl){
 if(cloneProgram[cl] == -1) cout << "basic" << endl;
 else {
  cout << programHistory[cloneProgram[cl]] << endl;
 }
}
void init(){
 cloneProgram.clear();
 cloneProgram.pb(-1);
 cloneProgram.pb(-1);
 cloneCancel.clear();
 cloneCancel.pb(-1);
 cloneCancel.pb(-1);
 programHistory.clear();
 programHistoryPrev.clear();
 cancelHistory.clear();
 cancelHistoryPrev.clear();
}
int main(){
 ios_base::sync_with_stdio(false);
 cin.tie(NULL);
 cout.tie(NULL);
#ifndef ONLINE_JUDGE
 freopen("in.txt", "r", stdin);
 //freopen("out.txt", "w", stdout);
#endif
 int n, m;
 cin >> n >> m;
 init();
 string s;
 int c, p;
 rep(i,n){
  cin >> s;
  if(s == "learn"){
   cin >> c >> p;
   learn(c,p);
  } else if(s == "rollback"){
   cin >> c;
   rollback(c);
  } else if(s == "relearn"){
   cin >> c;
   relearn(c);
  } else if(s == "clone"){
   cin >> c;
   clone(c);
  } else if(s == "check"){
   cin >> c;
   check(c);
  }
//  debugv(cloneProgram);
//  debugv(cloneCancel);
//  debugv(programHistory);
//  debugv(pPrev);
//  debugv(cancelHistory);
//  debugv(cPrev);
 }
 return 0;
}

No comments:

Post a Comment