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)

	}

}

Python: from bisect import bisect_left as bl
n, t, q = map(int, input().split())
lst = [list(map(int, input().split())) for _ in range(n)]
qlst = [lst[int(input()) - 1] for _ in range(q)]

left = [a for a, d in lst if d == 2]
right = [a for a, d in lst if d == 1]
lenl = len(left)
lenr = len(right)
for a, d in qlst:
  if d == 1:
    coll_ind = bl(left, a)
    if coll_ind >= lenl:
      print(a + t)
    else:
      coll = left[coll_ind]
      coll2 = right[bl(right, coll) - 1]
      coll_pos = (coll + coll2) // 2
      print(min(a + t, coll_pos))

  else:
    coll_ind = bl(right, a) - 1
    if coll_ind == -1:
      print(a - t)
    else:
      coll = right[coll_ind]
      coll2 = left[bl(left, coll)]
      coll_pos = (coll + coll2) // 2
      print(max(a - t, coll_pos))

Python: import bisect
n, t, q = map(int, input().split())
x = [[0, 0] for _ in range(n)]
s = []
for i in range(n):
    x[i] = list(map(int, input().split()))
    if x[i - 1][1] == 1 and x[i][1] == 2:
        s.append(int((x[i - 1][0] + x[i][0]) / 2))
for _ in range(q):
    v = int(input()) - 1
    if x[v][1] == 1:
        st = bisect.bisect_left(s, x[v][0])
        print(min(x[v][0] + t, s[st]) if st < len(s) else x[v][0] + t)
    else:
        st = bisect.bisect_left(s, x[v][0])
        print(max(x[v][0] - t, s[st - 1]) if st > 0 else x[v][0] - t)

C++: //Walking in JOI Kingdom
#include <iostream>

using namespace std;
typedef long long ll;

static const int MAX = 100;

enum DIRECTION {
    EAST = 1,
    WEST = 2
};

struct House {
    ll a;
    int d;
    House() {}
    House(ll a, int d): a(a), d(d) {}
};

int N, Q;
ll T;
House H[MAX + 1];
ll A[MAX + 1];
bool Traced[MAX + 1];

ll trace(int target) {
    if(Traced[target]) return A[target];
    ll a;
    if(H[target].d == EAST) {
        if(target == N) a = H[target].a + T;
        else {
            int neighbor = target + 1;
            if(H[neighbor].d == EAST) {
                if(H[neighbor].a - H[target].a >= T) {
                    a = H[target].a + T;
                } else {
                    a = min(H[target].a + T, trace(neighbor));
                }
            }
            else a = min(H[target].a + T, (H[target].a + H[neighbor].a) / 2);
        }
    } else if(H[target].d == WEST) {
        if(target == 1) a = H[target].a - T;
        else {
            int neighbor = target - 1;
            if(H[neighbor].d == WEST) {
                if(H[target].a - H[neighbor].a >= T) {
                    a = H[target].a - T;
                } else {
                    a = max(H[target].a - T, trace(neighbor));
                }
            }
            else a = max(H[target].a - T, (H[target].a + H[neighbor].a) / 2);
        }
    }
    Traced[target] = true;
    A[target] = a;
    return a;
}

int main() {
    cin >> N >> T >> Q;

    ll a;
    int d;
    for(int i = 1; i < N + 1; i++) {
        cin >> a >> d;
        H[i] = House(a, d);
        Traced[i] = false;
    }

    int target;
    for(int i = 0; i < Q; i++) {
        cin >> target;
        cout << trace(target) << endl;
    }
}

C++: #include <bits/stdc++.h>
using namespace std;

long long N,T,Q;
long long A[100001],D[100001];
long long X[10001];
vector<long long> stp;

int main(){
	scanf("%lld%lld%lld",&N,&T,&Q);
	for(int i = 0; i < N; i++){
		scanf("%lld%lld",&A[i],&D[i]);
	}
	for(int i = 0; i < Q; i++){
		scanf("%lld",&X[i]);
		X[i]--;
	}
	stp = vector<long long>();
	for(int i = 0;i < N-1; i++){
		if(D[i] == 1 && D[i+1] == 2){
			stp.push_back((A[i] + A[i+1])/2);
		}
	}


	for(int i = 0; i < Q; i++){
		if(stp.size() == 0){
			printf("%lld\n",D[X[i]] == 1 ? A[X[i]]+T : A[X[i]]-T);
		}
		else if(D[X[i]] == 2 && A[X[i]] < stp[0]){
			printf("%lld\n",D[X[i]] == 1 ? A[X[i]]+T : A[X[i]]-T);
		}
		else if (D[X[i]] == 1 && A[X[i]] > stp[stp.size()-1]){
			printf("%lld\n",D[X[i]] == 1 ? A[X[i]]+T : A[X[i]]-T);
		}
		else{
			vector<long long>::iterator itr = lower_bound(stp.begin(),stp.end(),A[X[i]]);	
			int east = distance(stp.begin(),itr);
			int west = east-1;
			if(D[X[i]] == 1){ // east
				printf("%lld\n",min(stp[east],A[X[i]]+T));
			}
			else{ // west
				printf("%lld\n",max(stp[west],A[X[i]]-T));
			}
		}
	}

	return 0;
}

C++: #include<stdio.h>


int main() {
	int N, Q, flag = 0;
	long long int T,A[100000], D[100000], X[100000];
	scanf("%d %lld %d", &N, &T, &Q);
	for (int i = 1; i <= N; i++) {
		scanf("%lld %lld", &A[i], &D[i]);
	}
	for (int i = 1; i <= Q; i++) {
		scanf("%lld", &X[i]);
	}
	for (int i = 1; i <= Q; i++) {
		if (D[X[i]] == 1) {
			for (int j = X[i]+1; j <= N;j++) {
				if (D[j] == 2) {
					if ((A[j-1] + A[j]) / 2 <= A[X[i]] + T) {
						printf("%lld\n", (A[j-1] + A[j]) / 2);
						flag = 1;
					}
					break;
				}
			}
			if (flag == 0) {
				printf("%lld\n", A[X[i]] + T);
			}
		}
		if (D[X[i]] == 2) {
			for (int j = X[i] -1; j >=1; j--) {
				if (D[j] == 1) {
					if ((A[j+1] + A[j]) / 2 >= A[X[i]] - T) {
						printf("%lld\n", (A[j+1] + A[j]) / 2);
						flag = 1;
					}
					break;
				}
			}
			if (flag == 0) {
				printf("%lld\n", A[X[i]] - T);
			}
	    }
		flag = 0;
    }
	return 0;
}