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;
#define rep(i, n) for(int i = 0; i < (int)n; i++)
#define repr(i, n) for(int i = n; i>=0; i--)
#define all(x) (x).begin(), (x).end()
#define INF 100100100
typedef long long ll;
typedef pair<int, int> p;
 
int main(void) {
    int n, q, d[100002];
    ll t, a[100002], stop[100002];
    scanf("%d%lld%d", &n, &t, &q);
    for (int i = 1; i <= n; i++) {
        scanf("%lld%d", &a[i], &d[i]);
    }
    a[0] = -1000000000000000000LL;
    d[0] = 1;
    a[n + 1] = 1000000000000000000LL;
    d[n + 1] = 2;
    for (int i = 0; i <= n; i++) {
        if (d[i] == 1 && d[i + 1] == 2) {
            ll tmp = (a[i] + a[i + 1]) / 2;
            for (int j = i; j >= 0 && d[j] == 1; j--) {
                if (a[j] + t >= tmp) stop[j] = tmp;
                else stop[j] = a[j] + t;
            }
            for (int j = i + 1; j <= n + 1 && d[j] == 2; j++) {
                if (a[j] - t <= tmp) stop[j] = tmp;
                else stop[j] = a[j] - t;
            }
        }
    }
    for (int i = 0; i < q; i++) {
        int x;
        scanf("%d", &x);
        printf("%lld\n", stop[x]);
    }
     
    return 0;
}

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<=2*T){
            dummy=(house[i].place+house[i+1].place)/2;
            house[i].place=dummy;
            house[i+1].place=dummy;
            for(int j=i-1;j>=1;--j){
                if(house[j+1].place-house[j].place<=T){
                    house[j].place=house[j+1].place;
                    house[j].dotti=3;
                }else{
                    break;
                }
            }
            for(int j=i+2;j<=n;++j){
                if(house[j].place-house[j-1].place<=T){
                    house[j].place=house[j-1].place;
                    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){
            house[i].place-=T;
        }else if(house[i].dotti==1){
            house[i].place+=T;
        }
    }
    int doll;
    for(int i=0;i<Q;++i){
        cin>>doll;
        cout<<house[doll].place<<endl;
    }
    return 0;
}

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

int n, q;
long t;

int d[100000], v[100000];
long long a[100000];

int main()
{
	cin >> n >> t >> q;

	for (int i = 0; i < n; i++)
		cin >> a[i] >> d[i];

	for (int i = 0; i < q; i++)
		cin >> v[i];

	int i = 0;
	while (d[i] == 2) {
		a[i] -= t;
		i++;
	}

	i = n - 1;
	while (d[i] == 1) {
		a[i] += t;
		i--;
	}

	for (int i = 0; i < n - 1; i++) {
		if (d[i] == 1 && d[i + 1] == 2) {
			if (a[i + 1] - a[i] <= t * 2) {
				a[i] = a[i] / 2 + a[i + 1] / 2;
				a[i + 1] = a[i];
			}
			else {
				a[i] += t;
				a[i + 1] -= t;
			}
		}
	}

	for (int i = 0; i < n - 1; i++) {
		if (d[i] == 1 && d[i + 1] == 1) {
			int j = i + 1;

			while (d[j] == 1)
				j++;

			if (j < n - 1) {
				for (int k = i; k < j; k++) {
					if (a[j] - a[k] <= t)
						a[k] = a[j];
					else
						a[k] += t;
				}
				i = j - 1;
			}
			else
				break;
		}
		else if (d[i] == 2 && d[i + 1] == 2) {
			int j = i + 1;
			while (d[j] == 2)
				j++;
			if (i > 0) {
				for (int k = j - 1; k >= i; k--) {
					if (a[k] - a[i] <= t)
						a[k] = a[i];
					else
						a[k] -= t;
				}
			}
			i = j - 1;
		}
	}

	for (int i = 0; i < q; i++)
		cout << a[v[i] - 1] << endl;

	return 0;
}

C++: #include<stdio.h>
int main(void)
{
	long long int x,y,z,i,j,cnt,cnt2,t,t1;
	long long int a[1001],b[1001],c[1001],d[1001],w[1001];
	long long int flg[1001],g[1001];
	scanf("%lld %lld %lld",&x,&y,&z);
	for(i=1;i<=x;i++){
		d[i]=0; w[i]=0; g[i]=0; flg[i]=0;
	}
	for(i=1;i<=x;i++) scanf("%lld %lld",&a[i],&b[i]);
	for(i=1;i<=z;i++) scanf("%lld",&c[i]);	
	for(i=1;i<x;i++){
		if(i+1<=x){
			t=0; t1=0;
			if(b[i]==1&&b[i+1]==2){
				if(b[i]==1){
					t=a[i]+y;
					d[i]=(a[i]+a[i+1])/2; w[i]=1;
					if(d[i]>=t) d[i]=t;
				}
				if(b[i+1]==2){
					t1=a[i+1]-y;
					d[i+1]=(a[i]+a[i+1])/2; w[i+1]=1;
					if(d[i+1]<=t1) d[i+1]=t1;
				}
			}
		}
	}
	//for(i=1;i<=x;i++) printf("%lld\n",d[i]);
	for(i=1;i<=z;i++){
		if(w[c[i]]!=1){
			if(b[c[i]]==2){
			t=0;
				for(j=i;j>=0;j--){
					if(c[i]==x) break;
					if(w[j]==1){
						t=a[c[i]]+y;
						if(t<=d[j]){
							d[c[i]]=t; w[c[i]]=1;
							break;
						}	
						else{
							d[c[i]]=d[j]; w[c[i]]=1;
							break;
						}
					}
				}
			}
			else{
			t1=0;
				for(j=i;j<=x-i;j++){
					if(c[i]==x) break;
					if(w[j]==1){
						t1=a[c[i]]+y;
						if(t1>=d[j]){
							d[c[i]]=t1; w[c[i]]=1;
							break;
						}	
						else{
							d[c[i]]=d[j]; w[c[i]]=1;
							break;
						}
					}
				}
			}
		}
	}
	for(i=1;i<=x;i++) printf("%lld\n",w[i]);
	for(i=1;i<=x;i++){
		if(w[i]!=1){
			if(b[i]==1) d[i]=a[i]+y;
			else d[i]=a[i]-y;
		}
	}
	//for(i=1;i<=x;i++) printf("%lld\n",w[i]);
	for(i=1;i<=z;i++) printf("%lld\n",d[c[i]]);
	return 0;
}