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>
#include<algorithm>
#include<utility>
using namespace std;
#define rep(i,n) for(int i = 0; i < (n); i++)
#define all(in) in.begin(),in.end()
#define int long long
typedef pair<int,int> pii;
signed main(){
    int n,t,q;
    cin >> n >> t >> q;
    vector<pii> l,r;
    vector<int> in(n),d(n);
    rep(i,n){
        cin >> in[i] >> d[i];
        if(d[i] == 1){
            r.push_back(pii(in[i],i));
        }else{
            l.push_back(pii(in[i],i));
        }
    }
    sort(all(l));
    sort(all(r));
    //rep(i,l.size())cout << " " << l[i].first;cout << endl;
    //rep(i,r.size())cout << " " << r[i].first;cout << endl;
    rep(i,q){
        int qq;
        cin >> qq;
        qq--;
        if(d[qq] == 1){
            //lの中からin[qq]以上の最小ind1をさがす
            int tmp1 = upper_bound(all(l), pii(in[qq],-1)) - l.begin();
            if(tmp1 == l.size()){
                cout << in[qq] + t << endl;
                continue;
            }
            int ind1 = l[tmp1].second;
            //rの中からin[ind1]以下の最大ind2をさがす
            int tmp2 = lower_bound(all(r), pii(in[ind1],-1)) - r.begin() - 1;
            int ind2 = r[tmp2].second;
            int x = (in[ind1] + in[ind2])/2;
            //cout << ind1 << " " << ind2 << " " << x << endl;
            if(in[qq] + t > x){
                cout << x << endl;
            }else{
                cout << in[qq] + t << endl;
            }
        }else{
            int tmp1 = lower_bound(all(r),pii(in[qq],-1)) - r.begin() - 1;
            if(tmp1 < 0){
                cout << in[qq] - t << endl;
                continue;
            }
            int ind1 = r[tmp1].second;
            int tmp2 = upper_bound(all(l), pii(in[ind1],-1)) - l.begin();
            int ind2 = l[tmp2].second;
            int x = (in[ind1] + in[ind2])/2;
            //cout << ind1 << " " << ind2 << " " << x << endl;
            if(x > in[qq] - t){
                cout << x << endl;
            }else{
                cout << in[qq] - t << endl;
            }
        }

    }
}

C++: #include <bits/stdc++.h>
#define rep(i, a, n) for(int i = a; i < n; i++)
#define repp(i, n) rep(i, 0, n)
#define repb(i, a, b) for(int i = a; i >= b; i--)
#define all(a) a.begin(), a.end()
#define int long long
using namespace std;

signed main(){
    int n, t, q;
    cin >> n >> t >> q;
    vector<int> a(n + 1), d(n + 1), x(q);
    rep(i, 0, n) cin >> a[i] >> d[i];
    rep(i, 0, q) cin >> x[i];
    vector<int> ans(n);
    int l = 0, r = n;
    rep(i, 0, n){
        if(d[i] == 1){
            l = i; break;
        }
        ans[i] = a[i] - t;
    }
    repb(i, n - 1, 0){
        if(d[i] == 2){
            r = i + 1; break;
        }
        ans[i] = a[i] + t;
    }
    d[r] = 1;
    int st = l, now = 1, tmp = 0;
    rep(i, l, r + 1){
        if(d[i] == now) continue;
        if(d[i] == 2) tmp = (a[i - 1] + a[i]) / 2;
        if(d[i] == 1){
            rep(j, st, i - 1){
                if(abs(a[j] - tmp) <= t) ans[j] = tmp;
                else if(d[j] == 1) ans[j] = a[j] + t;
                else ans[j] = a[j] - t;
            }
            st = i;
        }
        now = d[i];
    }
    rep(i, 0, q){
        cout << ans[x[i] - 1] << endl;
    }
}

C++: #include <bits/stdc++.h>
using namespace std;

int main(void){
    long long n,q;
    long long t;
    cin>>n>>t>>q;
    vector<pair<long long,int> >c(n);
    vector<long long> ans;
    for (int i = 0; i < n; ++i) {
        cin>>c[i].first>>c[i].second;
        if(c[i].second==2)c[i].second=-1;
    }
    for (int i = 0; i < n; ++i) {
        if(c[i].second!=c[i+c[i].second].second)ans.push_back((c[i].first+c[i+c[i].second].first)/2);
    }
    for (int i = 0; i < q; ++i) {
        int a;
        cin>>a;
        a=a-1;
        if(c[a].second==1) {
            for (int j = 0; j < ans.size(); ++j) {
                    if(j==ans.size()-1){
                        long long an = t * c[a].second + c[a].first;
                        cout << an << endl;
                        break;
                    }
                    if (c[a].first < ans[j]) {
                        if (abs(ans[j] - c[a].first) < t) {
                            cout << ans[j] << endl;
                            break;
                        }
                        else {
                            long long an = t * c[a].second + c[a].first;
                            cout << an << endl;
                            break;
                        }
                    }

            }
        }
        else{
            for (int j = ans.size(); j > 0; --j) {
                if(j==0){
                    long long an = t * c[a].second + c[a].first;
                    cout << an << endl;
                    break;
                }
                if (c[a].first > ans[j]) {
                    if (abs(ans[j] - c[a].first) < t) {
                        cout << ans[j] << endl;
                        break;
                    } else {
                        long long an = t * c[a].second + c[a].first;
                        cout << an << endl;
                        break;
                    }
                }
            }
        }
    }
    return 0;
}