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 C# (Mono 4.6.2.0) (judge verdict: Time Limit Exceeded)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Runtime.CompilerServices;

public class Contest
{
	public const long MOD1 = 1000000007L; // 10^9+7

		public static void Main()
	{
 
        int n, q;
		VT<int,int,int>[] stxl;
		int[] dl;
		MyIO.ReadMulti(out n, out q);
		MyIO.ReadLines(n, out stxl);
		MyIO.ReadLines(q, out dl);

		VT<int,int,int>[] strl = new VT<int, int, int>[n+1];
		for (int i = 0; i < n; i++)
		{
			strl[i] = new VT<int, int, int>(stxl[i].Item1-stxl[i].Item3,stxl[i].Item2-stxl[i].Item3,stxl[i].Item3);
		}
		strl[n] = new VT<int, int, int>(int.MaxValue,int.MaxValue,-1);

		Array.Sort(strl,(x,y)=>(x.Item2-y.Item2));
		int qi = 0;
		int bi = 0;
		SortedSet<int> el = new SortedSet<int>();
		int[] ret = new int[q];
		while(qi<q){
			for (int i = bi; i < strl.Length; i++)
			{
				if(strl[i].Item2<=dl[qi]){
					bi = i;
				}else if((strl[i].Item1<=dl[qi])&&(dl[qi]<strl[i].Item2)){
					el.Add(strl[i].Item3);
				}
			}
			if(el.Count==0) {
				ret[qi] = -1;
			}else{
				ret[qi] = el.Min;
				el.Clear();
			}

			qi++;
		}







		MyIO.WriteLines(ret);
 
 
 
		
	}
 
 
 
 
 
 
}
 
 
 











public struct LazySegmentTree<TMonoid, TOperatorMonoid> {
	private int sz;
	private TMonoid[] data;
	private TOperatorMonoid[] lazy;
	private Func<TMonoid, TMonoid, TMonoid> f;
	private Func<TMonoid, TOperatorMonoid, int, TMonoid> g;
	private Func<TOperatorMonoid, TOperatorMonoid, TOperatorMonoid> h;
	private TMonoid M1;
	private TOperatorMonoid OM0;

	public LazySegmentTree(int n, Func<TMonoid, TMonoid, TMonoid> F, Func<TMonoid, TOperatorMonoid, int, TMonoid> G, Func<TOperatorMonoid, TOperatorMonoid, TOperatorMonoid> H, TMonoid M1, TOperatorMonoid OM0) {
		f = F; g = G; h = H;
		this.M1 = M1; this.OM0 = OM0;
		sz = 1;
		while(sz < n) sz <<= 1;
		data = Enumerable.Repeat(M1, 2 * sz).ToArray();
		lazy = Enumerable.Repeat(OM0, 2 * sz).ToArray();
	}

	public void Build() {
		for(int k = sz - 1; k > 0; k--) {
			data[k] = f(data[2 * k + 0], data[2 * k + 1]);
		}
	}

	private void propagate(int k, int len) {
		if(!lazy[k].Equals(OM0)) {
			if(k < sz) {
				lazy[2 * k + 0] = h(lazy[2 * k + 0], lazy[k]);
				lazy[2 * k + 1] = h(lazy[2 * k + 1], lazy[k]);
			}
			data[k] = g(data[k], lazy[k], len);
			lazy[k] = OM0;
		}
	}

	public TMonoid Update(int a, int b, TOperatorMonoid x, int k, int l, int r) {
		propagate(k, r - l);
		if(r <= a || b <= l) {
			return data[k];
		} else if(a <= l && r <= b) {
			lazy[k] = h(lazy[k], x);
			propagate(k, r - l);
			return data[k];
		} else {
			return data[k] = f(Update(a, b, x, 2 * k + 0, l, (l + r) >> 1), Update(a, b, x, 2 * k + 1, (l + r) >> 1, r));
		}
	}

	public TMonoid Update(int a, int b, TOperatorMonoid x) {
		return Update(a, b, x, 1, 0, sz);
	}

	public TMonoid Query(int a, int b, int k, int l, int r) {
		propagate(k, r - l);
		if(r <= a || b <= l) {
			return M1;
		} else if(a <= l && r <= b) {
			return data[k];
		} else {
			return f(Query(a, b, 2 * k + 0, l, (l + r) >> 1), Query(a, b, 2 * k + 1, (l + r) >> 1, r));
		}
	}

	public TMonoid Query(int a, int b) {
		return Query(a, b, 1, 0, sz);
	}

    public TMonoid this[int k] {
        get { return Query(k, k + 1); }
		set { data[k + sz] = value; }
    }

}





public struct VT<T, U>
{
	public T Item1; public U Item2;
    public VT(T a, U b) { Item1 = a; Item2 = b; }
}
public struct VT<T, U, V>
{
	public T Item1; public U Item2; public V Item3;
    public VT(T a, U b, V c) { Item1 = a; Item2 = b; Item3 = c; }
}
public struct VT<T, U, V, W>
{
	public T Item1; public U Item2; public V Item3; public W Item4;
    public VT(T a, U b, V c, W d) { Item1 = a; Item2 = b; Item3 = c; Item4 = d; }
}

public class Printer : System.IO.StreamWriter
{
	public override IFormatProvider FormatProvider { get { return System.Globalization.CultureInfo.InvariantCulture; } }
	public Printer(System.IO.Stream stream) : base(stream, new UTF8Encoding(false, true)) { }
	public Printer(System.IO.Stream stream, Encoding encoding) : base(stream, encoding) { }
}
public static class MyIO
{
	private static char[] splitter = new char[] {' '};
	private static bool eq<T, U>() => typeof(T).Equals(typeof(U));
	private static T ct<T, U>(U a) => (T)Convert.ChangeType(a, typeof(T));
	private static T cv<T>(string s) => eq<T, int>()    ? ct<T, int>(int.Parse(s))
	                                  : eq<T, long>()   ? ct<T, long>(long.Parse(s))
	                                  : eq<T, double>() ? ct<T, double>(double.Parse(s, System.Globalization.CultureInfo.InvariantCulture))
	                                  : eq<T, char>()   ? ct<T, char>(s[0])
	                                                    : ct<T, string>(s);				
	public static string[] ReadMulti<T>(out T a) {
		var ar = Console.ReadLine().Split(splitter, StringSplitOptions.RemoveEmptyEntries); 
		a = cv<T>(ar[0]);
		return ar;
	}
	public static string[] ReadMulti<T, U>(out T a, out U b) {
		var ar = ReadMulti<T>(out a); 
        b = cv<U>(ar[1]);
		return ar;
	}
	public static string[] ReadMulti<T, U, V>(out T a, out U b, out V c) {
		var ar = ReadMulti<T, U>(out a, out b); 
        c = cv<V>(ar[2]);
		return ar;
	}
	public static string[] ReadMulti<T, U, V, W>(out T a, out U b, out V c, out W d) {
		var ar = ReadMulti<T, U, V>(out a, out b, out c); 
        d = cv<W>(ar[3]);
		return ar;
	}
	public static string[] ReadMulti<T, U, V, W, X>(out T a, out U b, out V c, out W d, out X e) {
		var ar = ReadMulti<T, U, V, W>(out a, out b, out c, out d); 
        e = cv<X>(ar[4]);
		return ar;
	}
	public static string[] ReadMulti<T, U, V, W, X, Y>(out T a, out U b, out V c, out W d, out X e, out Y f) {
		var ar = ReadMulti<T, U, V, W, X>(out a, out b, out c, out d, out e); 
        f = cv<Y>(ar[5]);
		return ar;
	}
	public static string[] ReadArray<T>(out T[] a) {		
		var ar = Console.ReadLine().Split(splitter, StringSplitOptions.RemoveEmptyEntries);
		a = ar.Select(cv<T>).ToArray();
		return ar;
	}		
	public static string[][] ReadLines<T>(int n, out T[] a) {
		a = new T[n];
		string[][] arr = new string[n][];
		for(int i=0; i<n; i++) arr[i] = ReadMulti(out a[i]);
		return arr;
	}
	public static string[][] ReadLines<T, U>(int n, out VT<T, U>[] a) {
		a = new VT<T, U>[n];
		string[][] arr = new string[n][];
		for(int i=0; i<n; i++) {
			T o; U p;
			arr[i] = ReadMulti(out o, out p);
			a[i] = new VT<T, U>(o, p);
		}
		return arr;
	}
	public static string[][] ReadLines<T, U, V>(int n, out VT<T, U, V>[] a) {
		a = new VT<T, U, V>[n];
		string[][] arr = new string[n][];
		for(int i=0; i<n; i++) {
			T o; U p; V q;
			arr[i] = ReadMulti(out o, out p, out q);
			a[i] = new VT<T, U, V>(o, p, q);
		}
		return arr;
	}
	public static string[][] ReadLines<T, U, V, W>(int n, out VT<T, U, V, W>[] a) {
		a = new VT<T, U, V, W>[n];
		string[][] arr = new string[n][];
		for(int i=0; i<n; i++) {
			T o; U p; V q; W r;
			arr[i] = ReadMulti(out o, out p, out q, out r);
			a[i] = new VT<T, U, V, W>(o, p, q, r);
		}
		return arr;
	}
	public static string[][] ReadMatrix<T>(int n, out T[][] a) {
		a = new T[n][];
		string[][] arr = new string[n][];
		for(int i=0; i<n; i++) arr[i] = ReadArray(out a[i]);
		return arr;
	}

	private static void wwp(Action act){
		var sw = new Printer(Console.OpenStandardOutput()){AutoFlush = false};
		Console.SetOut(sw);
		act();
		Console.Out.Flush();
		sw.AutoFlush = true;
		Console.SetOut(sw);
	}
	private static string wfm(Type t){
		return t.Equals(typeof(double)) ? "{0:F10}" : "{0}";
	}
	public static void WriteMulti(params object[] a) {
		wwp(()=>{
			for(int i=0;i<a.Length;i++) {
				string fs = wfm(a[i].GetType()) + ((i<a.Length-1) ? " " : Environment.NewLine);
				Console.Write(fs, a[i]);
			}
		});
	}
	public static void WriteArray<T>(T[] a) {
		wwp(()=>{
			string fs = wfm(typeof(T));
			string fs1 = fs + " ";
			string fs2 = fs + Environment.NewLine;
			for(int i=0;i<a.Length;i++) Console.Write( ((i<a.Length-1) ? fs1 : fs2), a[i]);
		});
	}
	public static void WriteLines<T>(T[] a) {
		wwp(()=>{
			string fs = wfm(typeof(T)) + Environment.NewLine;
			foreach(T x in a) Console.Write(fs, x);
		});
	}



}