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<algorithm>
using namespace std;
class people{
public:
    long long int place;
    int dotti;
};
int main(){
    int n;
    long long int T;
    int Q;
    cin>>n>>T>>Q;
    people *house;
    house=new people[n+2];
    for(int i=1;i<=n;++i){
        cin>>house[i].place>>house[i].dotti;
    }
    long long int dummy;
    house[0].dotti=2;
    house[n+1].dotti=1;
    for(int i=1;i+1<=n;++i){
        if(house[i].dotti==1&&house[i+1].dotti==2){
            if(house[i+1].place-house[i].place<=T*2){
                dummy=(house[i+1].place+house[i].place)/2;
                house[i].place=dummy;
                house[i+1].place=dummy;
                house[i].dotti=3;
                house[i+1].dotti=3;
                for(int j=i-1;j>=1;--j){
                    if(house[j].dotti==1){
                        house[j].place=min(house[j+1].place,house[j].place+T);
                        house[j].dotti=3;
                    }else{
                        break;
                    }
                }
                for(int j=i+2;j<=n;++j){
                    if(house[j].dotti==2){
                        house[j].place=max(house[j-1].place,house[j].place-T);
                        house[j].dotti=3;
                    }else{
                        break;
                    }
                }
            }else{
                house[i].place+=T;
                house[i+1].place-=T;
                house[i].dotti=3;
                house[i+1].dotti=3;
            }
        }
    }
    for(int i=1;i<=n;++i){
        if(house[i].dotti==2){
            if(house[i-1].dotti==3){
                house[i].place=max(house[i].place-T,house[i-1].place);
                house[i].dotti=3;
            }else{
                house[i].place-=T;
            }
        }else if(house[i].dotti==1){
            if(house[i+1].dotti==3){
                house[i].place=min(house[i].place+T,house[i+1].place);
                house[i].dotti=3;
            }else{
                house[i].place=min(house[i].place+T,house[i+1].place);
            }
        }
    }
    int doll;
    for(int i=0;i<Q;++i){
        cin>>doll;
        cout<<house[doll].place<<endl;
    }
    return 0;
}

C++: #include<iostream>
#include<stdio.h>
#include<string>
#include<sstream>
#include<vector>
#include<queue>
#include<stack>
#include<list>
#include<map>
#include<math.h>
#include<set>
#include<algorithm>
using namespace std;
#define REP(i,k,n) for(int (i)=(k);(i)<(n);(i)++)
#define FOR(i,n) for(int (i)=0;(i)<(n);(i)++)
#define ll long long
int n,q;
ll t;
int d[10001];
ll a[10001];
ll pstoppos[10001];
ll stoppos;


int main(){
  stack<ll> person;
  cin >>n>>t>>q>>a[0]>>d[0];
  if(d[0]==2){
    pstoppos[0]=100000000000000001;
  }
  else person.push(0);
  REP(i,1,n){
    cin >>a[i]>>d[i];
    if(d[i-1]==1 && d[i]==2){
      stoppos =(a[i-1]+a[i])/2;
    }
    if(d[i-1]==2 && d[i]==1){
      while(!person.empty()){
        pstoppos[person.top()]=stoppos;
        person.pop();
      }
    }
    if(d[i]==1)person.push(i);
    else pstoppos[i]=stoppos;
  }
  while(!person.empty()){
    pstoppos[person.top()]=stoppos;
    person.pop();
  }

  for(int i=0;i<q;i++){
    int x;
    cin>>x;
    x--;
    if(pstoppos[x]==100000000000000001){
      switch(d[x]){
        case 1:
        cout <<a[x]+t<<endl;
        break;
        case 2:
        cout <<a[x]-t<<endl;
        break;
      }
    }
    else if(d[x]==1){
      cout << min(a[x]+t,pstoppos[x])<<endl;
    }
    else{
      cout << max(a[x]-t,pstoppos[x])<<endl;
    }
  }
  return 0;
}

C++: #include <iostream>
#include <vector>

using namespace std;

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

    vector<long long> A;
    vector<int> D;
    for(int i=0; i<N; ++i) {
        long long a;
        int d;
        cin >> a >> d;
        A.push_back(a);
        D.push_back(d);
    }

    for(int i=0; i<Q; ++i) {
        long long vip;
        long long pos;

        cin >> vip;
        if(D[vip-1]==1) {
            bool flag=false;
            for(int j=vip; j<N ; ++j) {
                if(D[j]==2) {
                    pos=A[j]/2+A[j-1]/2;
                    if(pos-A[vip-1]>T) break;
                    flag=true;
                    break;
                }
            }
            if(!flag) cout << A[vip-1]+T << endl;
            else cout << pos << endl;
        } else {
            bool flag=false;
            for(int j=vip-2; j>=0; --j) {
                if(D[j]==1) {
                    pos=A[j]/2+A[j+1]/2;
                    if(A[vip-1]-pos>T) break;
                    flag=true;
                    break;
                }
            }
            if(!flag) cout << A[vip-1]-T << endl;
            else cout << pos << endl;
        }
    }

    return 0;
}