Guessing Game

Time limit2sMemory limit512 MB

Summary
Given difference constraints of the form a_i + b_j <= c or >= c, decide whether integer lists a and b satisfying all of them exist.
Level

Medium7 of 10

Topics
Graph, Shortest path, Union-find, Implementation
Solved
No attempts yet

Problem

Jaehyun has two integer lists a1,…,aNa_1, \dots, a_N and b1,…,bMb_1, \dots, b_M. Jeffrey wants to know these numbers, but Jaehyun won't reveal them directly. So Jeffrey asks a series of questions of the form "How big is ai+bja_i + b_j?" Even then Jaehyun refuses to give the exact value; he answers only "It's at least cc" (meaning ai+bj≥ca_i + b_j \ge c) or "It's at most cc" (meaning ai+bj≤ca_i + b_j \le c). After collecting every answer, Jeffrey still cannot reconstruct any consistent numbers no matter how hard he tries, and starts to suspect that Jaehyun lied on some of the answers. Given Jaehyun's answers, determine whether integer lists consistent with all of them exist, or whether it can be proven that Jaehyun definitely lied.

Input

The input contains multiple test cases. Each test case begins with a line of three positive integers NN, MM, and QQ — the lengths of Jaehyun's two lists and the number of questions Jeffrey asked — satisfying 2≤N+M≤10002 \le N + M \le 1000 and 1≤Q≤100001 \le Q \le 10000. Each of the next QQ lines has the form i j <= c or i j >= c: the former means ai+bj≤ca_i + b_j \le c and the latter means ai+bj≥ca_i + b_j \ge c, where −1000≤c≤1000-1000 \le c \le 1000. The input ends with a line 0 0 0, which is not processed.

Output

For each test case, print a single line: Possible if there exist integers a1,…,aNa_1, \dots, a_N and b1,…,bMb_1, \dots, b_M consistent with all of Jaehyun's answers, or Impossible if the answers can be proven contradictory (that is, Jaehyun definitely lied).

Examples4

  1. Example 1

    Input
    2 1 3
    1 1 <= 3
    2 1 <= 5
    1 1 >= 4
    2 2 4
    1 1 <= 3
    2 1 <= 4
    1 2 >= 5
    2 2 >= 7
    0 0 0
    
    Expected output
    Impossible
    Possible
    
  2. Example 2

    Input
    1 1 1
    1 1 <= 5
    0 0 0
    
    Expected output
    Possible
    
  3. Example 3

    Input
    1 1 2
    1 1 <= 2
    1 1 >= 3
    0 0 0
    
    Expected output
    Impossible
    
  4. Example 4

    Input
    1 1 2
    1 1 <= 5
    1 1 >= 5
    0 0 0
    
    Expected output
    Possible