File tree Expand file tree Collapse file tree 1 file changed +54
-0
lines changed
Expand file tree Collapse file tree 1 file changed +54
-0
lines changed Original file line number Diff line number Diff line change 1+ ```
2+ import java.io.*;
3+ import java.util.Objects;
4+ import java.util.StringTokenizer;
5+
6+ public class Main {
7+ private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
8+ private static final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
9+ private static Node[] history;
10+ private static int N;
11+
12+ public static void main(String[] args) throws IOException {
13+ init();
14+
15+ for (int i = 1; i <= N; i++) {
16+ StringTokenizer st = new StringTokenizer(br.readLine());
17+ char q = st.nextToken().charAt(0);
18+
19+ if (q == 'a') {
20+ int val = Integer.parseInt(st.nextToken());
21+ history[i] = new Node(val, history[i-1]);
22+ } else if (q == 's') {
23+ history[i] = history[i-1].prev;
24+ } else {
25+ int time = Integer.parseInt(st.nextToken());
26+ history[i] = history[time-1];
27+ }
28+
29+ if (Objects.isNull(history[i])) bw.write("-1" + "\n");
30+ else bw.write(history[i].val + "\n");
31+ }
32+ bw.flush();
33+ bw.close();
34+ br.close();
35+ }
36+
37+ private static void init() throws IOException {
38+ N = Integer.parseInt(br.readLine());
39+ history = new Node[N + 1];
40+
41+ history[0] = null;
42+ }
43+
44+ static class Node {
45+ int val;
46+ Node prev;
47+
48+ public Node(int val, Node prev) {
49+ this.val = val;
50+ this.prev = prev;
51+ }
52+ }
53+ }
54+ ```
You can’t perform that action at this time.
0 commit comments