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 <cmath>
#include <queue>

constexpr int MAX_N = 100000;
constexpr long long MAX_T = 1000000000000000000;
constexpr int MAX_Q = 1000;

typedef std::pair<long long, int> P;

int N, Q, target[MAX_Q];
long long T, ans[MAX_N];
P p[MAX_N];

void solve();

int main(){
	scanf("%d %lld %d", &N, &T, &Q);

	for(int i = 0; i < N; ++i){
		scanf("%lld %d", &p[i].first, &p[i].second);
	}
	for(int i = 0; i < Q; ++i){
		scanf("%d", &target[i]);
	}

	solve();
	return 0;
}

void solve(){
	for(int i = 0; i < N - 1; ++i){
		if(p[i].second == 1 && p[i + 1].second == 2 && p[i + 1].first - p[i].first <= T * 2){
			int tmp = ans[i] = ans[i + 1] = (p[i + 1].first + p[i].first) / 2;
			p[i].second = p[i + 1].second = 0;
			for(int j = i - 1; j >= 0; --j){
				if(p[j].second == 1 && tmp - p[j].first <= T){
					p[j].second = 0;
					ans[j] = tmp;
				}
				else{
					break;
				}
			}
			for(int j = i + 2; j < N; ++j){
				if(p[j].second == 2 && p[j].first - tmp <= T){
					p[j].second = 0;
					ans[j] = tmp;
				}
				else{
					break;
				}
			}
		}
	}

	for(int i = 0; i < N; ++i){
		if(ans[i] == 0 && p[i].second != 0){
			if(p[i].second == 1){
				ans[i] = p[i].first + T;
			}
			else{
				ans[i] = p[i].first - T;
			}
		}
	}

	for(int i = 0; i < Q; ++i){
		printf("%lld\n", ans[target[i] - 1]);
	}
}

C++: #include<bits/stdc++.h>
using ll = long long;
using namespace std;
const ll INF = 3000000000000000000;
const ll mod = 1000000007;
//printf("%.10f\n", n);
signed main() {
    ll n, t, q, a; cin >> n >> t >> q;
    vector<vector<ll>> hito(n + 1, vector<ll>(2));
    vector<ll> v; v.push_back(0 - 1 - INF);
    for (int h = 1; h <= n; h++) {
        cin >> hito[h][0] >> hito[h][1];
    }
    for (int h = 2; h <= n; h++) {
        if (hito[h - 1][1] == 1 && hito[h][1] == 2) {
            v.push_back((hito[h - 1][0] + hito[h][0]) / 2);
        }
    }
    v.push_back(INF + 1);
    for (int h = 0; h < q; h++) {
        cin >> a; ll b = hito[a][0], c = lower_bound(v.begin(), v.end(), b) - v.begin();
        if (hito[a][1] == 1) {//右
            cout << min(b + t, v[c]) << endl;
        }
        else {
            cout << max(b - t, v[c - 1]) << endl;
        }
    }
    return 0;
}

C++: #include<stdio.h>


int main() {
	int N, T, Q, flag = 0;
	long int A[100000], D[100000], X[100000];
	scanf("%d %d %d", &N, &T, &Q);
	for (int i = 1; i <= N; i++) {
		scanf("%d %d", &A[i], &D[i]);
	}
	for (int i = 1; i <= Q; i++) {
		scanf("%d", &X[i]);
	}
	for (int i = 1; i <= Q; i++) {
		if (D[X[i]] == 1) {
			for (int j = X[i]+1; j <= N;j++) {
				if (D[j] == 2) {
					if ((A[j-1] + A[j]) / 2 <= A[X[i]] + T) {
						printf("%d\n", (A[j-1] + A[j]) / 2);
						flag = 1;
					}
					break;
				}
			}
			if (flag == 0) {
				printf("%d\n", A[X[i]] + T);
			}
		}
		if (D[X[i]] == 2) {
			for (int j = X[i] -1; j >=1; j--) {
				if (D[j] == 1) {
					if ((A[j+1] + A[j]) / 2 >= A[X[i]] - T) {
						printf("%d\n", (A[j+1] + A[j]) / 2);
						flag = 1;
					}
					break;
				}
			}
			if (flag == 0) {
				printf("%d\n", A[X[i]] - T);
			}
	    }
		flag = 0;
    }
	return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;

#define fst(t) std::get<0>(t)
#define snd(t) std::get<1>(t)
#define thd(t) std::get<2>(t)
#define unless(p) if(!(p))
#define until(p) while(!(p))

using ll = long long;
using P = std::tuple<int,int>;

const int dx[8] = {-1, 1, 0, 0, -1, -1, 1, 1}, dy[8] = {0, 0, -1, 1, -1, 1, -1, 1};

ll N, T, Q;
ll A[100100], D[100100];
ll B[100100];

int main(){
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);

    std::cin >> N >> T >> Q;

    for(int i=1;i<=N;++i){
        std::cin >> A[i] >> D[i];
    }

    int i = 1;
    while(i <= N && D[i] == 2){
        B[i] = A[i] - T;
        ++i;
    }

    for(int j=i;i<=N;){
        while(j <= N && D[j] == 1){
            ++j;
        }

        if(j <= N){
            ll x = (A[j-1] + A[j]) / 2;
            while(i < j){
                B[i] = min(A[i] + T, x);
                ++i;
            }

            while(j <= N && D[j] == 2){
                B[j] = max(A[j] - T, x);
                ++j;
            }

            i = j;
        }else{
            while(i <= N){
                B[i] = A[i] + T;
                ++i;
            }
        }
    }

    for(int i=0;i<Q;++i){
        int X;
        std::cin >> X;

        std::cout << B[X] << std::endl;
    }
}

C++: #include <stdio.h>
int main(){
  long long a[100000],T,ans[100000],stop;
  int j,k,d[100000],x[100000],Q,i,N,s;
  //in
  scanf("%d %lld %d",&N,&T,&Q);  
  for(i=0;i<N;i++){
  scanf("%lld %d",&a[i],&d[i]);
  }
  for(i=0;i<Q;i++){
    scanf("%d",&x[i]);
    x[i]--;
  }
  //in end
  for(i=0;i<Q;i++){
    s=0;
    if(d[x[i]]==1){
      for(j=x[i];j<N;j++){
	if(d[j]==2){
	  stop=(a[j]+a[j-1])/2;
	  //printf("stop %d\n",stop);
	  if(a[x[i]]+T>stop){
	  ans[i]=stop;
	  s=1;
	  break;
	  }
	}
      }
      if(s==0)ans[i]=a[x[i]]+T;
    }

    if(d[x[i]]==2){
      for(j=x[i];j>=0;j--){
	if(d[j]==1){
	  stop=(a[j]+a[j+1])/2;
	  //printf("st %d\n",stop);//
	  if(a[x[i]]-T<stop){
	  ans[i]=stop;
	  s=1;
	  break;
	  }
	}
      }
      if(s==0)ans[i]=a[x[i]]-T;
    }
  }

  for(i=0;i<Q;i++){
    printf("%lld\n",ans[i]);
  }
  return 0;
}