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 <iostream>
#include <vector>
using namespace std;
typedef pair<long long,long long> Paint;
struct mpper{
  long long b;//begin
  long long c;//center -> 1,2の1側
  long long e;//end
};
int main(){
  long long n,t,q;cin>>n>>t>>q;
  vector<Paint> a(n);
  for(int i = 0; n > i; i++){
    cin>>a[i].first >> a[i].second;
  }
  // 222/1122/112222/1111222/11
  vector<mpper> lr;
  mpper tmp = {0,-100,-100};
  if(a[0].second == 2){
    tmp.b = -1;//none
    tmp.c = -1;//none
  }
  for(int i = 1; n > i; i++){
    if(a[i-1].second != a[i].second){
      if(a[i-1].second == 2){
        tmp.e = i-1;
        lr.push_back(tmp);
        tmp = {i,-100,-100};
      }else{
        tmp.c = i-1;
      }
    }
  }
  if(tmp.c == -100){// 1122/11
    tmp.c = n+1;//none
    tmp.e = n+1;//none
    lr.push_back(tmp);
  }else{ 
    tmp.e = n-1;
    lr.push_back(tmp);
  }

  //output
  for(int i = 0; q > i; i++){
    long long x;cin>>x;x--;
    int me = 0;
    for(; lr.size() > me; me++){
      if(lr[me].b <= x && x <= lr[me].e)break;
    }
    if(lr[me].b == -1){//永遠に西に歩け
      cout << a[x].first - t << endl;
    }else if(lr[me].e == n+1){//永遠に東に歩け
      cout << a[x].first + t << endl;
    }else{//ぶつかって歩き話をする可能性があるので
      long long seken = (a[lr[me].c].first + a[lr[me].c+1].first) / 2;
      if(x <= lr[me].c){//->
        cout << min(seken,a[x].first + t) << endl;
      }else{//<-
        cout << max(seken,a[x].first - t) << endl;
      }
    }

  }
}

C++: #include<bits/stdc++.h>
#define INF 100010001000
using namespace std;
long int p[100010],ans[100010],dire[100010],n,t,q,x[100010],mm,s1,s2;


int main(){

    for(int i=0;i<100010;i++)ans[i]=INF;
    mm=INF;
    cin >> n >> t >> q;
    for(int i=1;i<=n;i++)cin >> x[i] >> dire[i];

    for(int i=1;i<=n;i++){
        if(dire[i]==1&&dire[i+1]==2){
            mm=(x[i]+x[i+1])/2;
            ans[i]=mm,ans[i+1]=mm;

            for(int j=i-1;j>0;j--){
                if(dire[j]==1)ans[j]=mm;
                else break;
            }
        }else if(dire[i]==2)ans[i]=mm;
    }

    for(int i=0;i<q;i++){
        cin >> mm;
        if(dire[mm]==1)s1=x[mm]+t;
        else s1=x[mm]-t;

        //cout << "??????????????? " << x[mm] << "  ?????????????????? "  << x[mm]-t << "  ??¶????????£?????´??? " << ans[mm] << endl;
        if(labs(x[mm]-s1)<labs(x[mm]-ans[mm])){
            if(dire[mm]==1)cout << x[mm]+t << endl;
            else cout << x[mm]-t << endl;
        }else cout << ans[mm] << endl;
    }

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;

long long 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<stdio.h>
long long t,tt;
long long a[100001],s[100001];
int b[100001],flg[100001];
int main(void)
{
	int n,q,i,j,c[1000],x;
	scanf("%d %lld %d",&n,&t,&q);
	for(i=1;i<=n;i++){
		scanf("%lld %d",&a[i],&b[i]);
		flg[i]=0;
	}	
	for(i=1;i<=q;i++)	scanf("%d",&c[i]);
	for(i=1;i<n;i++){
		if(i+1<=n){
			if(b[i]==1&&b[i+1]==2){
				flg[i]=1; flg[i+1]=1;
				s[i]=s[i+1]=(a[i]+a[i+1])/2;
			}
		}
	}
	x=0;
	for(i=q;i>0;i--){
		if(flg[c[i]]==1){
			tt=s[c[i]];	x=1;
		}
		else{
			if(b[c[i]]==1&&x==1&&a[c[i]]+t>tt){
				s[c[i]]=tt;	flg[c[i]]=2;
			}
		}
	}
	x=0;
	for(i=1;i<=q;i++){
		if(flg[c[i]]==1){
			tt=s[c[i]];	x=1;
		}
		else{
			if(b[c[i]]==2&&x==1&&a[c[i]]-t<tt){
				s[c[i]]=tt;	flg[c[i]]=2;
			}
		}
	}
	for(i=1;i<=n;i++){
		if(flg[i]==0){
			if(b[i]==1) s[i]=a[i]+t;
			else s[i]=a[i]-t;
		}
	}	
	for(i=1;i<=q;i++) printf("%lld\n",s[c[i]]);
}