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: Runtime Error)

using System;
using System.Linq;//リストの使用
using System.Collections.Generic;
using System.Text;//テキストの高速出力に必要
class Program
{
static int[] compressedX;//[i]は、iに圧縮前の座標
static int[] compressedY;
static int[] segttt = new int[(1<<20)];
static int segkkk = 0;
static int seglll = 0;
static int segrrr = (1<<19);
static int[] segValue = new int[600005];


	static void Main()
	{
		string[] input = Console.ReadLine().Split(' ');
		int n = int.Parse(input[0]);
		int q = int.Parse(input[1]);
    int[,] vertexes = new int[n,3];
    segValue[0] = -1;//初期化
    for(int i = 0; i < n; i++)
    {
      int[] numsa = Array.ConvertAll(Console.ReadLine().Split(' '),int.Parse);
      vertexes[i,0] = numsa[0];
      vertexes[i,1] = numsa[1];
      vertexes[i,2] = numsa[2];
    }
    int[,] times = new int[2*n+q,2];
    for(int i = 0; i < n; i++)
    {
      times[i,0] = vertexes[i,0]-vertexes[i,2];
      times[n+i,0] = vertexes[i,1]-vertexes[i,2]-1;
      times[i,1] = i;
      times[n+i,1] = n+i;
    }
    for(int i = 0; i < q; i++)
    {
      times[2*n+i,0] = int.Parse(Console.ReadLine());
      times[2*n+i,1] = 2*n+i;
    }

    int[,] afterTimes = CoordinateCompression(times);//1次元の座標圧縮

    int[][] timesSub = new int[n][];
    for(int i = 0; i < n; i++)
    {
      timesSub[i] = new int[3];
      timesSub[i][0] = afterTimes[i,2];
      timesSub[i][1] = afterTimes[n+i,2];
      timesSub[i][2] = vertexes[i,2];
    }
    Array.Sort(timesSub, (a, b) => b[2].CompareTo(a[2]));
        
      for(int i = 0; i < n; i++)
      {
        if(timesSub[i][1] >= 0)
        {
          segValue[i+1] = timesSub[i][2];
          AreaChangePointGetSeg((int)Math.Max(0,timesSub[i][0]), (int)timesSub[i][1], i+1);
        }
      }
      for(int i = 0; i < q; i++)//値を求める
      {
        int answer = AreaChangePointGetSegAnswer((int)afterTimes[2*n+i,2]);
        Console.WriteLine(segValue[answer]);
      }
	}

  static int[,] CoordinateCompression(int[,] vertexesOriginal)
    //1次元の座標圧縮。配列は0-indexで[x,順番]。compressedXも更新。
    //例えば、{2,4,4,3}は{0,2,2,1}を返す。
  {
    int n = vertexesOriginal.GetLength(0);//多次元配列の長さの取得
    int xCount = 1;//圧縮後のxの個数
    int[] compressedXmemo = new int[n];
    int[][] vertexes = new int[n][];
    for(int i = 0; i < n; i++)
    {
      vertexes[i] = new int[3];
      for(int j = 0; j < 2; j++)
      {
        vertexes[i][j] = vertexesOriginal[i,j];//配列をジャグ配列に置換
      }
    }

    Array.Sort(vertexes, (a, b) => a[0].CompareTo(b[0]));//x座標でソート
    vertexes[0][2] = 0;
    compressedXmemo[0] = vertexes[0][0];
    for(int i = 1; i < n; i++)
    {
      if(vertexes[i][0] == vertexes[i-1][0])//x座標が同じ
      {
        vertexes[i][2] = xCount - 1;
      }else
      {
        xCount++;
        vertexes[i][2] = xCount - 1;
        compressedXmemo[xCount-1] = vertexes[i][0];
      }
    }
    compressedX = new int[xCount];
    for(int i = 0; i < xCount; i++) compressedX[i] = compressedXmemo[i];

    Array.Sort(vertexes, (a, b) => a[1].CompareTo(b[1]));//もとの番号順でソート

    int[,] answer = new int[n,3];
    for(int i = 0; i < n; i++)
    {
      for(int j = 0; j < 3; j++)
      {
        answer[i,j] = vertexes[i][j];//ジャグ配列から戻す
      }
    }

    return answer;//[x,順番,圧縮x]を返す。
  }

  static void AreaChangePointGetSeg(int a, int b, int x)
  {
    AreaChangePointGetSegSub(a,b+1,x,segkkk,seglll,segrrr);
  }
    
  static void AreaChangePointGetSegSub(int a, int b, int x, int segkkk, int seglll, int segrrr)
  {//区間以上以下の値を全てchangeNumに変更し、ある１点の値を求める。入力側
    if(b <= seglll || segrrr <= a)
    {
      b = b;
    }
    else if(a <= seglll && segrrr <= b)
    {
      segttt[segkkk] = Math.Max(segttt[segkkk],x);
    }else
    {
      int m = (seglll+segrrr)/2;
      AreaChangePointGetSegSub(a, b, x, segkkk*2+1, seglll, m);
      AreaChangePointGetSegSub(a, b, x, segkkk*2+2, m, segrrr);
    }
  }
    
  static int AreaChangePointGetSegAnswer(int i)
  {//区間以上以下の値を全てchangeNumに変更し、ある１点の値を求める。出力側。
    i += (1<<19)-1;
    int res = segttt[i];
    while(i != 0)
    {
      i = (i-1)/2;
      res = Math.Max(res,segttt[i]);
    }
    return res;
  }
   
 }