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 <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <algorithm>
#include <utility>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <functional>

using namespace std;

#define int long long

#define fst first
#define scd second
#define PB push_back
#define MP make_pair
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define omajinai ios::sync_with_stdio(false);cin.tie(0)
#define rep(i,x) for(int i=0;i<(int)(x);++i)
#define rep1(i,x) for(int i=1;i<=(int)(x);++i)
#define rrep(i,x) for(int i=(int)x-1;i>=0;--i)
#define rrep1(i,x) for(int i=(int)x;i>=1;--i)

using ll=long long;
using ld=long double;
using vi=vector<int>;
using vvi=vector<vi>;
using pii=pair<int, int>;
using vpii=vector<pii>;

template<class T,class U>ostream&operator<<(ostream&os,const pair<T,U>p){os<<"("<<p.fst<<", "<<p.scd<<")";return os;}
template<class T>ostream&operator<<(ostream&os,const vector<T>v){rep(i,v.size()){if(i)os<<", ";os<<v[i];}return os;}
template<typename T>T&max(T&a,T&b){if(a>=b)return a;return b;}
template<typename T>T&min(T&a,T&b){if(a < b)return a;return b;}
template<typename T>bool chmax(T&a,T b){if(a < b){a=b;return true;}return false;}
template<typename T>bool chmin(T&a,T b){if(a > b){a=b;return true;}return false;}

constexpr int inf = 3e18;
constexpr double eps = 1e-9;

int N, T, Q;
int A[100010];
int D[100010];

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

    rep(i, N) {
        cin >> A[i] >> D[i];
    }

    vi kabe;

    kabe.PB(-inf);

    rep(i, N - 1) {
        if (D[i] == 1 && D[i + 1] == 2) {
            kabe.PB((A[i] + A[i + 1]) / 2);
        }
    }

    kabe.PB(inf);

    rep(i, Q) {
        int x; cin >> x;
        --x;

        if (D[x] == 1) {
            // ??±???????????????
            int val = *lower_bound(all(kabe), A[x]);
            if (val == inf) {
                cout << A[x] + T << endl;
            } else {
                cout << min(A[x] + T, val) << endl;
            }
        } else {
            int val = *(upper_bound(all(kabe), A[x]) - 1);

            if (val == -inf) {
                cout << A[x] - T << endl;
            } else {
                cout << max(A[x] - T, val) << endl;
            }
        }
    }
}

C++: #include <stdio.h>
#include <string.h>
#include <algorithm>
long long int home[12345678];
long long int muki[12345678];
long long int important[123456];
long long int hit[12345678];
long long int ans[12345678];
int main(){
	long long int num;
	long long int time;
	long long int imp_num;
	long long int hit_num = 0;
	scanf("%lld%lld%lld", &num, &time, &imp_num);
	for (long long int a = 0; a < num; a++){
		scanf("%lld%lld", &home[a], &muki[a]);
	}
	for (int a = 0; a < imp_num; a++){
		scanf("%lld", &important[a]);
	}
	for (long long int a = 0; a < num - 1; a++){
		if (muki[a] == 1 &&
			muki[a + 1] == 2){
			hit[hit_num] = (home[a + 1] - home[a]) / 2 + home[a];
			hit_num++;
		}
	}
	for (long long int a = 0; a < num; a++){
		if (muki[a] == 1){
			home[a] += time;
			for (long long int b = 0; b < hit_num; b++){
				if (home[a] >= hit[b] &&
					home[a] - time <= hit[b]){
					home[a] = hit[b];
					break;
				}
			}
		}
		else{
			home[a] -= time;
			for (long long int b = hit_num - 1; 0 <= b; b--){
				if (home[a] <= hit[b] &&
					home[a] + time >= hit[b]){
					home[a] = hit[b];
					break;
				}
			}
		}
	}
	for (int a = 0; a < imp_num; a++){
		printf("%lld\n", home[important[a] - 1]);
	}
	return 0;
}

C++: #include <iostream>
#include <vector>
#define INF 2e+18
#define int long long
using namespace std;

signed main(){
	int n,t,q,a[100000],d[100000],stop[100000];
	vector<int> vec;
	cin >> n >> t >> q;
	for(int i = 0;i < n;i++){
		cin >> a[i] >> d[i];
		if(d[i] == 2) d[i] = -1;
		stop[i] = INF;
	}
	for(int i = 0;i < n - 1;i++){
		if(d[i] != d[i + 1]) vec.push_back((a[i] + a[i + 1]) / 2);
	}
	int pos = vec.size() - 1;
	for(int i = n - 1;i >= 0;i--){
		if(d[i] == 1) continue;
		while(pos >= 0){
			if(vec[pos] < a[i]){
				stop[i] = a[i] - vec[pos];
				break;
			}
			pos--;
		}
	}
	pos = 0;
	for(int i = 0;i < n;i++){
		if(d[i] == -1) continue;
		while(pos < vec.size()){
			if(vec[pos] > a[i]){
				stop[i] = vec[pos] - a[i];
				break;
			}
			pos++;
		}
	}
	for(int i = 0;i < q;i++){
		int p;
		cin >> p; p--;
		cout << a[p] + min(t,stop[p]) * d[p] << endl;
	}
	return 0;
}