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;

#define int long long
#define PB push_back
#define PPB pop_back
#define ALL(X) (X).begin(),(X).end()
typedef pair<int, int> pii;

static const int INF = 1LL << 61;
static const int MAX_N = 100005;

int n, t, A[MAX_N], D[MAX_N], pos[MAX_N], q, X[1005];
int stop[MAX_N];

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

    fill(stop, stop + MAX_N, INF);
    cin >> n >> t >> q;
    for (int i = 1; i <= n; ++i) {
        cin >> A[i] >> D[i];
    }
    for (int i = 0; i < q; ++i) {
        cin >> X[i];
    }

    for (int i = 2; i <= n; ++i) {
        if (D[i - 1] < D[i] && (A[i] - A[i - 1]) < 2 * t) {
            int temp = (A[i - 1] + A[i]) / 2;
            stop[i - 1] = stop[i] = temp;
        }
    }

    for (int i = 1; i <= n; ++i) {
        if (stop[i] != INF && D[i] == 2) {
            int pos = stop[i];

            for (i = i + 1; D[i] == 2; ++i) {
                if (pos < A[i] - t) {
                    stop[i] = A[i] - t;
                } else {
                    stop[i] = pos;
                }
            }
        }
    }
    for (int i = n; 0 < i; --i) {
        if (stop[i] != INF && D[i] == 1) {
            int pos = stop[i];

            for (i = i - 1; D[i] == 1; --i) {
                if (A[i] + t < pos) {
                    stop[i] = A[i] + t;
                } else {
                    stop[i] = pos;
                }
            }
        }
    }

    for (int i = 0; i < q; ++i) {
        int tmp = X[i];
        if (stop[tmp] != INF) {
            cout << stop[tmp] << endl;
        } else {
            if (D[tmp] == 1) {
                cout << A[tmp] + t << endl;
            } else {
                cout << A[tmp] - t << endl;
            }
        }
    }
}

C++: #include <cstdio>
#include <vector>
#include <cstdlib>
#include <algorithm>
using namespace std;

int n, t, q;
long long int a[100000];
int d[100000];
int x[100000];

vector<long long int> v;

int main(){
	scanf("%d %d %d", &n, &t, &q);
	for(int i=0; i<n; i++) scanf("%lld %d", &a[i], &d[i]);
	for(int i=0; i<q; i++) scanf("%d", &x[i]);

	bool f = false;
	long long int p;
	for(int i=0; i<n; i++){
		if(d[i] == 1){
			p = a[i];
			f = true;
		}else if(f){
			v.push_back(p / 2 + a[i] / 2);
			f = false;
		}
	}

	long long int s;
	for(int i=0; i<q; i++){
		if(d[x[i]-1] == 1){
			s = *upper_bound(v.begin(), v.end(), a[x[i]-1]);
			if(a[x[i]-1] < s && s - a[x[i]-1] < t) printf("%lld\n", s);
			else printf("%lld\n", a[x[i]-1] + t);
		}else{
			s = *(lower_bound(v.begin(), v.end(), a[x[i]-1])-1);
			if(s < a[x[i]-1] && a[x[i]-1] - s < t) printf("%lld\n", s);
			else printf("%lld\n", a[x[i]-1] - t);
		}
	}
/*
	printf("\n");
	for(int i=0; i<v.size(); i++)
		printf("%lld\n", v[i]);
*/	return 0;
}

C++: #include<cstdio>
#include<algorithm>
#include<functional>
#include<cstring>
#include<iostream>
using namespace std;
typedef long long int LL;
int main(void)
{
	LL N,T,Q,house[100001],muki[100001];
	LL X[1001];
	LL list[100001];
	LL i,j;
	cin>>N>>T>>Q;
	for(i=1;i<=N;i++) cin>>house[i]>>muki[i];
	for(i=1;i<=Q;i++) cin>>X[i];
	for(i=N;i>=1;i--) {
		if(muki[i]==2) continue;
		if(i==N) list[i]=house[i]+T;
		else if(muki[i+1]==2) list[i]=min(house[i]+T,(house[i]+house[i+1])/2);
		else list[i]=min(house[i]+T,list[i+1]);
	}
	for(i=1;i<=N;i++) {
		if(muki[i]==1) continue;
		if(i==1) list[i]=house[i]-T;
		else if(muki[i-1]==1) list[i]=max(house[i]-T,(house[i]+house[i-1])/2);
		else list[i]=max(house[i]-T,list[i-1]);
	}
	for(i=1;i<=Q;i++) {
		cout<<list[X[i]]<<endl;
	}
	return 0;
}

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

#define int long long
#define PB push_back
#define PPB pop_back
#define ALL(X) (X).begin(),(X).end()
typedef pair<int, int> pii;

static const int INF = 1LL << 61;
static const int MAX_N = 100005;

int n, t, A[MAX_N], D[MAX_N], pos[MAX_N], q, X[1005];
int stop[MAX_N];

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

    fill(stop, stop + MAX_N, INF);
    cin >> n >> t >> q;
    for (int i = 1; i <= n; ++i) {
        cin >> A[i] >> D[i];
    }
    for (int i = 0; i < q; ++i) {
        cin >> X[i];
    }

    for (int i = 2; i <= n; ++i) {
        if (D[i - 1] < D[i] && (A[i] - A[i - 1]) - 2 * t) {
            int temp = (A[i - 1] + A[i]) / 2;
            stop[i - 1] = stop[i] = temp;
        }
    }

    for (int i = 1; i <= n; ++i) {
        if (stop[i] != INF && D[i] == 2) {
            int pos = stop[i];

            for (i = i + 1; D[i] == 2; ++i) {
                if (pos < A[i] - t) {
                    stop[i] = A[i] - t;
                } else {
                    stop[i] = pos;
                }
            }
        }
    }
    for (int i = n; 0 < i; --i) {
        if (stop[i] != INF && D[i] == 1) {
            int pos = stop[i];

            for (i = i - 1; D[i] == 1; --i) {
                if (A[i] + t < pos) {
                    stop[i] = A[i] + t;
                } else {
                    stop[i] = pos;
                }
            }
        }
    }

    for (int i = 0; i < q; ++i) {
        int tmp = X[i];
        if (stop[tmp] != INF) {
            cout << stop[tmp] << endl;
        } else {
            if (D[tmp] == 1) {
                cout << A[tmp] + t << endl;
            } else {
                cout << A[tmp] - t << endl;
            }
        }
    }
}