Problem:
MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }});

Problem H
Kimagure Cleaner

Ichiro won the newest model cleaner as a prize of a programming contest. This cleaner automatically moves around in a house for cleaning. Because Ichiro's house is very large, it can be modeled as an infinite two-dimensional Cartesian plane, whose axes are called $X$ and $Y$. The positive direction of the $Y$-axis is to the left if you face the positive direction of the $X$-axis.

The cleaner performs a sequence of actions for cleaning. Each action consists of a turn and a run. In an action, first the cleaner turns right or left by 90 degrees, and then runs straight by an integer length to the direction that the cleaner faces. At the end of a day, the cleaner reports the log of its actions in the day to Ichiro, in order to inform him where it has cleaned and where it hasn't.

Unlike common cleaners, this cleaner has human-like artificial intelligence. Therefore, the cleaner is very forgetful (like humans) and it is possible that the cleaner forgets the direction of a turn, or the cleaner only remembers the length of a run as a very rough range. However, in order to pretend to be operating correctly, the cleaner has to recover the complete log of actions after finishing the cleaning.

The cleaner was initially at the point $(0, 0)$, facing the positive direction of $X$-axis. You are given the cleaner's location after cleaning, $(X, Y)$, and an incomplete log of the cleaner's actions that the cleaner remembered. Please recover a complete log from the given incomplete log. The actions in the recovered log must satisfy the following constraints:

The number of actions must be the same as that in the incomplete log.

The direction of the $i$-th turn must be the same as that in the incomplete log if it is recorded in the incomplete log.

The length of the $i$-th run must be within the range of the length specified in the incomplete log.

The cleaner must be at $(X, Y)$ after finishing all actions. The direction of the cleaner after cleaning is not important and you do not have to care about it, because the cleaner can turn freely after cleaning, though it cannot run after cleaning. You are not required to recover the actual path, because Ichiro only checks the format of the log and the location of the cleaner after cleaning.

Input

The input consists of a single test case. The first line contains three integers $N$ $(1 \leq N \leq 50)$, $X$, and $Y$ $(-10^9 \leq X, Y \leq 10^9)$. $N$ represents the number of actions in the incomplete log. $X$ and $Y$ represent the cleaner's location $(X, Y)$ after cleaning. The $i$-th line of the following $N$ lines contains a character $D_i$ and two integers $LL_i$ and $LU_i$. $D_i$ represents the direction of the $i$-th turn: 'L', 'R', and '?' represents left, right, and not recorded respectively. $LL_i$ and $LU_i$ represent a lower and upper bound of the length of the $i$-th run, respectively. You can assume $1 \leq LL_i \leq LU_i \leq 55,555,555$.

Output

Display the recovered log. In the first line, display N, the number of actions in the log. In the $i$-th line of the following $N$ lines, display the direction of the $i$-th turn in a character and the length of the $i$-th run separated by a single space. Represent a right turn by a single character 'R', and a left turn by a single character 'L'. The recovered log must satisfy the constraints in the problem. If there are multiple
logs that satisfy the constraints, you can display any of them. Display $-1$ in a line if there is no log that satisfies the constraints.

Sample Input 1

2 -3 4
L 2 5
? 3 5

Output for the Sample Input 1

2
L 4
L 3

Sample Input 2

5 3 -4
? 1 5
? 1 5
? 1 5
? 1 5
? 1 5

Output for the Sample Input 2

5
L 1
R 2
R 4
L 1
R 1

Solution in JAVA (judge verdict: Runtime Error)

import java.io.IOException;
import java.io.InputStream;
import java.util.Comparator;
import java.util.HashSet;
import java.util.PriorityQueue;

public class Main {
	static boolean debug = false;

	public static void main(String[] args) throws IOException {

		UserScanner scan = new UserScanner(System.in);
		// PrintWriter pwriter = new PrintWriter(System.out);
		do {
			int n = scan.nextInt();
			int x = scan.nextInt();
			int y = scan.nextInt();
			Cleaner cl = new Cleaner(n, debug);
			for (int i = 0; i < n; i++) {
				cl.abbLog(i, scan.next(), scan.nextInt(), scan.nextInt());
			}
			cl.printAnser(x, y);
		} while (debug);

		scan.close();
		System.exit(0);
	}
}

class Cleaner {
	boolean debug;

	class Log {
		boolean fixed;
		char d;
		int ll;
		int lu;

		int l;
		int dw;

		public Log(char d, int ll, int lu) {
			this.d = d;
			this.ll = ll;
			this.lu = lu;
			if (d == 'L' || d == 'R')
				fixed = true;
			else {
				fixed = false;
				this.d = 'L';
			}
		}
	}

	class Move {
		int tuxrn;
		int xl, xu;
		int yl, yu;

		public Move(int xwl, int xwu, int ywl, int ywu) {
			this.xl = xwl;
			this.xu = xwu;
			this.yl = ywl;
			this.yu = ywu;
		}
	}

	Log[] log;

	public Cleaner(int n, boolean debug) {
		this.debug = debug;
		log = new Log[n];
	}

	public void printAnser(int x, int y) {
		int cnt = 0;
		for (int i = 0; i < log.length; i++)
			if (!log[i].fixed)
				cnt++;
		cnt++;
		Move[] mv = new Move[cnt];
		initLogDirection(mv);

		if (!fixLogDirection(mv, x, y)) {
			System.out.println("-1");
			return;
		} else
			System.out.println(log.length);

		if (debug)
			for (int i = 0; i < log.length; i++)
				System.out.println("debug\tDirection=" + log[i].d);

		fixLength(x, y);

		for (int i = 0; i < log.length; i++)
			System.out.println(log[i].d + " " + log[i].l);
	}

	private void fixLength(int x, int y) {

		int dw = 0;
		for (int i = 0; i < log.length; i++) {
			if (log[i].d == 'L')
				dw = (dw + 1) % 4;
			else
				dw = (dw + 3) % 4;
			log[i].dw = dw;
		}

		int xw = 0, yw = 0;
		for (int i = 0; i < log.length; i++)
			switch (log[i].dw) {
			case 0:
				if (x > 0)
					log[i].l = log[i].lu;
				else
					log[i].l = log[i].ll;
				xw += log[i].l;
				break;
			case 1:
				if (y > 0)
					log[i].l = log[i].lu;
				else
					log[i].l = log[i].ll;
				yw += log[i].l;
				break;
			case 2:
				if (x > 0)
					log[i].l = log[i].ll;
				else
					log[i].l = log[i].lu;
				xw -= log[i].l;
				break;
			case 3:
				if (y > 0)
					log[i].l = log[i].ll;
				else
					log[i].l = log[i].lu;
				yw -= log[i].l;
				break;
			}

		if (debug)
			System.out.println("debug\tTotal phase-1 x=" + xw + "\ty=" + yw);

		xw = Math.abs(xw - x);
		yw = Math.abs(yw - y);
		int adj;

		for (int i = 0; i < log.length; i++)
			if (log[i].dw % 2 == 0) {
				if (xw > 0) {
					adj = Math.min(xw, Math.abs(log[i].lu - log[i].ll));
					xw -= adj;
					if (log[i].l == log[i].ll)
						log[i].l += adj;
					else
						log[i].l -= adj;
				}
			} else {
				if (yw > 0) {
					adj = Math.min(yw, Math.abs(log[i].lu - log[i].ll));
					yw -= adj;
					if (log[i].l == log[i].ll)
						log[i].l += adj;
					else
						log[i].l -= adj;
				}
			}

		if (debug)
			System.out.println("debug\tTotal phase-2 x=" + xw + "\ty=" + yw);

	}

	class DirectionPatt {
		int differ;
		long patt;

		public DirectionPatt(int df, long pt) {
			this.differ = df;
			this.patt = pt;
		}
	}

	public class compDir implements Comparator<DirectionPatt> {

		@Override
		public int compare(DirectionPatt o1, DirectionPatt o2) {
			if (o1.differ > o2.differ)
				return 1;
			else if (o1.differ == o2.differ)
				return 0;
			else
				return -1;
		}

	}

	private boolean fixLogDirection(Move[] mv, int x, int y) {
		// set pattern to long code
		// same with initial=1 not same=0 and first ? is set to bit 1, second ?
		// is set to bit 2
		// all ? is set to L in initial status
		//
		long patt = 0;
		for (int i = 1; i < mv.length; i++) {
			patt <<= 1;
			patt++;
		}

		PriorityQueue<DirectionPatt> pqDir = new PriorityQueue<DirectionPatt>(new compDir());
		HashSet<Long> hsDir = new HashSet<Long>();

		hsDir.add(patt);
		pqDir.add(new DirectionPatt(getDiffer(patt, mv, x, y), patt));
		patt = searchDirection(pqDir, hsDir, mv, x, y);
		if (debug)
			System.out.println("debug\tSelected pattern=" + patt);
		if (patt == -1)
			return false;

		int tmp = 0;
		for (int i = 0; i < log.length; i++)
			if (!log[i].fixed) {
				if ((tmp ^ (patt & 1)) == 0) {
					log[i].d = 'R';
					tmp ^= 1;
				}
				patt >>= 1;
			}
		return true;
	}

	private int getDiffer(long patt, Move[] mv, int x, int y) {
		// get possible range
		int xwl = mv[0].xl;
		int xwu = mv[0].xu;
		int ywl = mv[0].yl;
		int ywu = mv[0].yu;

		for (int i = 1; i < mv.length; i++, patt >>= 1)
			if ((patt & 1) != 0) {
				xwl += mv[i].xl;
				xwu += mv[i].xu;
				ywl += mv[i].yl;
				ywu += mv[i].yu;
			} else {
				xwl -= mv[i].xu;
				xwu -= mv[i].xl;
				ywl -= mv[i].yu;
				ywu -= mv[i].yl;
			}

		int r = 0;

		if (x < xwl)
			r += (xwl - x);
		else if (x > xwu)
			r += (x - xwu);

		if (y < ywl)
			r += (ywl - y);
		else if (y > ywu)
			r += (y - ywu);

		return r;
	}

	private long searchDirection(PriorityQueue<DirectionPatt> pqDir, HashSet<Long> hsDir, Move[] mv, int x, int y) {
		if (pqDir.isEmpty())
			return -1;
		DirectionPatt dp = pqDir.poll();
		if (dp.differ == 0)
			return dp.patt;

		if (debug)
			System.out.println("debug\tdiffer=" + dp.differ + "\tpattern=" + dp.patt);

		long mask = 1;
		for (int i = 1; i < mv.length; i++, mask <<= 1) {

			long newPatt = (dp.patt ^ mask);
			if (hsDir.contains(newPatt))
				continue;

			hsDir.add(newPatt);
			pqDir.add(new DirectionPatt(getDiffer(newPatt, mv, x, y), newPatt));
		}

		return searchDirection(pqDir, hsDir, mv, x, y);
	}

	private void initLogDirection(Move[] mv) {
		int xwl = 0, xwu = 0;
		int ywl = 0, ywu = 0;
		int dw = 0;
		int cnt = 0;
		for (int i = 0; i < log.length; i++) {
			if (!log[i].fixed) {
				mv[cnt++] = new Move(xwl, xwu, ywl, ywu);
				xwl = 0;
				xwu = 0;
				ywl = 0;
				ywu = 0;
			}
			if (log[i].d == 'L')
				dw = (dw + 1) % 4;
			else
				dw = (dw + 3) % 4;
			switch (dw) {
			case 0:
				xwl += log[i].ll;
				xwu += log[i].lu;
				break;
			case 1:
				ywl += log[i].ll;
				ywu += log[i].lu;
				break;
			case 2:
				xwl -= log[i].lu;
				xwu -= log[i].ll;
				break;
			case 3:
				ywl -= log[i].lu;
				ywu -= log[i].ll;
				break;
			}
		}

		mv[cnt++] = new Move(xwl, xwu, ywl, ywu);

		if (debug)
			for (int i = 0; i < mv.length; i++)
				System.out.println("debug\tturn=" + i + "\txl=" + mv[i].xl + "\txu=" + mv[i].xu + "\tyl=" + mv[i].yl
						+ "\tyu=" + mv[i].yu);
	}

	public void abbLog(int i, String d, int ll, int lu) {
		log[i] = new Log(d.charAt(0), ll, lu);
	}
}

class UserScanner {
	private InputStream in;
	private final byte[] buffer = new byte[1024];
	private int ptr = 0;
	private int buflen = 0;

	public UserScanner(InputStream inStream) {
		in = inStream;
	}

	private void read() {
		ptr = 0;
		try {
			buflen = in.read(buffer);
		} catch (IOException e) {
			e.printStackTrace();
			System.exit(9);
		}
	}

	private byte getByte() {
		if (ptr >= buflen)
			read();
		if (buflen < 0 || isCtlSpace(buffer[ptr])) {
			return -1;
		} else
			return buffer[ptr++];
	}

	private void skipCtlSpace() {
		for (; ptr < buflen; ptr++)
			if (!isCtlSpace(buffer[ptr]))
				return;
		read();
		skipCtlSpace();
	}

	private boolean isCtlSpace(byte b) {
		if (b <= ' ' || b > '~')
			return true;
		else
			return false;
	}

	public void close() {
		try {
			in.close();
		} catch (IOException e) {
			e.printStackTrace();
			System.exit(9);
		}
	}

	public String next() {
		skipCtlSpace();
		StringBuilder sb = new StringBuilder();
		byte b;
		while ((b = getByte()) != -1) {
			sb.appendCodePoint(b);
		}
		return sb.toString();
	}

	public long nextLong() {
		skipCtlSpace();
		long n = 0;
		boolean minus = false;
		byte b;
		while ((b = getByte()) != -1) {
			if (b == '-')
				minus = true;
			else {
				n *= 10d;
				n += (long) (b - '0');
			}
		}
		if (minus)
			return n * -1;
		else
			return n;
	}

	public int nextInt() {
		skipCtlSpace();
		int n = 0;
		boolean minus = false;
		byte b;
		while ((b = getByte()) != -1) {
			if (b == '-')
				minus = true;
			else {
				n *= 10;
				n += (b - '0');
			}
		}
		if (minus)
			return n * -1;
		else
			return n;
	}
}