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 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define df 1
#define MAX 200010
int n,q;

typedef struct{
  int num;
  int S;
  int X;
  int flag;
} data;


data work[MAX*2];

int comp_d(const void* a,const void* b){
  data p=*((data*)a), q=*((data*)b);
  if(p.X!=q.X) return p.X-q.X;
  if(p.num!=q.num) return p.num-q.num;
  return q.flag-p.flag;
}

int comp(const void* a,const void* b){
  data p=*((data*)a), q=*((data*)b);
  return p.S-q.S;
}

typedef struct{
  int num;
  int X;
}cell;
int eq(cell a,cell b){
  return (a.num==b.num && a.X==b.X);
}

cell Q0[MAX+1];  
cell Q1[MAX+1];
int len0=0,len1=0;
#define PARENT(i) ((i)>>1)
#define RIGHT(i) (((i)<<1)+1)
#define LEFT(i) ((i)<<1)
#define NULL_CELL (cell){-1,-1}



int compare(cell a,cell b){
  if(a.X!=b.X) return a.X<b.X;
  return a.num<b.num;
}

void enque(cell* Q,int* len,cell a);
void del(cell* Q,int* len);
void solve();
void print_que(cell* Q,int len);

int k=0;


int main(){
  scanf("%d%d",&n,&q);
  int i;
  for(i=0;i<n;i++){
    int s,t,x;
    scanf("%d%d%d",&s,&t,&x);
    work[i].num=i;
    work[i].S=s-x;
    work[i].X=x;
    work[i].flag=1;
    work[i+n].num=i;
    work[i+n].S=t-x;
    work[i+n].X=x;
    work[i+n].flag=0;
  }
  if(df){
    qsort(work,n*2,sizeof(data),comp_d);
    printf("i S X f\n");
    for(i=0;i<2*n;i++)
      printf("%d %2d %2d %d\n"
	     ,work[i].num,work[i].S,work[i].X,work[i].flag);
    printf("   ");
    for(i=0;i<71;i++)printf("%d",i%10);
    printf("\n");
    for(i=0;i<2*n;i+=2){
      printf("%2d:",work[i].X);
      if(work[i+1].S<0)printf("X");
      else{
	int j;
	for(j=0;j<work[i].S;j++)printf(" ");
	for(;j<work[i+1].S;j++)printf("-");
      }
      printf("(%d)\n",work[i].num);
    }
  }
  qsort(work,n*2,sizeof(data),comp);
  for(i=0;i<q;i++){
    solve();
  }
  return 0;
}
void enque(cell* Q,int* len,cell a){
    if(df)printf("len:%d->%d (enq:{%d,%d})\n",*len,*len+1,a.num,a.X);
  (*len)++;
  int i=*len;
  while(i>1 && compare(a,Q[PARENT(i)])){
    //    if(df)printf("parent: %d\n",PARENT(i));
    Q[i]=Q[PARENT(i)];
    i=PARENT(i);
  }
  Q[i]=a;
    if(df)print_que(Q,*len);
}
void del(cell* Q,int* len){
  if(len==0)return; 
  if(df)  printf("len:%d->%d (del)\n",*len,*len-1);
  cell a=Q[(*len)--];
  int i=1;
  while(LEFT(i)<=*len){
    int min=LEFT(i);
    //    if(df)printf("right: %d left:%d\n",RIGHT(i),LEFT(i));
    if(RIGHT(i)<=*len) min=compare(Q[min],Q[RIGHT(i)])?min:RIGHT(i);
    if(compare(a,Q[min])) break;
    Q[i]=Q[min];
    i=min;
  }
  Q[i]=a;
    if(df)print_que(Q,*len);
}

void solve(){
  int d;
  scanf("%d",&d);
  if(df)printf("solve:%d\n",d);
  while(k<2*n && work[k].S<=d){
    data a=work[k];
    if(df)printf("flag:%d\n",a.flag);
    if(a.flag){
      enque(Q1,&len1,(cell){a.num,a.X});
    }else{
      enque(Q0,&len0,(cell){a.num,a.X});
    }
    if(df){
    }
    k++;
  }
  cell c1=Q1[1],c0=Q0[1];
  while(len1!=0 && eq(c1,c0)){
    del(Q1,&len1);
    del(Q0,&len0);
    c1=Q1[1]; c0=Q0[1];
  }
    if(df){
      printf("\n");
      print_que(Q0,len0);
      print_que(Q1,len1);
    }
  if(len1==0){
    printf("-1\n");
    return;
  }
  if(df)printf("[");
  printf("%d",c1.X);
  if(df)printf("]");
  printf("\n");
}

void print_que(cell* Q,int len){
  int i;
  printf("Q:[");
  for(i=1;i<= len;i++){
    printf("%d(%d)",Q[i].X,Q[i].num);
  }
  printf("]\n");
}

/// confirm df==0 ///