Skip to content

Instantly share code, notes, and snippets.

@masyax
Created April 19, 2016 11:58
Show Gist options
  • Save masyax/65b177f579779677a0d14fda127347f0 to your computer and use it in GitHub Desktop.
Save masyax/65b177f579779677a0d14fda127347f0 to your computer and use it in GitHub Desktop.
#include <algorithm>
#include <cmath>
#include <climits>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#include <cassert>
#include <functional>
using namespace std;
#define LOG(...) printf(__VA_ARGS__)
//#define LOG(...)
#define FOR(i,a,b) for(int i=(int)(a);i<(int)(b);++i)
#define REP(i,n) for(int i=0;i<(int)(n);++i)
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())
#define RSORT(c) sort((c).rbegin(),(c).rend())
#define CLR(a) memset((a), 0 ,sizeof(a))
typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int dx[] = { -1, 0, 1, 0 }; const int dy[] = { 0, 1, 0, -1 };
struct UnionFind {
vector<int> v;
UnionFind(int n) : v(n) { for (int i = 0; i < n; i++) v[i] = i; }
int find(int x) { return v[x] == x ? x : v[x] = find(v[x]); }
void unite(int x, int y) { v[find(x)] = find(y); }
};
struct data{
int no;//識別子
int front;//データのはじめ
int size;//データのサイズ
int pre;//前のdataの場所
int next;//後のdataの場所
};
int main() {
int n;
while (cin>>n,n){
vector<data> file;
file.push_back({ -1, 0, 1000000001, -1, -1 });//空の代入
int first = 0;
REP(i,n){
int a, b;
char c;
int now = first;
cin >> c;
switch (c)
{
case 'W':
cin >> a >> b;
while(1){
if (file[now].no == -1){
if (file[now].size > b){
file.push_back({ a, file[now].front, b, file[now].pre, now });//空の部分があるときその前との間にねじ込む
file[now].front += b;//空の部分のアドレスを押し上げる
file[now].size -= b;//空の部分のサイズを減らす
if (file[now].pre == -1)
first = file.size() - 1;//もしデータの前に何もなかったら今のデータをはじめのデータとする
else
file[file[now].pre].next = file.size() - 1;//もしデータの前にデータがあったら前のデータの次を今のデータとする
file[now].pre = file.size() - 1;//空のデータの前を今のデータとする
break;
} else if(file[now].size==b){
file[now].no = a;//データのサイズが一緒だったら識別子変えるだけ
break;
}
else{//もし空のサイズより入れるデータのサイズが多かったら、識別子を更新し、はみ出した部分を繰り越す
file[now].no = a;
b -= file[now].size;
}
}
now = file[now].next;//次のデータ
}
break;
case 'D':
cin >> a;
REP(j, file.size()){
if (file[j].no == a)
file[j].no = -1;
}
break;
case 'R':
cin >> a;
while(1){
if (file[now].front <= a&&a <file[now].front+file[now].size){
cout << file[now].no << endl;
break;
}
else{
now = file[now].next;
}
}
break;
}
}
cout << endl;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment