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;
 
typedef long long ll;
 
int main()
{
    ll N, T, Q;
    cin >> N >> T >> Q;
    vector<ll> A(N), D(N), left, right, rleft, rright;
    for (int i = 0; i < N; i++) {
        cin >> A[i] >> D[i];
        if (D[i] == 1) {      
            right.push_back(A[i]);
            rleft.push_back(-A[i]);
        } else {
            left.push_back(A[i]);
            rright.push_back(-A[i]);
        }
    } 

    sort(left.begin(), left.end());
    sort(right.begin(), right.end());
   
    sort(rleft.begin(), rleft.end());
    sort(rright.begin(), rright.end());
 
    ll q;
    for (int i = 0; i < Q; i++) {
        cin >> q; q--;    
        if (D[q] == 1) {
            ll l = lower_bound(left.begin(), left.end(), A[q]) - left.begin();
            if (l == (int)left.size()) {
                cout << A[q] + T << endl;
                continue;
            }
            
            ll ub = upper_bound(right.begin(), right.end(), left[l]) - right.begin()-1;
            ll now = (right[ub] != A[q] ? right[ub] : A[q]); 
            if (right[ub] != A[q]) {
                ll g;
                if (now + T >= left[l] - T) {
                    g = (now + left[l]) / 2;
                } else {
                    g = now + T;
                }
                cout << min(A[q] + T, g) << endl;
            } else {
                if (now + T >= left[l] - T) {
                    cout << (now + left[l]) / 2 << endl;
                } else {
                    cout << now + T << endl;
                }
            }   
        } else {  
            ll l = lower_bound(rleft.begin(), rleft.end(), -A[q]) - rleft.begin();
            if (l == (int)rleft.size()) {
                cout << A[q] - T << endl;
                continue;
            }

            ll ub = upper_bound(rright.begin(), rright.end(), rleft[l]) - rright.begin()-1;
            ll now = (-rright[ub] != A[q] ? -rright[ub] : A[q]);
            if (-rright[ub] != A[q]) {
                ll g;
                if (now - T <= -rleft[l] + T) {
                    g = (now - rleft[l]) / 2;
                } else {
                    g = now - T;
                }
                cout << max(A[q] - T, g) << endl;
            } else {
                if (now - T <= -rleft[l] + T) {
                    cout << (now - rleft[l]) / 2 << endl;
                } else {
                    cout << now - T << endl;
                }
            } 
        }
    }  
    return 0;
}

C++: #include <bits/stdc++.h>
#define int long long
#define ALL(c) begin(c),end(c)
using namespace std;
const int INF=1001001001001001001ll;
int N,T,Q;
vector<int> A,D;
vector<bool> done;
vector<int> ans;
int rec(int i) {
    int ret=INF;
    if(D[i]==2) {
        if(i!=0 && D[i-1]==1) {
            ret=A[i-1]+(A[i]-A[i-1])/2;
        }
        if(i!=0 && D[i-1]==2) {
            ret=rec(i-1);
        }
    }
    if(D[i]==1) {
        if(i!=N-1 && D[i+1]==1) {
            ret=rec(i+1);
        }
        if(i!=N-1 && D[i+1]==2) {
            ret=A[i]+(A[i+1]-A[i])/2;
        }
    }
    done[i]=true;
    return ans[i]=ret;
}
signed main() {
    cin>>N>>T>>Q;
    A=vector<int>(N);
    D=vector<int>(N);
    ans=vector<int>(N,INF);
    done=vector<bool>(N,false);
    for(int i=0; i<N; i++) {
        cin>>A[i]>>D[i];
    }
    for(int i=0; i<N; i++) {
        if(!done[i]) {
            rec(i);
        }
    }
    for(int i=0; i<Q; i++) {
        int x;
        cin>>x;
        x--;
        if(abs(ans[x]-A[x])>=T) {
            cout<<A[x]+(D[x]==1?T:-T)<<endl;
        }
        else {
            cout<<ans[x]<<endl;
        }
    }
    return 0;
}

C++: #include<iostream>
#include<string>
#include<cstdio>
using namespace std;
int main() {
	long long int n, t;
	int q;
	scanf("%lld %lld %d",&n,&t,&q);
	long long int home[100005] = {0};
	int keyman[100005] = { 0 };
	long long int walk[2][100005] = {0};
	for (int i = 0; i < n; i++) {
		scanf("%lld %lld", &walk[0][i],&home[i]);
	}
	for (int i = 0; i <q; i++) {
		scanf("%lld", &keyman[i]);
	}
	
	for (int i = 1; i <= t; i++) {
		for (int j = 0; j < n; j++) {
			if (walk[1][j] != 1) {
				if (home[j]== 1) {
					walk[0][j]++;
				}
				else {
					walk[0][j]--;
				}
				
			}
			for (int k = 0; k < j; k++) {
					if (walk[0][k] == walk[0][j]) {
						walk[1][k] = 1;
						walk[1][j] = 1;
					}
				}
			/*cout << "nowspot" << walk[0][j] << endl;*/
		}
		for (int x = 0; x < n; x++) {
			if (walk[1][x] == 1) {
				/*cout << "freeze:" << x << endl;*/
			}
		}

	}
	for (int i = 0; i < q; i++) {
		printf("%lld\n", walk[0][keyman[i] - 1]);
	}
	return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <unordered_map>
#include <cmath>
using namespace std;
typedef long long int ll;
int main() {
	ll N, T, Q;
	cin >> N >> T >> Q;
	vector<ll> pos(N), direction(N), person(Q), ans(Q);
	for (int i = 0; i < N; i++) cin >> pos[i] >> direction[i];
	for (int i = 0; i < Q; i++) {
		cin >> person[i];
		person[i]--;
	}

	
	for (int i = 0; i < Q; i++) {
		ll pair_point = 0;
		if (direction[person[i]] == 1) {
			int pair = -1;

			for (int j = person[i] + 1; j < N; j++) {
				if (direction[j] == 2) {
					pair = j;
					break;
				}
			}

			if (pair == -1) {
				ans[i] = pos[person[i]] + T;
				continue;
			}

			pair_point = (pos[pair] + pos[pair - 1]) / 2;
			if (abs(pair_point - pos[person[i]]) < T) ans[i] = pair_point;
			else ans[i] = pos[person[i]] + T;
		}
		else {
			int pair = -1;
			for (int j = person[i] - 1; j >= 0; j--) {
				if (direction[j] == 1) {
					pair = j;
					break;
				}
			}
			if (pair == -1) {
				ans[i] = pos[person[i]] + -T;
				continue;
			}

			pair_point = (pos[pair] + pos[pair + 1]) / 2;
			if (abs(pair_point - pos[person[i]]) < T) ans[i] = pair_point;
			else ans[i] = pos[person[i]] + -T;
		}
	}
	for (int i = 0; i < Q; i++)cout << ans[i] << endl;
	return 0;
}