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;

int main(void){
    long long n,q;
    long long t;
    cin>>n>>t>>q;
    vector<pair<long long,int> >c(n);
    vector<long long> ans;
    for (int i = 0; i < n; ++i) {
        cin>>c[i].first>>c[i].second;
        if(c[i].second==2)c[i].second=-1;
    }
    for (int i = 0; i < n-1; ++i) {
        if(c[i].second==1)if(c[i].second!=c[i+1].second)ans.push_back((c[i].first+c[i+1].first)/2);
    }
    for (int i = 0; i < q; ++i) {
        int a;
        cin>>a;
        a=a-1;
        if(c[a].second==1) {
            for (int j = 0; j < ans.size(); ++j) {
                    if (c[a].first < ans[j]) {
                        if (abs(ans[j] - c[a].first) < t) {
                            cout << ans[j] << endl;
                            break;
                        }
                        else {
                            long long an = t * c[a].second + c[a].first;
                            cout << an << endl;
                            break;
                        }
                    }
                    if(j==ans.size()-1){
                    long long an = t * c[a].second + c[a].first;
                    cout << an << endl;
                    break;
                }

            }
        }
        else{
            for (int j = ans.size()-1; j >= 0; --j) {
                if (c[a].first > ans[j]) {
                    if (abs(ans[j] - c[a].first) < t) {
                        cout << ans[j] << endl;
                        break;
                    } else {
                        long long an = t * c[a].second + c[a].first;
                        cout << an << endl;
                        break;
                    }
                }
                if(j==0){
                    long long an = t * c[a].second + c[a].first;
                    cout << an << endl;
                    break;
                }
            }
        }
    }
    return 0;
}

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

using ll = long long;

int main() {
    ll N, T, Q; cin >> N >> T >> Q;
    vector<ll> A(N), D(N);
    for(int i = 0; i < N; ++i) {
        cin >> A[i] >> D[i];
    }

    vector<ll> ans(N);
    deque<int> deq;
    int i = 0;
    while(i < N) {
        if(D[i] == 2) { // west
            if(!deq.empty() && D[deq.back()] == 1) {
                ll mid = (A[deq.back()] + A[i]) / 2;
                while(i < N && D[i] == 2) {
                    deq.push_back(i++);
                }
                for(auto j : deq) {
                    ans[j] = (abs(mid - A[j]) <= T ? mid : A[j] + (D[j] == 2 ? -T : T));
                }
                deq.clear();
            } else {
                ans[i] = A[i] - T;
                i++;
            }
        } else {
            deq.push_back(i++);
        }
    }
    for(auto j : deq) {
        ans[j] = A[j] + T;
    }

    for(int i = 0; i < Q; ++i) {
        int X; cin >> X;
        cout << ans[X - 1] << endl;
    }
}

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

constexpr int MAX_N = 100000;
constexpr long long MAX_T = 1000000000000000000;

long long N, T, Q;

long long pos[MAX_N], target[MAX_N];
int dir[MAX_N];
std::vector<long long> stops;

void solve();

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

	std::cin >> N >> T >> Q;

	for(int i = 0; i < N; ++i){
		std::cin >> pos[i] >> dir[i];
	}

	for(int i = 0; i < N - 1; ++i){
		if(dir[i] == 1 && dir[i + 1] == 2 && (pos[i + 1] - pos[i]) <= (T * 2)){
			stops.push_back((pos[i + 1] + pos[i]) / 2);
		}
	}

	for(int i = 0; i < Q; ++i){
		int input;
		std::cin >> input;
		--input;

		long long res = MAX_T;
		if(dir[input] == 1){ //When a citizen heads east.
			auto itr = std::lower_bound(stops.begin(), stops.end(), pos[input]);
			if(itr != stops.end() && *itr <= pos[input] + T){
				res = *itr;
			}
		}
		else{ //When a citizen heade west.
			auto itr = std::lower_bound(stops.begin(), stops.end(), pos[input] - T);
			while(itr != stops.end() && *itr <= pos[input]){
				res = *itr;
				++itr;
			}
		}

		if(res == MAX_T){
			if(dir[input] == 1){
				std::cout << pos[input] + T << std::endl;
			}
			else{
				std::cout << pos[input] - T << std::endl;
			}
		}
		else{
			std::cout << res <<  std::endl;
		}
	}

	return 0;
}

C++: #include<iostream>
#include<algorithm>
using namespace std;
typedef long long int ll;
static const int MAX_N = 100000;

int N; ll T; int Q;
ll A[MAX_N]; int D[MAX_N];
ll ans[MAX_N];

int main(){
	cin >> N >> T >> Q;
	for(int i = 0; i < N; i++) cin >> A[i] >> D[i];
	if(D[0] == 2) ans[0] = A[0] - T;
	if(D[N - 1] == 1) ans[N - 1] = A[N - 1] + T;
	for(int i = 0; i < N - 1; i++){
		if(D[i] == 1 && D[i + 1] == 2){
			ans[i] = min((A[i] + A[i + 1]) / 2, A[i] + T);
			ans[i + 1] = min((A[i] + A[i + 1]) / 2, A[i + 1] - T);
		}
	}
	for(int i = 1; i < N; i++){
		if(D[i] == 2) ans[i] = max(ans[i - 1], A[i] - T);
	}
	for(int i = N - 2; i >= 0; i--){
		if(D[i] == 1) ans[i] = min(ans[i + 1], A[i] + T);
	}
	for(int i = 0; i < Q; i++){
		int x;
		cin >> x;
		x--;
		cout << ans[x] << endl;
	}
	return 0;
}