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 <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef pair<int,int> P;

int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);

	ll n,t,q; cin >> n >> t >> q;
	ll a[100005],d[100005];
	vector<ll> p;
	cin >> a[0] >> d[0];
	for (int i = 1;i < n;i++){
		cin >> a[i] >> d[i];
		if(d[i] != d[i-1]){
			p.push_back((a[i]+a[i-1])/2);
		}
	}

	ll x;

	for (int j = 0;j < q;j++){
		cin >> x; x--;
		bool f = true;
		if(d[x] == 1){
			for (int i = 0;i < p.size();i++){
				if(p[i] > a[x] && p[i] - a[x] <= t){ 
					cout << p[i] << endl;
					f = false;
					break;
				}
			}
			if(f) cout << a[x] + t << endl;
		}else{
			for (int i = p.size()-1;i >= 0; i--){
				if(p[i] < a[x] && a[x] - p[i] <= t){
					cout << p[i] << endl;
					f = false;
					break;
				}
			}
			if(f) cout << a[x] - t << endl;	
		}
	}

	return 0;

}

C++: #include<iostream>
#include<cmath>
#include<cstdio>
using namespace std;
#pragma warning(disable:4996)
long long N, T, Q, a[1 << 17], b[1 << 17], c[1 << 17];
int main() {
	cin >> N >> T >> Q;
	for (int i = 0; i < N; i++)scanf("%lld%lld", &a[i], &b[i]);
	long long H = -1LL << 62;
	for (int i = 0; i < N; i++) {
		if (b[i] == 2) {
			if (i >= 1) {
				if (b[i - 1] == 1) {
					long long C = (a[i - 1] + a[i]) / 2;
					if (abs(C - a[i]) <= T) { H = C; c[i] = C; }
					else { c[i] = a[i] - T; }
				}
				else {
					if (abs(H - a[i]) <= T)c[i] = H;
					else { c[i] = a[i] - T; }
				}
			}
			if (i == 0)c[i] = a[i] - T;
		}
	}
	H = 1LL << 62;
	for (int i = N - 1; i >= 0; i--) {
		if (b[i] == 1) {
			if (i < N - 1) {
				if (b[i + 1] == 2) {
					long long C = (a[i + 1] + a[i]) / 2;
					if (abs(C - a[i]) <= T) { H = C; c[i] = C; }
					else { c[i] = a[i] + T; }
				}
				else {
					if (abs(H - a[i]) <= T)c[i] = H;
					else { c[i] = a[i] + T; }
				}
			}
			if (i == N - 1)c[i] = a[i] + T;
		}
	}
	for (int i = 0; i < Q; i++) {
		int p; scanf("%d", &p); p--; cout << c[p] << endl;
	}
	return 0;
}

C++: #include<stdio.h>


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

C++: #include <iostream>
#include <vector>

using lint = long long;
constexpr lint INF = 1LL << 62;

void solve() {
    int n, q;
    lint t;
    std::cin >> n >> t >> q;

    std::vector<lint> xs(n);
    std::vector<int> ds(n);
    for (int i = 0; i < n; ++i) {
        std::cin >> xs[i] >> ds[i];
    }
    xs.insert(xs.begin(), -INF);
    xs.push_back(INF);
    ds.insert(ds.begin(), 1);
    ds.push_back(2);

    while (q--) {
        int i;
        std::cin >> i;
        if (ds[i] == 1) {
            int j = i;
            while (ds[j + 1] == 1) ++j;
            lint dst = (xs[j] + xs[j + 1]) / 2;
            std::cout << std::min(dst, xs[i] + t) << std::endl;
        } else {
            int j = i;
            while (ds[j - 1] == 2) --j;
            lint dst = (xs[j] + xs[j - 1]) / 2;
            std::cout << std::max(dst, xs[i] - t) << std::endl;
        }
    }
}

int main() {
    std::cin.tie(nullptr);
    std::cout.tie(nullptr);
    std::ios::sync_with_stdio(false);

    solve();

    return 0;
}

C++: # include <iostream>
# include <algorithm>
# include <vector>
# include <string>
# include <set>
# include <cmath>
# include <iomanip>
# include <functional>
# include <utility>
using namespace std;

int main() {
	long long n, t, q;
	cin >> n >> t >> q;
	long long a, d;
	vector<pair<long long, int>> people;
	for (int i = 0; i < n; i++) {
		cin >> a >> d;
		people.emplace_back(a, d);
	}
	for (int i = 0; i < n; i++) {
		if (people[i].second == 1) {
			for (int j = i+1; j < n; j++) {
				if (people[j].second == 0) {
					if (people[j].first - people[i].first <= t) {
						people[i].second = 0;
						people[i].first = people[j].first;
						break;
					}
				}
				if (people[j].second == 2) {
					if (people[j].first - people[i].first <= t) {
						people[i].second = people[j].second = 0;
						people[i].first = people[j].first = (people[i].first + people[j].first) / 2;
						break;
					}
				}
			}
			if (people[i].second != 0) {
				people[i].second = 0;
				people[i].first += t;
			}
		}
		if (people[i].second == 2) {
			for (int j = i-1; j >= 0; j--) {
				if (people[j].second == 0) {
					if (people[i].first - people[j].first <= t) {
						people[i].second = 0;
						people[i].first = people[j].first;
						break;
					}
				}
				if (people[j].second == 1) {
					if (people[i].first - people[j].first <= t) {
						people[i].second = people[j].second = 0;
						people[i].first = people[j].first = (people[i].first + people[j].first) / 2;
						break;
					}
				}
			}
			if (people[i].second != 0) {
				people[i].second = 0;
				people[i].first -= t;
			}
		}
	}
	for (int i = 0; i < q; i++) {
		cin >> a;
		cout << people[a - 1].first << endl;
	}
}