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 <algorithm>
#pragma warning(disable : 4996)
#define INF 8108931145141919810
using namespace std;
int n, q, c, e, dir[111111]; long long t, x[111111], y[111111];
int main() {
	scanf("%d%lld%d", &n, &t, &q);
	for (int i = 0; i < n; i++) scanf("%lld%d", x + i, dir + i);
	for (int i = 1; i < n; i++) {
		if (dir[i - 1] == 1 && dir[i] == 2) y[c++] = (x[i - 1] + x[i]) / 2;
	}
	for (int i = 0; i < q; i++) {
		scanf("%d", &e); e--;
		int ptr = lower_bound(y, y + c, x[e]) - y;
		long long ret;
		if (dir[e] == 1) {
			ret = x[e] + t;
			if (ptr != c) ret = min(ret, y[ptr]);
		}
		else {
			ret = x[e] - t; ptr--;
			if (ptr != -1) ret = max(ret, y[ptr]);
		}
		printf("%lld\n", ret);
	}
	return 0;
}

C++: #include "bits/stdc++.h"
#include<unordered_map>
#include<unordered_set>
#pragma warning(disable:4996)
using namespace std;
using ld = long double;
template<class T>
using Table = vector<vector<T>>;
const ld eps=1e-9;

//// < "D:\D_Download\Visual Studio 2015\Projects\programing_contest_c++\Debug\a.txt" > "D:\D_Download\Visual Studio 2015\Projects\programing_contest_c++\Debug\b.answer"
vector<long long int>xs;
vector<int>islefts;
vector<long long int>anss;
vector<int>oks;

int N, T, Q;
long long int getans(const int q) {
	if (oks[q])return anss[q];
	else {
		oks[q] = true;
		if (islefts[q]) {
			if (q == 0) {
				return anss[q] = xs[q] - T;
			}
			else {
				long long int leftx = getans(q - 1);
				return anss[q] = max(leftx, xs[q] - T);
			}
		}
		else {
			if (q == N - 1) {
				return anss[q] = xs[q] + T;
			}
			else {
				long long int rightx = getans(q + 1);
				return anss[q] = min(rightx, xs[q] + T);
			}
		}
	}
}

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

	anss.resize(N);
	for (int i = 0; i < N; ++i) {
		long long int a,isleft; cin >> a >> isleft;
		isleft--;
		xs.push_back(a); islefts.push_back(isleft);
		oks.push_back(false);
	}
	for (int i = 0; i < N-1; ++i) {
		if (!islefts[i] && islefts[i + 1]) {
			long long int dis = xs[i + 1] - xs[i];
			oks[i] = true; oks[i + 1] = true;
			if (dis < 2 * T) {
				anss[i] = xs[i] + dis / 2;
				anss[i + 1] = xs[i + 1] - dis / 2;
			}
			else {
				anss[i] = xs[i] + T;
				anss[i + 1] = xs[i + 1] - T;
			}
		}
	}
	while (Q--) {
		int q; cin >> q; q--;
		long long int ans = getans(q);
		cout << ans << endl;
	}
	
	return 0;
}

C++: #include <iostream>

constexpr int MAX_N = (int)1e5, MAX_Q = (int)1e3;

using ll = long long int;

int n, q;
ll t;
ll a[MAX_N], d[MAX_N];

int find_l(int first, int last) {
	for (int i = first; i >= last; --i) {
		if (d[i] == 1)
			return i;
	}
	return -1;
}

int find_r(int first, int last) {
	for (int i = first; i <= last; ++i) {
		if (d[i] == 2)
			return i;
	}
	return -1;
}

int main() {
	std::cin >> n >> t >> q;
	for (int i = 0; i < n; ++i)
		std::cin >> a[i] >> d[i];
	int x;
	for (int i = 0; i < q; ++i) {
		std::cin >> x;
		--x;
		ll ans;
		if (d[x] == 2) {
			if (x == 0)
				ans = a[x] - t;
			else {
				int res = find_l(x - 1, 0);
				if (res == -1)
					ans = a[x] - t;
				else {
					ans = (a[res + 1] + a[res]) / 2;
					if (a[x] - ans > t)
						ans = a[x] - t;
				}
			}
		}
		else {
			if (x == n - 1)
				ans = a[x] + t;
			else {
				int res = find_r(x + 1, n - 1);
				if (res == -1)
					ans = a[x] + t;
				else {
					ans = (a[res] + a[res - 1]) / 2;
					if (ans - a[x] > t)
						ans = a[x] + t;
				}
			}
		}
		std::cout << ans << std::endl;
	}
	return 0;
}

C++: #include <stdio.h>
long long dat[100010];
long long vec[100010][2];
int main()
{
    int num,query,i,j,z,zb;
    long long t,tt,za;
    i=0;
    scanf("%d%lld%d",&num,&t,&query);
    vec[0][0]=-3000000000000000000LL;
    vec[0][1]=1;
    for(;i<num;i++){
        scanf("%lld%d",&za,&zb);
        vec[i+1][0]=za;
        vec[i+1][1]=zb;
    }
    vec[num+1][0]=3000000000000000000LL;
    vec[num+1][1]=2;
    i=0;
    for(;i<=num;i++){
        if(vec[i][1]==1&&vec[i+1][1]==2){
            tt=(vec[i][0]+vec[i+1][0])/2;
            j=i;
            for(;j>=0;j--){
                if(vec[j][1]==1)dat[j]=tt;
                else break;
            }
            j=i+1;
            for(;j<num+2;j++){
                if(vec[j][1]==2)dat[j]=tt;
                else break;
            }
        }
    }
    i=0;
    for(;i<query;i++){
        scanf("%d",&z);
        if(vec[z][1]==1){
            printf("%lld\n",(vec[z][0]+t<dat[z])?(vec[z][0]+t):(dat[z]));
        }else{
            printf("%lld\n",(vec[z][0]-t<dat[z])?(dat[z]):(vec[z][0]-t));
        }
    }
    return 0;
}

C++: #include <bits/stdc++.h>
#define ll long long
#define rep(i,n) for(int i=0;i<n;i++)
#define INF 100000005
#define MAX 5001
#define fi first
#define se second
using namespace std;
//__gcd(a,b), __builtin_popcount(a);

pair<ll, int> p[100002];
ll x[1002], d[100002];
int main(){
	ll n, T, Q, t1, t2;
	scanf("%lld%lld%lld", &n, &T, &Q);
	for(int i = 0;i < n;i++)scanf("%lld%d", &p[i].fi, &p[i].se);
	for(int i = 0;i < Q;i++)scanf("%lld", &x[i]);
	
	ll f[2] = {0, 0}, tt;
	for(int i = 0;i < n;i++){
		if(p[i].se == 1 && f[0] == 0)f[0] = 1;
		if(p[i].se == 2 && f[0] == 1){
			f[1] = (p[i-1].fi+p[i].fi)/2;
			tt = i-1;
			while(tt != -1 && p[tt].se == 1)d[tt--] = f[1];
			tt = i;
			while(tt < n && p[tt].se == 2)d[tt++] = f[1];
			i = tt;
		}
		if(i == n-1){
			tt = i;
			while(tt != -1 && p[tt].se == 1)d[tt--] = p[i].fi+T;
		}
		if(p[i].se == 2 && f[0] == 0)d[i] = p[i].fi-T;
	}
	
	ll tmp;
	for(int i = 0;i < Q;i++){
		tmp = x[i]-1;
		if(p[tmp].fi < d[tmp])d[tmp] = min(p[tmp].fi+T, d[tmp]);
		if(p[tmp].fi > d[tmp])d[tmp] = max(p[tmp].fi-T, d[tmp]);
		printf("%lld\n", d[tmp]);
	}
	return 0;
}