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 int64;
typedef pair< int64, int64 > Pi;
const int64 INF = 3000000000000000000LL;
    
int64 N, T, Q;
vector< Pi > data;
int64 dp[100002];
    
int64 rec(int idx, const int d)
{
  if(~dp[idx]) {
    return(dp[idx]);
  } else if(data[idx].second != data[idx + d].second) {
    return(dp[idx] = (data[idx].first + data[idx + d].first) / 2LL);
  } else {
    return(dp[idx] = rec(idx + d, d));
  }
}
    
int main()
{
  memset(dp, -1, sizeof(dp));
  scanf("%lld %lld %lld", &N, &T, &Q);
  data.push_back(Pi(-INF, 1));
  for(int i = 0; i < N; i++) {
    int64 A, D;
    scanf("%lld %lld", &A, &D);
    data.push_back(Pi(A, D));
  }
  data.push_back(Pi(INF, 2));
  while(Q--) {
    int64 X;
    scanf("%lld", &X);
    if(data[X].second == 1) printf("%lld\n", min(data[X].first + T, rec(X, +1)));
    else                    printf("%lld\n", max(data[X].first - T, rec(X, -1)));
  }
}

C++: //Walking in JOI Kingdom
#include <iostream>

using namespace std;
typedef long long ll;

static const int MAX = 100;

enum DIRECTION {
    EAST = 1,
    WEST = 2
};

struct House {
    ll a;
    int d;
    House() {}
    House(ll a, int d): a(a), d(d) {}
};

int N, Q;
ll T;
House H[MAX + 1];

ll trace(int target) {
    if(H[target].d == EAST) {
        if(target == N) return H[target].a + T;
        int neighbor = target + 1;
        if(H[neighbor].d == EAST) return min(H[target].a + T, trace(neighbor));
        else return min(H[target].a + T, (H[target].a + H[neighbor].a) / 2);
    } else if(H[target].d == WEST) {
        if(target == 1) return H[target].a - T;
        int neighbor = target - 1;
        if(H[neighbor].d == WEST) return max(H[target].a - T, trace(neighbor));
        else return max(H[target].a - T, (H[target].a + H[neighbor].a) / 2);
    }
    return 0;
}

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

    ll a;
    int d;
    for(int i = 1; i < N + 1; i++) {
        cin >> a >> d;
        H[i] = House(a, d);
    }

    int target;
    for(int i = 0; i < Q; i++) {
        cin >> target;
        cout << trace(target) << endl;
    }
}

C++: #include <iostream>
using namespace std;
#define M 100000


long long int n,t,q;
long long int a[M],d[M];
long long int x;
long long int m[M]={0};

int meet(int i){
    for (int j=i+1; j<n; j++) {
        if(d[j]==2){
            m[i]=(a[j]+a[j-1])/2;
            return m[i];
        }
    }
    m[i]=a[i]+t;
    return m[i];
}

void output(){
    for (int i=0; i<q; i++) {
        cin>>x;
        x--;
        if(d[x]==1){
            if(a[x]+t<=m[x])cout<<a[x]+t<<endl;
            else cout<<m[x]<<endl;
        }
        else{
            if(a[x]-t>=m[x])cout<<a[x]-t<<endl;
            else cout<<m[x]<<endl;
        }
    }
}

int main(){
    cin>>n>>t>>q;
    for (int i=0; i<n; i++)cin>>a[i]>>d[i];
    long long int tt=a[0]-t;
    for (int i=0; i<n-1; i++) {
        if(d[i]==1)tt=meet(i);
        if(d[i]==2)m[i]=tt;
    }
    if(d[n-1]==1)m[n-1]=a[n-1]+t;
    else m[n-1]=tt;
    output();
    return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <stack>
#define ll long long
using namespace std;
int main() {
	cin.tie(0); ios::sync_with_stdio(false);
	ll N, T, Q; cin >> N >> T >> Q;
	vector <pair<int, int>> A(N + 1);
	for (int i = 1; i <= N; i++) {
		cin >> A[i].first >> A[i].second;
		if (A[i].second == 2) A[i].second = -1;
	}
	vector<int> q(Q);
	for (int i = 0; i < Q;i++) cin >> q[i];


	vector<ll> ans(N + 1);
	int f = 0;
	ll l;
	ll mid;
	for (int i = 1; i <= N;i++) {
		if (f == 0 && A[i].second == -1) {
			ans[i] = A[i].first + A[i].second*T;
			continue;
		}
		if (f == 0 && A[i].second == 1) {
			f = 1; l = i;
			continue;
		}
		if (f == 1 && A[i].second == 1) continue;
		if (f == 1 && A[i].second == -1) {
			mid = (A[i].first + A[i - 1].first) / 2;
			ans[i] = max(mid, A[i].first + A[i].second * T);

			for (int at = l; at < i; at++) {
				ans[at] = min(mid, A[at].first + A[at].second*T);
			}
			f = 2;
			continue;
		}
		if (f == 2 && A[i].second == -1) {
			ans[i] = max(mid, A[i].first + A[i].second*T);
			continue;
		}
		if (f == 2 && A[i].second == 1) {
			f = 1; l = i;
			continue;
		}
	}
	if (f == 1) ans[N] = A[N].first + A[N].second*T;

	for (int i = 0; i < Q;i++) {
		cout << ans[q[i]] << endl;
	}
}