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>
#define int long long
using namespace std;

int N, T, Q;
int A[100001], D[100001];
int X[1001];
int P[100001];	//P[i] = ???i???????????????

signed main()
{
	int i, j;
	
	cin >> N >> T >> Q;
	for( i = 1; i <= N; i++ ){
		cin >> A[i] >> D[i];
	}
	for( i = 1; i <= Q; i++ ){
		cin >> X[i];
	}
	
	for( i = N; i >= 1; i-- ){
		if( D[i] == 2 )
			continue;
		//????????????????????????????????????
		if( i == N )
			P[i] = A[i] + T;
		else if( D[i + 1] == 2 )
			P[i] = min(A[i] + T, (A[i] + A[i + 1]) / 2);
		else
			P[i] = min(A[i] + T, P[i + 1]);
	}
	for( i = 1; i <= N; i++ ){
		if( D[i] == 1 )
			continue;
		//????????????????????????????????????
		if( i == 1 )
			P[i] = A[i] - T;
		else if( D[i - 1] == 1 )
			P[i] = max(A[i] - T, (A[i - 1] + A[i]) / 2);
		else
			P[i] = max(A[i] - T, P[i - 1]);
	}
	
	for( i = 1; i <= Q; i++ ){
		cout << P[X[i]] << endl;
	}
	return 0;
}

C++: #include<iostream>
#include<string>
#include<cstdio>

using namespace std;
long long int home[50002] = { 0 };
long long int home2[50002] = { 0 };
int keyman[1005] = { 0 };
long long int walk[2][50002] = { 0 };
long long int walk2[2][50002] = { 0 };
int main() {
	long long int n, t;
	int q;
	scanf("%lld %lld %d", &n, &t, &q);

	for (int i = 0; i < n; i++) {
		if (i < 50002) {
			scanf("%lld %lld", &walk[0][i], &home[i]);
		}
		else {
			int j = i - 50002;
			scanf("%lld %lld", &walk2[0][j], &home2[j]);
		}
	}
	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 (j < 50002) {
				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;
					}
				}
			}
			else {
				int u = j - 50002;
				if (walk2[1][u] != 1) {
					if (home2[u] == 1) {
						walk2[0][u]++;
					}
				else {
					walk2[0][u]--;
					}

				}
				for (int k = 0; k < j; k++) {
					if(k<50002){
						if (walk[0][k] == walk2[0][u]) {
							walk[1][k] = 1;
							walk2[1][u] = 1;
						}
					}
					else {
						int r = k - 50002;
						if (walk2[0][r] == walk2[0][u]) {
							walk2[1][r] = 1;
							walk2[1][u] = 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++) {
		if (keyman[i] < 50003) {
			printf("%lld\n", walk[0][keyman[i] - 1]);
		}
		else {
			printf("%lld\n", walk2[0][keyman[i] - 50003]);
		}
	}
	return 0;
}

C++: #include<iostream>
#include<cmath>
#include<cstdio>
using namespace std;
#pragma warning(disable:4996)
long long N, T, Q, a[1 << 17], b[1 << 17], c[1 << 17];
int main() {
	cin >> N >> T >> Q;
	for (int i = 0; i < N; i++)scanf("%lld%lld", &a[i], &b[i]);
	long long H = -1LL << 62;
	for (int i = 0; i < N; i++) {
		if (b[i] == 2) {
			if (i >= 1) {
				if (b[i - 1] == 1) {
					long long C = (a[i - 1] + a[i]) / 2;
					if (abs(C - a[i]) <= T) { H = C; c[i] = C; }
					else { c[i] = a[i] - T; }
				}
				else {
					if (abs(H - a[i]) <= T)c[i] = H;
					else { c[i] = a[i] - T; }
				}
			}
			if (i == 0)c[i] = a[i] - T;
		}
	}
	H = 1LL << 62;
	for (int i = N - 1; i >= 0; i--) {
		if (b[i] == 1) {
			if (i < N - 1) {
				if (b[i + 1] == 2) {
					long long C = (a[i + 1] + a[i]) / 2;
					if (abs(C - a[i]) <= T) { H = C; c[i] = C; }
					else { c[i] = a[i] + T; }
				}
				else {
					if (abs(H - a[i]) <= T)c[i] = H;
					else { c[i] = a[i] + T; }
				}
			}
			if (i == N - 1)c[i] = a[i] + T;
		}
	}
	for (int i = 0; i < Q; i++) {
		int p; scanf("%d", &p); p--; cout << c[p] << endl;
	}
	return 0;
}

C++: #include <bits/stdc++.h>

#define whole(xs) xs.begin(),xs.end()

using namespace std;

namespace {

    typedef double real;
    typedef long long ll;

    const ll INF = LLONG_MAX / 2LL;

    template<class T> ostream& operator<<(ostream& os, const vector<T>& vs) {
        if (vs.empty()) return os << "[]";
        os << "[" << vs[0];
        for (int i = 1; i < vs.size(); i++) os << " " << vs[i];
        return os << "]";
    }
    template<class T> istream& operator>>(istream& is, vector<T>& vs) {
        for (auto it = vs.begin(); it != vs.end(); it++) is >> *it;
        return is;
    }

    struct P {
        int index;
        int a, d;
        P() {}
        P(int index, int a, int d) : index(index), a(a), d(d) {}
    };
    bool operator<(const P& a, const P& b) {
        return a.a < b.a;
    }

    ll N, T, Q;
    vector<P> X;
    vector<int> I;
    void input() {
        cin >> N >> T >> Q;
        X.clear(); X.resize(N);
        for (int i = 0; i < N; i++) {
            X[i].index = i;
            cin >> X[i].a >> X[i].d;
        }
        I.clear(); I.resize(Q);
        for (int i = 0; i < Q; i++) {
            int q; cin >> q; q--;
            I[i] = q;
        }
    }


    void solve() {
        sort(whole(X));
        vector<ll> stop(N);
        vector<P> es;
        ll p = -INF;
        for (int i = 0; i < N; i++) {
            const P& a = X[i];
            if (a.d == 1) {
                es.push_back(a);
            } else {
                if (es.empty()) {
                    stop[a.index] = p;
                } else {
                    ll s = (es.back().a + a.a) / 2;
                    stop[a.index] = s;
                    p = s;
                    for (const auto& e : es) {
                        stop[e.index] = s;
                    }
                    es.clear();
                }
            }
        }
        for (auto e : es) {
            stop[e.index] = INF;
        }

        for (int q = 0; q < Q; q++) {
            int i = I[q];
            if (X[i].d == 1) {
                cout << min(stop[i], X[i].a + T) << endl;
            } else {
                cout << max(stop[i], X[i].a - T) << endl;
            }
        }

    }
}

int main() {
    input(); solve();
    return 0;
}