import java.util.*; import java.io.*; public class whiteboard_font { public static void main(String[] args) throws Exception { PrintWriter out = new PrintWriter(System.out); new whiteboard_font(new FastScanner(System.in), out); out.close(); } int W, H, N; int[] dx = {-1, 0, 1, 0}; int[] dy = {0, 1, 0, -1}; boolean[] targetColor; long[] lastTouched; ArrayDeque[] events; int getDir(String s) { switch (s) { case "left": return 0; case "down": return 1; case "right": return 2; case "up": return 3; } System.out.printf("BADNESS INVALID DIRECTION (%s)%n", s); return -1; } TreeSet hull; void updateTable(int x, int y, int i, int dir) { // Process all add events for (Event e : events[x+W*y]) if (e.dir == dir) hull.add(new Node(e.t, i)); // Update the table if (hull.size() > 0) lastTouched[x+W*y] = Math.max(lastTouched[x+W*y], hull.last().getTimeAt(i)); // Process all remove events for (Event e : events[x+W*y]) if (e.dir == -dir-1) hull.remove(new Node(e.t, -1)); } public whiteboard_font(FastScanner in, PrintWriter out) { H = in.nextInt(); W = in.nextInt(); N = in.nextInt(); targetColor = new boolean[W*H]; for (int j=0; j(); int x = 0, y = H-1; long timer = 1; while (N-->0) { int dir = getDir(in.next()); int len = in.nextInt(); events[x+W*y].add(new Event(dir, timer)); x += dx[dir] * len; y += dy[dir] * len; events[x+W*y].add(new Event(-dir-1, timer)); timer += len; } // Update the table hull = new TreeSet<>(); // LEFT (update last touched) for (int j=0; j=0; i--) updateTable(i, j, i, 0); // DOWN (update last touched) for (int i=0; i=0; j--) updateTable(i, j, j, 3); // Check unreachable mark for (int i=0; i { long startTime; int startLoc; Node(long st, int loc) { startTime = st; startLoc = loc; } long getTimeAt(int loc) { int delta = Math.abs(loc-startLoc); return startTime + delta; } public int compareTo(Node rhs) { return Long.compare(startTime, rhs.startTime); } } class Event { int dir; long t; Event(int dd, long tt) { dir=dd; t=tt; } } class FastScanner{ private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; public FastScanner(InputStream stream) { this.stream = stream; } int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars){ curChar = 0; try{ numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } boolean isSpaceChar(int c) { return c==' '||c=='\n'||c=='\r'||c=='\t'||c==-1; } boolean isEndline(int c) { return c=='\n'||c=='\r'||c==-1; } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String next(){ int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do{ res.appendCodePoint(c); c = read(); }while(!isSpaceChar(c)); return res.toString(); } String nextLine(){ int c = read(); while (isEndline(c)) c = read(); StringBuilder res = new StringBuilder(); do{ res.appendCodePoint(c); c = read(); }while(!isEndline(c)); return res.toString(); } }