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 <string.h>
#include <stdlib.h>
#include <stdint.h> // int64_t

#define max(a,b) ((a) > (b) ? (a) : (b))
#define min(a,b) ((a) > (b) ? (b) : (a))

int get_int(void) {
  int num;
  scanf("%d", &num);
  return num;
}

int get_int2(int *a1, int *a2) {
  scanf("%d %d", a1, a2);
  return 0;
}

int get_int3(int *a1, int *a2, int *a3) {
  scanf("%d %d %d", a1, a2, a3);
  return 0;
}

//ここから二分探索木
typedef struct bt
{
	int value;
	struct bt *left;
	struct bt *right;
}btree;
 
//int btsize=0;
btree *root;
//root = NULL;
 
void insert(int value)
{
	btree *x,*y,*z;
	z = (btree *)(malloc(sizeof(btree)));//新しい要素のメモリ確保
	z->value=value;
	z->left=NULL;
	z->right=NULL;
	
	x=root; y=NULL;
	while(x!=NULL)
	{
		y=x;
		if(z->value < x->value)x = x->left;
		else x = x->right;
	}
	if(y==NULL)root=z;
	else if(z->value < y->value)y->left=z;
	else y->right=z;
	//printf("inserted %d\n",value);
}
 
btree *del(btree *dstt, btree *t)
{
	btree *q;
	if(t->right!=NULL)t->right = del(dstt,t->right);
	else
	{
		dstt->value = t->value; 
		q = t;
		t = t->left;
		free(q);
	}
	return(t);
}
 
void delete(int value)
{
	btree *x,*y,*z,*p;
	x=root;
	while(x!=NULL && x->value!=value)
	{
		p=x;//親になる
		if(value < x->value)x = x->left;
		else x = x->right;
	}
	//printf("%d %d\n",x->left,x->right);
	y=x;//削除されるべき値を持ったノード
	if(y->right==NULL)//右部分木がない
	{
		z = y;
		if(y==root)root =y->left;
		else if(value < p->value)p->left =y->left;
		else p->right = y->left;
		free(z);
	}
	else if(y->left==NULL)//左部分木がない
	{
		z = y;
		if(y==root)root =y->right;
		else if(value < p->value)p->left =y->right;
		else p->right = y->right;
		//printf("%d %d\n",y,*(y));
		free(z);
	}
	else
	{
		y->left = del(y,y->left);//左部分木の最右節点をこの木の根に移動する
	}
	//printf("deleted %d\n",value);
}
 
int peek_min()
{
	btree *x,*y;
	x=root;
	while(x!=NULL)
	{
		y=x;
		x=x->left;
	}
	if(x==root)return -1;
	else return (y->value);
}
 
//ここまで二分探索木

#define NUM_MAX 200000
#define QUERY_MAX 200000

struct event {
    int flag;
    int time;
    int pos;
};

int asc_by_time(const void *a1, const void *a2) {
    struct event *e1 = (struct event*)a1;
    struct event *e2 = (struct event*)a2;
    return e1->time - e2->time;
}

// <arr[0]> <arr[1]> .. <arr[size-1]>
#define INF (2e+9)
int main(void) {
    int num, qs;
    get_int2(&num, &qs);
    int i;
    static struct event events[NUM_MAX*2+QUERY_MAX+2];
    static int eidx = 0;
    events[0] = (struct event){1, -INF, INF};
    for(i = 0; i < num; i++) {
        int start, end, pos;
        get_int3(&start, &end, &pos);
        start -= pos;
        end -= pos;
        events[eidx++] = (struct event){1, start, pos};
        events[eidx++] = (struct event){-1, end, pos};
    }
    events[eidx++] = (struct event){1, INF, INF};

    for(i = 0; i < qs; i++) {
        int time = get_int();
        events[eidx++] = (struct event){0, time, 0};
    }
    qsort(events, eidx, sizeof(struct event), asc_by_time);

    int mmin = INF;
    static int ans[QUERY_MAX];
    int aidx = 0;
    for(i = 0; i < eidx; i++) {
        struct event e = events[i];
        if(!e.flag) {
            ans[aidx++] = mmin;
            continue;
        }
        if(e.flag == 1) {
            insert(e.pos);
        } else if (e.flag == -1) {
            delete(e.pos);
        }
        mmin = peek_min();
    }


    // presentation
    for(i = 0; i < aidx; i++) {
        printf("%d\n", ans[i] == INF ? -1 : ans[i]);
    }
    return 0;
}