Problem:
Score : 500 points

Problem Statement

There is an infinitely long street that runs west to east, which we consider as a number line.

There are N roadworks scheduled on this street.
The i-th roadwork blocks the point at coordinate X_i from time S_i - 0.5 to time T_i - 0.5.

Q people are standing at coordinate 0. The i-th person will start the coordinate 0 at time D_i, continue to walk with speed 1 in the positive direction and stop walking when reaching a blocked point.

Find the distance each of the Q people will walk.

Constraints

All values in input are integers.

1 \leq N, Q \leq 2 \times 10^5

0 \leq S_i < T_i \leq 10^9

1 \leq X_i \leq 10^9

0 \leq D_1 < D_2 < ... < D_Q \leq 10^9

If i \neq j and X_i = X_j, the intervals [S_i, T_i) and [S_j, T_j) do not overlap.

Input

Input is given from Standard Input in the following format:

N Q
S_1 T_1 X_1
:
S_N T_N X_N
D_1
:
D_Q

Output

Print Q lines. The i-th line should contain the distance the i-th person will walk or -1 if that person walks forever.

Sample Input 1

4 6
1 3 2
7 13 10
18 20 13
3 4 2
0
1
2
3
5
8

Sample Output 1

2
2
10
-1
13
-1

The first person starts coordinate 0 at time 0 and stops walking at coordinate 2 when reaching a point blocked by the first roadwork at time 2.

The second person starts coordinate 0 at time 1 and reaches coordinate 2 at time 3. The first roadwork has ended, but the fourth roadwork has begun, so this person also stops walking at coordinate 2.

The fourth and sixth persons encounter no roadworks while walking, so they walk forever. The output for these cases is -1.

Solution in Go (1.6) (judge verdict: Accepted)

package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
	"strconv"
	"strings"
)

type Io struct {
	reader    *bufio.Reader
	writer    *bufio.Writer
	tokens    []string
	nextToken int
}

func NewIo() *Io {
	return &Io{
		reader: bufio.NewReader(os.Stdin),
		writer: bufio.NewWriter(os.Stdout),
	}
}

func (io *Io) Flush() {
	err := io.writer.Flush()
	if err != nil {
		panic(err)
	}
}

func (io *Io) NextLine() string {
	var buffer []byte
	for {
		line, isPrefix, err := io.reader.ReadLine()
		if err != nil {
			panic(err)
		}
		buffer = append(buffer, line...)
		if !isPrefix {
			break
		}
	}
	return string(buffer)
}

func (io *Io) Next() string {
	for io.nextToken >= len(io.tokens) {
		line := io.NextLine()
		io.tokens = strings.Fields(line)
		io.nextToken = 0
	}
	r := io.tokens[io.nextToken]
	io.nextToken++
	return r
}

func (io *Io) NextInt() int {
	i, err := strconv.Atoi(io.Next())
	if err != nil {
		panic(err)
	}
	return i
}

func (io *Io) NextInt64() int64 {
	i, err := strconv.ParseInt(io.Next(), 10, 64)
	if err != nil {
		panic(err)
	}
	return i
}


func (io *Io) NextFloat() float64 {
	i, err := strconv.ParseFloat(io.Next(), 64)
	if err != nil {
		panic(err)
	}
	return i
}

func (io *Io) PrintLn(a ...interface{}) {
	fmt.Fprintln(io.writer, a...)
}

func (io *Io) Printf(format string, a ...interface{}) {
	fmt.Fprintf(io.writer, format, a...)
}

func intMin(a, b int) int {
	if a < b {
		return a
	}
	return b
}

func (io *Io) PrintBool(b bool, trueS, falseS string) {
	if b {
		io.PrintLn(trueS)
	} else {
		io.PrintLn(falseS)
	}
}

func intMax(a, b int) int {
	if a > b {
		return a
	}
	return b
}

func absi(a int) int {
	if a > 0 {
		return a
	} else {
		return -a
	}
}

func b2i(b bool) int {
	if b {
		return 1
	}
	return 0
}

type Int64s []int64
func (f Int64s) Len() int {
	return len(f)
}
func (f Int64s) Less(i, j int) bool {
	return f[i] < f[j]
}

func (f Int64s) Swap(i, j int) {
	f[i], f[j] = f[j], f[i]
}

func abcInt(a int) int {
	if a > 0 {
		return a
	} else {
		return -a
	}
}

func gcdInt(a, b int) int {
	for a%b != 0 {
		a, b = b, a%b
	}
	return b
}

type avlNode struct {
	data ValType
	parent, left, right *avlNode
	height int
}

type AvlTree struct {
	root *avlNode
	size int
}

func getHeight(node*avlNode) int {
	if node == nil {
		return 0
	}
	return node.height
}

func getBalance(node *avlNode) int {
	ret := 0
	if node.left != nil {
		ret += node.left.height
	}
	if node.right != nil {
		ret -= node.right.height
	}
	return ret
}

func recalcHeight(node *avlNode) {
	node.height = 1 + Max(getHeight(node.left), getHeight(node.right))
}

func rotateRight(node*avlNode) *avlNode {
	newRoot := node.left
	oldRoot := node

	if newRoot.right != nil {
		newRoot.right.parent = oldRoot
	}
	oldRoot.left = newRoot.right
	newRoot.right = oldRoot
	newRoot.parent = oldRoot.parent
	oldRoot.parent = newRoot

	recalcHeight(oldRoot)
	recalcHeight(newRoot)

	return newRoot
}

func rotateLeft(node*avlNode) *avlNode {
	newRoot := node.right
	oldRoot := node

	if newRoot.left != nil {
		newRoot.left.parent = oldRoot
	}
	oldRoot.right = newRoot.left
	newRoot.left = oldRoot
	newRoot.parent = oldRoot.parent
	oldRoot.parent = newRoot

	recalcHeight(oldRoot)
	recalcHeight(newRoot)

	return newRoot
}

func insert(node *avlNode, val ValType) (*avlNode, bool) {
	if node == nil {
		return &avlNode{val, nil, nil, nil, 1}, true
	}

	switch val.Compare(node.data) {
	case EQUAL:
		//duplication is not allowed
		return node, false
	case LESS:
		var inserted bool
		node.left, inserted = insert(node.left, val)
		node.left.parent = node
		if !inserted {
			return node, false
		}
	case GREATER:
		var inserted bool
		node.right, inserted = insert(node.right, val)
		node.right.parent = node
		if !inserted {
			return node, false
		}
	}
	recalcHeight(node)

	balance := getBalance(node)

	if balance > 1 {
		switch val.Compare(node.left.data) {
		case EQUAL:
		case LESS:
			// LL
			return rotateRight(node), true
		case GREATER:
			// LR
			node.left = rotateLeft(node.left)
			return rotateRight(node), true
		}
	} else if balance < -1 {
		switch val.Compare(node.right.data) {
		case EQUAL:
		case LESS:
			// RL
			node.right = rotateRight(node.right)
			return rotateLeft(node), true
		case GREATER:
			// RR
			return rotateLeft(node), true
		}
	} else {
		// balanced
		return node, true
	}

	panic("unreachable")
}

func delete(node *avlNode, val ValType) (*avlNode, bool) {
	if node == nil {
		return node, false
	}

	switch val.Compare(node.data) {
	case EQUAL:
		if node.right == nil {
			return node.left, true
		} else if node.left == nil {
			return node.right, true
		} else {
			//find min
			swapped := node.right
			for swapped.left != nil {
				swapped = swapped.left
			}
			node.data = swapped.data
			node.right, _ = delete(node.right, node.data)
		}
	case LESS:
		var deleted bool
		node.left, deleted = delete(node.left, val)
		if !deleted {
			return node, deleted
		}
	case GREATER:
		var deleted bool
		node.right, deleted = delete(node.right, val)
		if !deleted {
			return node, deleted
		}
	}

	recalcHeight(node)
	balance := getBalance(node)

	if(balance > 1) {
		childBalance := getBalance(node.left)
		if (childBalance >= 0) {
			return rotateRight(node), true
		} else {
			node.left = rotateLeft(node.left)
			return rotateRight(node), true
		}
	} else if (balance < -1) {
		childBalance := getBalance(node.right)
		if (childBalance <= 0) {
			return rotateLeft(node), true
		} else {
			node.right = rotateRight(node.right)
			return rotateLeft(node), true
		}
	} else {
		return node, true
	}
}

func walkForToList(node *avlNode, ret *[]ValType) {
	if node == nil {
		return
	}
	walkForToList(node.left, ret)
	*ret = append(*ret, node.data)
	walkForToList(node.right, ret)
}

func NewAvlTree() *AvlTree {
	return &AvlTree{nil, 0}
}

func (avlTree *AvlTree) toList() []ValType {
	ret := make([]ValType, 0, avlTree.size)
	walkForToList(avlTree.root, &ret)
	return ret
}

func (avlTree *AvlTree) Insert(val ValType) {
	var inserted bool
	avlTree.root, inserted = insert(avlTree.root, val)
	if inserted {
		avlTree.size += 1
	}
}

func (avlTree *AvlTree) Delete(val ValType) {
	var deleted bool
	avlTree.root, deleted = delete(avlTree.root, val)
	if deleted {
		avlTree.size -= 1
	}
}

func (avlTree *AvlTree) Smallest() ValType {
	ret := avlTree.root
	for ret.left != nil {
		ret = ret.left
	}
	return ret.data
}

type CompareResult int
const (
	LESS CompareResult = iota - 1
	EQUAL
	GREATER
)

type Comparable interface {
	Compare(r interface{}) CompareResult
}

func ToCompareResult(diff int) CompareResult {
	switch {
	case diff < 0:
		return LESS
	case diff > 0:
		return GREATER
	default:
		return EQUAL
	}
}

type ValType Comparable

func Max(l, r int) int {
	if l > r {
		return l
	}
	return r
}

type ComparableInt int

func (p *ComparableInt) Compare(r interface{}) CompareResult {
	rval, _ := r.(*ComparableInt)
	return ToCompareResult(int(*p) - int(*rval))
}

var (
	io *Io
)

type Pii struct {
	f,s int
}

type Piis []Pii
func (f Piis) Len() int {
	return len(f)
}
func (f Piis) Less(i, j int) bool {
	if f[i].f != f[j].f {
		return f[i].f < f[j].f
	}
	return f[i].s < f[j].s
}

func (f Piis) Swap(i, j int) {
	f[i], f[j] = f[j], f[i]
}

func solve() {
	n,q := io.NextInt(), io.NextInt()
	var vp []Pii
	for i := 0; i < n; i++ {
		l,r,d := io.NextInt(), io.NextInt(), io.NextInt()
		vp = append(vp, Pii{l-d, d})
		vp = append(vp, Pii{r-d, -d})
	}
	sort.Sort(Piis(vp))

	mp := NewAvlTree()

	for i := 0; i < q; i++ {
		d := io.NextInt()
		for len(vp) > 0 && vp[0].f <= d {
			if vp[0].s > 0 {
				tmp := ComparableInt(vp[0].s)
				mp.Insert(&tmp)
			} else {
				tmp := ComparableInt(-vp[0].s)
				mp.Delete(&tmp)
			}
			vp = vp[1:]
		}

		ans := -1
		if mp.size > 0 {
			cur := mp.Smallest().(*ComparableInt)
			ans = int(*cur)
		}

		io.PrintLn(ans)
	}
}


func main() {
	io = NewIo()
	f, err := os.Open("src/input")
	if err == nil {
		io.reader = bufio.NewReader(f)
	}
	defer io.Flush()

	solve()
}