Submit Info #29711

Problem Lang User Status Time Memory
Persistent Queue java 37zigen AC 425 ms 76.98 MiB

ケース詳細
Name Status Time Memory
ephemeral_00 AC 280 ms 59.28 MiB
example_00 AC 51 ms 9.12 MiB
half_rot_killer2_00 AC 162 ms 44.86 MiB
half_rot_killer_00 AC 389 ms 73.21 MiB
random_00 AC 363 ms 63.07 MiB
random_01 AC 425 ms 71.08 MiB
random_02 AC 139 ms 16.97 MiB
random_2_00 AC 369 ms 67.73 MiB
random_2_01 AC 388 ms 76.98 MiB
random_2_02 AC 142 ms 16.34 MiB
random_3_00 AC 239 ms 45.95 MiB
random_3_01 AC 265 ms 52.34 MiB
random_3_02 AC 118 ms 14.56 MiB
two_stacks_killer_00 AC 313 ms 60.09 MiB

import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.NoSuchElementException; class FastScanner { private final InputStream in = System.in; private final byte[] buffer = new byte[1024]; private int ptr = 0; private int buflen = 0; private boolean hasNextByte() { if (ptr < buflen) { return true; }else{ ptr = 0; try { buflen = in.read(buffer); } catch (IOException e) { e.printStackTrace(); } if (buflen <= 0) { return false; } } return true; } private int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1;} private static boolean isPrintableChar(int c) { return 33 <= c && c <= 126;} private void skipUnprintable() { while(hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++;} public boolean hasNext() { skipUnprintable(); return hasNextByte();} public String next() { if (!hasNext()) throw new NoSuchElementException(); StringBuilder sb = new StringBuilder(); int b = readByte(); while(isPrintableChar(b)) { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } public long nextLong() { if (!hasNext()) throw new NoSuchElementException(); long n = 0; boolean minus = false; int b = readByte(); if (b == '-') { minus = true; b = readByte(); } if (b < '0' || '9' < b) { throw new NumberFormatException(); } while(true){ if ('0' <= b && b <= '9') { n *= 10; n += b - '0'; }else if(b == -1 || !isPrintableChar(b)){ return minus ? -n : n; }else{ throw new NumberFormatException(); } b = readByte(); } } int nextInt() { return (int)nextLong(); } } class PersistentStack { PersistentStack next=null; int val; public PersistentStack(int val, PersistentStack next) { this.val=val; this.next=next; } public PersistentStack() { } int peek() { return val; } PersistentStack pop() { if (next!=null) next=next.eval(); return next; } static PersistentStack push(PersistentStack x, int v) { return new PersistentStack(v, x); } static PersistentStack concat(PersistentStack x, PersistentStack y) { if (x==null) return y.eval(); else return new PersistentStack(x.val, new PersistentStack() { @Override PersistentStack eval() { return PersistentStack.concat(x.pop(), y); } }); } static PersistentStack reverse(PersistentStack head) { return new PersistentStack () { @Override PersistentStack eval() { PersistentStack ret=null; for (PersistentStack x=head;x!=null;x=x.pop()) { ret=PersistentStack.push(ret, x.peek()); } return ret; } }; } PersistentStack eval() { return this; } } class PersistentQueue { int front_size=0; int back_size=0; PersistentStack front=null; PersistentStack back=null; public PersistentQueue(PersistentStack front, int front_size, PersistentStack back, int back_size) { this.front_size=front_size; this.back_size=back_size; this.front=front; this.back=back; } public PersistentQueue() { } boolean isEmpty() { return front_size==0; } int peek() { return front.peek(); } PersistentQueue push(int x) { return new PersistentQueue(front, front_size, PersistentStack.push(back, x), back_size+1).normalize(); } PersistentQueue pop() { return new PersistentQueue(front.pop(), front_size-1, back, back_size).normalize(); } PersistentQueue normalize() { if (front_size >= back_size) return this; else { return new PersistentQueue(PersistentStack.concat(front, PersistentStack.reverse(back)), front_size+back_size, null, 0); } } } public class Main { public void run() { FastScanner sc=new FastScanner(); int Q=sc.nextInt(); PersistentQueue[] queue=new PersistentQueue[Q+1]; queue[0]=new PersistentQueue(); PrintWriter pw=new PrintWriter(System.out); for (int q=0;q<Q;++q) { int type=sc.nextInt(); int t=sc.nextInt()+1; if (type==0) { int x=sc.nextInt(); queue[q+1]=queue[t].push(x); } else { pw.println(queue[t].peek()); queue[q+1]=queue[t].pop(); } } pw.close(); } static void tr(Object...objects) {System.out.println(Arrays.deepToString(objects));} public static void main(String[] args) { new Main().run(); } }