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 <algorithm>
#include <map>
#include <vector>
#include <tuple>

#define int long long

using namespace std;

class Solver {
};
signed main() {
	int n,t, q;
	cin >> n >> t >> q;

	vector<tuple<int, int>> people;
	vector<tuple<int, int>> people_sub;
	for (int i = 0; i < n; i++) {
		int a,d;
		cin >> a >> d;
		people.emplace_back(make_tuple(a, d));
		people_sub.emplace_back(make_tuple(a, d));
	}
	sort(people.begin(), people.end());

	vector<int> wall;
	for (int i = 1; i < n; i++) {
		int d1 = get<1>(people[i - 1]);
		int d2 = get<1>(people[i]);
		if (d1 == 1 && d2 == 2) {
			int pos = (get<0>(people[i - 1]) + get<0>(people[i])) / 2;
			wall.emplace_back(pos);
		}
	}

	for (int i = 0; i < q; i++) {
		int index;
		cin >> index;
		index--;
		int pos = get<0>(people_sub[index]);
		int dire = get<1>(people_sub[index]);

		if (dire == 1) {
			int wall_pos = 3e18;
			for (int j = 0; j < wall.size(); j++) {
				if (pos <= wall[j]) {
					wall_pos = wall[j];
					break;
				}
			}
			cout << min(pos + t, wall_pos) << endl;
		}
		else {
			int wall_pos = -3e18;
			int size = wall.size();
			for (int j = size - 1; j >= 0; j--) {
				if (wall[j] <= pos) {
					wall_pos = wall[j];
					break;
				}
			}
			cout << max(pos - t, wall_pos) << endl;
		}
	}
}

C++: #include <stdio.h>
#include <algorithm>

long long int N, T, Q;
long long int A[112345], D[112345];
long long int X[1123];
long long int t[112345];

int main(void) {
	scanf("%lld%lld%lld", &N, &T, &Q);
	for (long long int i = 0; i < N; ++i) { scanf("%lld%lld", &A[i], &D[i]); }
	for (long long int i = 0; i < Q; ++i) { scanf("%lld", &X[i]); }

	for (long long int i = 0; i < N - 1; ++i) {
		if (D[i] == 1 && D[i + 1] == 2) {
			long long int t_mem = (A[i] + A[i + 1]) / 2;
			t[i] = t_mem;
			t[i + 1] = t_mem;
			for (long long int j = i - 1; j >= 0; --j) {
				if (D[j] == 1) {
					t[j] = t_mem;
				}
				else { break; }
			}
			for (long long int j = i + 1; j < N; ++j) {
				if (D[j] == 2) {
					t[j] = t_mem;
				}
				else { break; }
			}
		}
	}

	for (long long int i = 0; i < N; ++i) {
		if(T<std::abs(A[i]-t[i])){
			if (D[i] == 1) {
				t[i] = A[i] + T;
			}
			if (D[i] == 2) {
				t[i] = A[i] - T;
			}
		}
	}

	for (long long int i = 0; i < Q; ++i) {
		printf("%lld\n", t[X[i] - 1]);
	}

	return 0;
}

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

typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,int> plli;
typedef pair<int,pii> pipii;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<pii> vpii;

#define rep(i,n) for (int i=0;i<(n);i++)
#define rep2(i,a,b) for (int i=(a);i<(b);i++)
#define rrep(i,n) for (int i=(n);i>0;i--)
#define rrep2(i,a,b) for (int i=(a);i>b;i--)
#define pb push_back
#define fi first
#define se second
#define all(a) (a).begin(),(a).end()

const ll mod = 1e9 + 7;
const ll INF = 1<<30;
const int dx4[4] = {1, 0, -1, 0};
const int dy4[4] = {0, 1, 0, -1};
const int dx8[8] = {1, 1, 1, 0, 0, -1, -1, -1};
const int dy8[8] = {0, 1, -1, 1, -1, 0, 1, -1};
const double pi = 3.141592653589793;

int n, t, q;
vector<int> east, west;
pair<int, int> ad[100000 + 5];

signed main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    cin >> n >> t >> q;
    rep(i, n) {
        cin >> ad[i].fi >> ad[i].se;
        if (ad[i].se == 1) east.pb(ad[i].fi);
        else west.pb(ad[i].fi);
    }
    rep(i, q) {
        int x;
        cin >> x;
        x--;
        int a = ad[x].fi;
        int d = ad[x].se;
        if (d == 1) {
            auto itr_w = lower_bound(all(west), a);
            int dist_w = distance(west.begin(), itr_w);
            if (dist_w == west.size()) {
                cout << a + t << endl;
                continue;
            }
            auto itr_e = lower_bound(all(east), *itr_w);
            itr_e--;
            cout << min(a + t, (*itr_e + *itr_w) / 2LL) << endl;
        }
        else {
            auto itr_e = lower_bound(all(east), a);
            int dist_e = distance(east.begin(), itr_e);
            if (dist_e == 0) {
                cout << a - t << endl;
                continue;
            }
            itr_e--;
            auto itr_w = lower_bound(all(west), *itr_e);
            cout << max(a - t, (*itr_e + *itr_w) / 2LL) << endl;
        }
    }

}

C++: #define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <set>
#include <map>
#include <vector>
#include <math.h>
#include <queue>
#include <stack>
#include <list>
#include <functional>

using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)

int N, Q;
long long int T, A[112345];
int D[112345];
int X[1123];

int main() {

	scanf("%d %lld %d", &N, &T, &Q);

	vector<int> point;
	int prevD = 0;
	rep(i, N) {
		scanf("%lld %d", &A[i], &D[i]);
		if (prevD == 1 && D[i] == 2) point.push_back(i - 1);
		prevD = D[i];
	}
	rep(i, Q) scanf("%d", &X[i]);

	int x = 0;
	int count = -1;
	prevD = 0;
	rep(i, N) {
		if (prevD != 1 && D[i] == 1) count++;
		prevD = D[i];
		if (i == X[x] - 1) {
			if (count == -1) printf("%lld\n", A[i] - T);
			else if (count >= point.size()) printf("%lld\n", A[i] + T);
			else if (D[i] == 1) printf("%lld\n", min((A[point[count]] + A[point[count] + 1]) / 2, A[i] + T));
			else printf("%lld\n", max((A[point[count]] + A[point[count] + 1]) / 2, A[i] - T));
			x++;
			if (x >= Q) break;
		}
	}

	return 0;
}