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;

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

C++: #include<stdio.h>


int main() {
	int N, T, Q;
	int A[10000], D[10000], X[10000];
	scanf("%d %d %d", &N, &T, &Q);
	for (int i = 1; i <= N; i++) {
		scanf("%d %d", &A[i], &D[i]);
		if (D[i] == 2) D[i] = -1;
	}
	for (int i = 1; i <= Q; i++) {
		scanf("%d", &X[i]);
	}
	for (int i = 1; i <= T; i++) {
		if (D[i] != 0) {
			for (int j = 1; j <= N; j++) {
				A[j] += D[j];
				if (A[j] == A[j - 1]) {
					D[j] = 0;
					D[j - 1] = 0;
				}
				if (A[j] == A[j + 1]) {
					D[j] = 0;
					D[j + 1] = 0;
				}
			}
		}
	}
	for (int i = 1; i <= Q; i++) {
		printf("%d\n", A[X[i]]);
	}
	
	return 0;
}

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

ll N,T,Q;
set<ll> S;

ll a[100005];
ll d[100005];

int main(){
  cin>>N>>T>>Q;
  for(int i=0;i<N;i++){
    cin>>a[i]>>d[i];
    if(i>0 && d[i]!=d[i-1]){
      S.insert( a[i]/2LL + a[i-1]/2LL);
    }
  }

  ll INF = 1000000000000000001LL;
  S.insert(INF);
  S.insert(-INF);

  for(int i=0;i<Q;i++){
    ll id;
    scanf("%lld",&id);
    id--;
    if( d[id] == 1 ){
      ll target = *S.lower_bound(a[id]);
      cout<<min(a[id]+T,target)<<endl;
    }else{
      set<ll> :: iterator it;
      it=S.lower_bound(a[id]);
      it--;
      ll target = *it;
      cout<<max(a[id]-T,target)<<endl;
    }
  }
  return 0;
}

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]);
			if(*itr <= pos[input] + T){
				res = *itr;
			}
		}
		else{ //When a citizen heade west.
			auto itr = std::lower_bound(stops.begin(), stops.end(), pos[input] - T);
			while(*itr <= pos[input]){
				res = *itr;
				++itr;
			}
		}

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

	return 0;
}

C++: #include<stdio.h>


int main() {
	int N, T, Q, flag = 0;
	int A[10000], D[10000], X[10000];
	scanf("%d %d %d", &N, &T, &Q);
	for (int i = 1; i <= N; i++) {
		scanf("%d %d", &A[i], &D[i]);
	}
	for (int i = 1; i <= Q; i++) {
		scanf("%d", &X[i]);
	}
	for (int i = 1; i <= Q; i++) {
		if (D[X[i]] == 1) {
			for (int j = X[i]+1; j <= N;j++) {
				if (D[j] == 2) {
					if ((A[j-1] + A[j]) / 2 <= A[X[i]] + T) {
						printf("%ld\n", (A[j-1] + A[j]) / 2);
						flag = 1;
					}
					break;
				}
			}
			if (flag == 0) {
				printf("%d\n", A[X[i]] + T);
			}
		}
		if (D[X[i]] == 2) {
			for (int j = X[i] -1; j >=1; j--) {
				if (D[j] == 1) {
					if ((A[j+1] + A[j]) / 2 >= A[X[i]] - T) {
						printf("%d\n", (A[j+1] + A[j]) / 2);
						flag = 1;
					}
					break;
				}
			}
			if (flag == 0) {
				printf("%d\n", A[X[i]] - T);
			}
	    }
		flag = 0;
    }
	return 0;
}