Go: package main

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

func main() {

	sc := bufio.NewScanner(os.Stdin)
	sc.Scan()
	args := strings.Split(sc.Text(), " ")
	n, _ := strconv.Atoi(args[0])
	t, _ := strconv.ParseInt(args[1], 10, 64)

	q, _ := strconv.Atoi(args[2])

	pos := make([]int64, n)
	direction := make([]bool, n)

	for i := 0; i < n; i++ {
		sc.Scan()
		args = strings.Split(sc.Text(), " ")

		pos[i], _ = strconv.ParseInt(args[0], 10, 64)
		q, _ := strconv.Atoi(args[1])
		if q == 1 {
			direction[i] = true
		}
	}

	qq := make([]int, q)
	for i := 0; i < q; i++ {
		sc.Scan()
		args = strings.Split(sc.Text(), " ")

		qq[i], _ = strconv.Atoi(args[0])
	}

	ans(n, q, t, pos, direction, qq)

}

func ans(n, q int, t int64, pos []int64, dir []bool, qq []int) {

	colleation := []int64{} // syoutotu positon

	for i := 1; i < n; i++ {
		if dir[i-1] == dir[i] {
			continue
		}
		colleation = append(colleation, (pos[i-1]+pos[i])/2)

	}
	//fmt.Println(colleation)
	for i := 0; i < q; i++ {
		var ans int64
		ans = int64(-9999999)
		if dir[qq[i]-1] == true {
			// 東向きなら現ポジから＋方向で一番近い衝突点との距離を求めて、Tとの差が
			for _, v := range colleation {
				if v <= pos[qq[i]-1] {
					continue
				} else {
					// 衝突予定点が確定。衝突点との距離がT以下なら衝突点で停止
					if v-pos[qq[i]-1] <= t {
						ans = v
					} else {
						ans = pos[qq[i]-1] + int64(t)
					}
					break
				}
			}
			if ans == -9999999 {
				ans = pos[qq[i]-1] + int64((t))
			}

		} else {
			// nishi向きなら現ポジから-方向で一番近い衝突点との距離を求めて、Tとの差が
			for j := len(colleation); j > 0; j-- {
				if colleation[j-1] >= pos[qq[i]-1] {
					continue
				}
				v := colleation[j-1]
				//fmt.Printf("hoge=%d\n", v)
				// 衝突予定点が確定
				if pos[qq[i]-1]-v <= t {
					ans = v
				} else {
					ans = pos[qq[i]-1] - int64((t))
				}
				break

			}
			if ans == -9999999 {
				ans = pos[qq[i]-1] - int64((t))
			}

		}
		fmt.Printf("%d\n", ans)

	}

}

C++: #include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

typedef long long LL;
typedef pair<int, int> P;

int main() {
	int a, c;
	LL b;
	cin >> a >> b >> c;

	P m[100005];
	P q[1010];
	vector<int>st;

	for (int i = 0;i < a;i++)cin >> m[i].first >> m[i].second;
	for (int i = 0;i < c;i++) {
		int d;
		cin >> d;
		q[i] = m[d - 1];
	}

	sort(m, m + a);

	for (int i = 0;i < a - 1;i++) {
		if (m[i].second == 1 && m[i + 1].second == 2)st.push_back((m[i].first + m[i+1].first) / 2);
	}

	for (int i = 0;i < c;i++) {

		if (q[i].second == 1) {

			long long e = 3e13;

			for (int j = 0;j < st.size();j++) {

				if (q[i].first < st[j]) {
					if (q[i].first + b >= st[j])e = st[j];
					else e = q[i].first + b;
				}

			}
			if (e == 3e13)e = q[i].first + b;
			cout << e << endl;
		}

		else {

			long long e=3e13;

			for (int j = st.size() - 1;j > -1;j--) {

				if (q[i].first > st[j]) {
					if (q[i].first - b <= st[j])e = st[j];
					else e = q[i].first - b;
				}
				
			}
			if (e == 3e13)e = q[i].first - b;
			cout << e << endl;
		}
	}

	return 0;
}

C++: #include <stdio.h>
#include <cmath>
#include <algorithm>
#include <cfloat>
#include <stack>
#include <queue>
#include <vector>
typedef long long int ll;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000001
using namespace std;

enum DIR{
	None,
	Right,
	Left,
};

DIR dir_array[3] = {None,Right,Left};

struct Info{
	void set(ll arg_loc,DIR arg_dir){
		loc = arg_loc;
		dir = arg_dir;
	}
	ll loc,moved_loc;
	DIR dir;
	bool is_Moved;
};

int main(){

	int N,Q,tmp_dir;
	ll T,tmp_loc;
	scanf("%d %lld %d",&N,&T,&Q);

	int* most_left_to_left_Peron = new int[N+2];
	int* most_right_to_right_Person = new int[N+2];
	Info* info = (Info*)malloc(sizeof(Info)*(N+1));

	for(int i = 1; i <= N; i++){
		scanf("%lld %d",&tmp_loc,&tmp_dir);
		info[i].set(tmp_loc,dir_array[tmp_dir]);
		info[i].is_Moved = false;
	}

	most_left_to_left_Peron[N+1] = BIG_NUM;
	for(int i = N; i >= 1; i--){
		if(info[i].dir == Left){
			most_left_to_left_Peron[i] = i;
		}else{
			most_left_to_left_Peron[i] = most_left_to_left_Peron[i+1];
		}
	}

	most_right_to_right_Person[0] = 0;
	for(int i = 1; i <= N; i++){
		if(info[i].dir == Right){
			most_right_to_right_Person[i] = i;
		}else{
			most_right_to_right_Person[i] = most_right_to_right_Person[i-1];
		}
	}

	int tmp;

	for(int i = 1; i <= N; i++){
		if(info[i].dir == Right && most_left_to_left_Peron[i] != BIG_NUM && most_right_to_right_Person[most_left_to_left_Peron[i]] == i){
			tmp = most_left_to_left_Peron[i];
			if(info[tmp].loc-info[i].loc > 2*T){
				info[tmp].moved_loc = info[tmp].loc - T;
				info[i].moved_loc = info[i].loc + T;
			}else{
				info[tmp].moved_loc = (info[tmp].loc + info[i].loc)/2;
				info[i].moved_loc = (info[tmp].loc + info[i].loc)/2;
			}
			info[tmp].is_Moved = true;
			info[i].is_Moved = true;
		}
	}

	for(int i = 1; i <= N; i++){
		if(info[i].dir == Right && info[i].is_Moved == false){
			if(most_left_to_left_Peron[i] == BIG_NUM){
				info[i].moved_loc = info[i].loc + T;
			}else{
				tmp = most_left_to_left_Peron[i];
				if(info[tmp].moved_loc > info[i].loc+T){
					info[i].moved_loc = info[i].loc + T;
				}else{
					info[i].moved_loc = info[tmp].moved_loc;
				}
			}
		}else if(info[i].dir == Left  && info[i].is_Moved == false){
			if(most_right_to_right_Person[i] == 0){
				info[i].moved_loc = info[i].loc-T;
			}else{
				tmp = most_right_to_right_Person[i];
				if(info[tmp].moved_loc < info[i].loc-T){
					info[i].moved_loc = info[i].loc-T;
				}else{
					info[i].moved_loc = info[tmp].moved_loc;
				}
			}
		}
	}

	int person;

	for(int i = 0; i < Q; i++){
		scanf("%d",&person);

		printf("%lld\n",info[person].moved_loc);
	}

	return 0;
}

C++: #include <bits/stdc++.h>
#define range(i, a, b) for(int i = (a); i < (b); i++)
#define rep(i, a) range(i, 0, a)
using namespace std;

int main() {
    long long n, t, q;
    cin >> n >> t >> q;
    vector <vector <long long>> ad(4);
    vector <long long> a(n), d(n);
    rep (i, n) {
        cin >> a[i] >> d[i], d[i]--;
        ad[d[i]    ].push_back( a[i]);
        ad[d[i] + 2].push_back(-a[i]);
    }
    sort(ad[2].begin(), ad[2].end());
    sort(ad[3].begin(), ad[3].end());
    // 0 := 右向き
    // 1 := 左向き
    // 2 := 右向き符号反転
    // 3 := 左向き符号反転

    rep (i, q) {
        int in;
        cin >> in, in--;
        auto itrA = d[in] == 0 ? lower_bound(ad[1].begin(),  ad[1].end(),  a[in])
                               : lower_bound(ad[2].begin(),  ad[2].end(), -a[in]);

        auto itrB = d[in] == 0 ? upper_bound(ad[2].begin(),  ad[2].end(),  a[in])
                               : upper_bound(ad[3].begin(),  ad[3].end(), -a[in]);

        //auto itrB = lower_bound(ad[d[in]].rbegin(), ad[d[in]].rend(), a[in]);

        if (itrA == ad[d[in] + 1].end()) {
            cout << a[in] + t << endl;
        }
        else if (itrB == ad[d[in]].end()) {
            cout << (d[in] ? a[in] - min(t, (*itrA - a[in]) / 2)
                           : a[in] + min(t, (*itrA - a[in]) / 2))
                 << endl;
        }
        else {
            //左に進む
            if (d[in]) {
                cout << max(-*itrA + abs(*itrA - *itrB) / 2, a[in] - t) << endl;
            }
            else {
                //yosaso
                cout << min((*itrA + max(a[in], -*itrB)) / 2, a[in] + t) << endl;
            }
        }
        /*
        cout << a[in] << " "
             << (itrA == ad[!d[in]].end() ? "va" : to_string(*itrA)) << " "
             << (itrB == ad[d[in]].end()  ? "va" : to_string(*itrB)) << endl;
             */
        //if (itr == ad[!d[in]].end()) cout << "va" << endl;
        //else if (d[i] == 0) cout << a[in] << " " << *itr << endl;
        //else cout << -a[in] << " " << *itr << endl;
    }
    return 0;
}