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>
#define range(i, a, b) for(int i = (a); i < (b); i++)
#define rep(i, a) range(i, 0, a)
using namespace std;

int main() {
    long long n, t, q;
    cin >> n >> t >> q;
    vector <vector <long long>> ad(4);
    vector <long long> a(n), d(n);
    rep (i, n) {
        cin >> a[i] >> d[i], d[i]--;
        ad[d[i]    ].push_back( a[i]);
        ad[d[i] + 2].push_back(-a[i]);
    }
    sort(ad[2].begin(), ad[2].end());
    sort(ad[3].begin(), ad[3].end());
    // 0 := 右向き
    // 1 := 左向き
    // 2 := 右向き符号反転
    // 3 := 左向き符号反転

    rep (i, q) {
        int in;
        cin >> in, in--;
        auto itrA = d[in] == 0 ? lower_bound(ad[1].begin(),  ad[1].end(),  a[in])
                               : lower_bound(ad[2].begin(),  ad[2].end(), -a[in]);

        auto itrB = d[in] == 0 ? upper_bound(ad[2].begin(),  ad[2].end(),  a[in])
                               : upper_bound(ad[3].begin(),  ad[3].end(), -a[in]);

        //auto itrB = lower_bound(ad[d[in]].rbegin(), ad[d[in]].rend(), a[in]);

        if (itrA == ad[d[in] + 1].end()) {
            cout << a[in] + t << endl;
        }
        else if (itrB == ad[d[in]].end()) {
            cout << (d[in] ? a[in] - min(t, (-*itrA - a[in]) / 2)
                           : a[in] + min(t, (*itrA - a[in]) / 2))
                 << endl;
        }
        else {
            //左に進む
            if (d[in]) {
                cout << max(-*itrA + abs(*itrA - *itrB) / 2, a[in] - t) << endl;
            }
            else {
                //yosaso
                cout << min((*itrA + max(a[in], -*itrB)) / 2, a[in] + t) << endl;
            }
        }
        /*
        cout << a[in] << " "
             << (itrA == ad[!d[in]].end() ? "va" : to_string(*itrA)) << " "
             << (itrB == ad[d[in]].end()  ? "va" : to_string(*itrB)) << endl;
             */
        //if (itr == ad[!d[in]].end()) cout << "va" << endl;
        //else if (d[i] == 0) cout << a[in] << " " << *itr << endl;
        //else cout << -a[in] << " " << *itr << endl;
    }
    return 0;
}

C++: #include <stdio.h>
typedef long long ll;

ll max(ll a, ll b) {
  return a > b ? a : b;
}

ll min(ll a, ll b) {
  return a < b ? a : b;
}

int main(void) {
  ll n, t, q, i, j, k;
  scanf("%lld%lld%lld", &n, &t, &q);
  ll a[n], d[n], x[n], now, s = 0, be, bel, l;
  for(i = 0; i < n; ++i) scanf("%lld%lld", &a[i], &d[i]);
  for(i = 0; i < n; ++i) {
    if(d[i] == 1) {
      s = 1;
      be = i;
      if(!i || (i &&  d[i - 1] == 2)) bel = i;
      l = 1;
    } else {
      if(be == i - 1) {
        now = (a[i - 1] + a[i]) / 2;
        for(j = bel; j < i; ++j) x[j] = min(now, a[j] + t);
      } 
     x[i] = max(now, a[i] - t);
      l = 2;
    }
    if(!s) {
      x[i] = a[i] - t;
    }
  }
  if(l == 1) for(i = bel; i < n; ++i) x[i] = a[i] + t;
  for(i = 0; i < q; ++i) {
    int p;
    scanf("%d", &p);
    p--;
    printf("%lld\n", x[p]);
  }
  return 0;
}

C++: #include <cstdio>
#include <algorithm>
using namespace std;
int n, q, c, e, d[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, d + i);
	for (int i = 1; i < n; i++) if (d[i] - d[i - 1] == 1) 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 = x[e] + t * (d[e] * -2 + 3);
		if (d[e] == 1) { if (ptr != c) ret = min(ret, y[ptr]); }
		else if (--ptr != -1) ret = max(ret, y[ptr]);
		printf("%lld\n", ret);
	}
	return 0;
}

C++: //
//  main.cpp

#include <iostream>
using namespace std;
using ll = long long;

int main(){	
	int N,Q;
	ll T;
	cin >> N >> T >> Q;
	
	ll people[1001];
	ll A[100001];
	int D[100001];
	for(int i=0;i<N;i++){
		cin >> A[i] >> D[i];
	}
	for(int i=0;i<Q;i++){
		cin >> people[i];
		people[i]--;
	}
	
	ll east = 0;
	ll west = 0;
	ll t = 0;
	for(int i=0;i<Q;i++){
		if(D[people[i]] == 1){
			east = A[people[i]];
			for(ll j=people[i]+1;j<N;j++){
				if(D[j] == 1){
					east = A[j];
				}else{
					t = (east + A[j])/2;
					break;
				}
			}
			if(T < abs(t - A[people[i]])){
				cout << A[people[i]] + T << endl;
			}else{
				cout << t << endl;
			}
		}else{
			west = A[people[i]];
			for(ll j=people[i]-1;j>=0;j--){
				if(D[j] == 1){
					t = (west + A[j])/2;
					break;
				}else{
					west = A[j];
				}
			}
			if(T < abs(A[people[i]] - t)){
				cout << A[people[i]] - T << endl;
			}else{
				cout << t << endl;
			}
		}
	}

	return 0;
}