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>

constexpr int MAX_N = 100000;
constexpr long long MAX_T = 1000000000000000000;

long long N, T, Q;

long long pos[MAX_N], target[MAX_N];
int dir[MAX_N];
std::vector<long long> stops;

void solve();

int main(){
	std::ios::sync_with_stdio(false);
	std::cin.tie(0);

	std::cin >> N >> T >> Q;

	for(int i = 0; i < N; ++i){
		std::cin >> pos[i] >> dir[i];
	}

	for(int i = 0; i < N - 1; ++i){
		if(dir[i] == 1 && dir[i + 1] == 2 && pos[i + 1] - pos[i] <= 2 * T){
			stops.push_back((pos[i + 1] + pos[i]) / 2);
		}
	}

	for(int i = 0; i < Q; ++i){
		int input;
		std::cin >> input;
		--input;

		long long res = MAX_T;
		if(dir[input] == 1){ //When a citizen heads east.
			auto itr = std::lower_bound(stops.begin(), stops.end(), pos[input]);
			while(*itr <= pos[input] + T){
				res = std::min(res, *itr);
				++itr;
			}
		}
		else{ //When a citizen heade west.
			auto itr = std::lower_bound(stops.begin(), stops.end(), pos[input] - T);
			while(*itr <= pos[input]){
				res = std::min(res, *itr);
				++itr;
			}
		}

		if(res == MAX_T){
			if(dir[input] == 1){
				std::cout << pos[input] + T;
			}
			else{
				std::cout << pos[input] - T;
			}
		}
		else{
			std::cout << res <<  std::endl;
		}
	}

	return 0;
}

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

long long n, q, t;

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

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

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

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

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

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

	for (long long 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 (long long i = 0; i < n - 1; i++) {
		if (d[i] == 1 && d[i + 1] == 1) {
			long long j = i + 1;

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

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

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

	return 0;
}

C++: #include<bits/stdc++.h>
#define PB push_back
#define MP make_pair
#define FI first
#define SE second
using namespace std;
typedef pair<int,int> pii;
static const int inf = 1ll<<60;
static const int MAX_N = 100000;

int N,T,Q;
vector<pii> A;
int flg[MAX_N+5];
int table[MAX_N+5];
int next[MAX_N+5];

signed main(){
    memset(next,-1,sizeof(next));
    cin>>N>>T>>Q;
    for(int i=0;i<N;++i){
        int a,d;
        cin>>a>>d;
        if(d!=1)d=-1;
        A.PB(pii(a,d));
    }
    sort(A.begin(),A.end());
    int maxd=0;
    int t=0;
    for(int i=0;i<N;++i){
        if(flg[i]==1)continue;
        if(A[i].SE!=1||A[i+1].SE!=-1){
            if(A[i].SE==1)table[i]=t;
            else table[i]=t;
            continue;
        }
        int D=abs(A[i].FI-A[i+1].FI)/2;
        if(D<=T){
            maxd=max(D,maxd);
            A[i].FI+=A[i].SE*D;
            A[i+1].FI+=A[i+1].SE*D;
            flg[i]=flg[i+1]=1;
            next[t]=A[i].FI;
            t++;
        }
    }
    for(int i=0;i<N;++i){
        if(flg[i]==1)continue;
        if(A[i].SE==1)A[i].FI+=maxd;
        else A[i].FI-=maxd;
    }
    T-=maxd;
    for(int i=0;i<N;++i){
        if(flg[i]==1)continue;
        if(A[i].SE==1){
            int k=table[i];
            if(next[k]==-1)A[i].FI+=T;
            else{
                int d=abs(A[i].FI-next[k]);
                if(d<=T)A[i].FI=next[k];
                else A[i].FI+=T;
            }
        }
        else{
            int k=table[i];
            if(next[k]==-1)A[i].FI-=T;
            else{
                int d=abs(A[i].FI-next[k]);
                if(d<=T)A[i].FI=next[k];
                else A[i].FI-=T;
            }
        }
    }
    for(int i=0;i<Q;++i){
        int q;
        cin>>q;
        q--;
        cout<<A[q].FI<<endl;
    }
}

C++: #include <stdio.h>
#include <string.h>
#include <algorithm>
long long int home[12345678];
long long int muki[12345678];
long long int important[123456];
long long int hit[12345678];
long long int ans[12345678];
int main(){
	long long int num;
	long long int time;
	long long int imp_num;
	long long int hit_num = 0;
	scanf("%lld%lld%lld", &num, &time, &imp_num);
	for (long long int a = 0; a < num; a++){
		scanf("%lld%lld", &home[a], &muki[a]);
	}
	for (int a = 0; a < imp_num; a++){
		scanf("%lld", &important[a]);
	}
	for (long long int a = 0; a < num - 1; a++){
		if (muki[a] == 1 &&
			muki[a + 1] == 2){
			hit[hit_num] = (home[a + 1] - home[a]) / 2 + home[a];
			hit_num++;
		}
	}
	for (long long int a = 0; a < num; a++){
		if (muki[a] == 1){
			home[a] += time;
			for (long long int b = 0; b < hit_num; b++){
				if (home[a] >= hit[b] &&
					home[a] - time <= hit[b]){
					home[a] = hit[b];
					break;
				}
			}
		}
		else{
			home[a] -= time;
			for (long long int b = hit_num - 1; 0 <= b; b--){
				if (home[a] <= hit[b] &&
					home[a] + time >= hit[b]){
					home[a] = hit[b];
					break;
				}
			}
		}
	}
	for (int a = 0; a < imp_num; a++){
		printf("%lld\n", home[important[a] - 1]);
	}
	return 0;
}