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>
using namespace std;
 
typedef long long LL;
typedef pair<LL, LL> P;
 
int main() {
    LL a, c;
    LL b;
    cin >> a >> b >> c;
 
    P m[100005];
    P q[1010];
    vector<LL>st;
 
    for (LL i = 0;i < a;i++)cin >> m[i].first >> m[i].second;
    
    for (LL i = 0;i < c;i++) {
        LL d;
        cin >> d;
        q[i] = m[d - 1];
    }
 
    for (LL i = 0;i < a - 1;i++) {
        if (m[i].second == 1 && m[i + 1].second == 2)st.push_back((m[i].first + m[i+1].first) / 2);
    }
 
 
    for (LL i = 0;i < c;i++) {
 
        if (q[i].second == 1) {
 
            long long e = q[i].first+b;
 
            for (int j = 0;j < st.size();j++) {
 
                if (q[i].first < st[j]) {
                    if (e>= st[j])e = st[j];
                }
 
            }
            cout << e << endl;
        }
 
        else {
 
            long long e=q[i].first-b;
 
            LL f = st.size();
            for (LL j = f - 1;j > -1;j--) {
 
                if (q[i].first > st[j]) {
                    if (e <= st[j])e = st[j];
                }
                 
            }
            cout << e << endl;
        }
        
    }
   
    return 0;
}

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

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

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

int main(){
  scanf("%lld %lld %lld",&N,&T,&Q);
  for(int i=0;i<N;i++){
    scanf("%lld %lld",&a[i],&d[i]);
    if(i && 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 <bits/stdc++.h>
#define f first
#define s second
using namespace std;
typedef long long ll;
typedef pair<ll,ll> P;
P p[100010];
vector <ll>x;


int search(ll X){
  int L=0,M,R=x.size();
  while(L<R){
    M=(L+R)/2;
    if(X<=x[M]) R=M;
    else L=M+1;
  }
  return L;
}

int main(){
  ll n,T,q;
  cin>>n>>T>>q;

  for(int i=0,idx=-1,dir;i<n;i++){
    ll a;
    cin>>a>>dir;
    dir--;
    if(dir==0) idx=i;
    p[i]=P(a,dir);
    if(i&&idx!=-1&&dir==1){
      ll dis=(p[i].f+p[idx].f)/2;
      if(dis<=2*T)x.push_back(dis);
      idx=-1;
    }
  }

  //  for(int i=0;i<x.size();i++)cout <<x[i]<<" ";cout <<endl;

  while(q--){
    int Q;
    cin>>Q;
    Q--;
    ll a=p[Q].f;
    int dir=p[Q].s;
    int idx=search(a)-(dir? 1:0);
    ll ans=(dir? -1:1)*T+a;
    if(!((dir==1&&idx==-1)||(dir==0&&idx==(int)x.size()))&&abs(x[idx]-a)<=T)ans=x[idx];
    cout <<ans<<endl;
  }
  return 0;
}

C++: #include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <array>
#include <queue>
#include <algorithm>
#include <utility>
#include <cmath>
#include <map>
#include <set>

#define INF_LL 9000000000000000000
#define INF 2000000000

#define REP(i, n) for(int i = 0;i < (n);i++)
#define FOR(i, a, b) for(int i = (a);i < (b);i++)

using namespace std;

typedef long long ll;
typedef pair<int, int> PII;

class Union_find{
private:
	vector<int> par;
	vector<int> rank;
	int n;

public:
	Union_find(int a){
		n = a;
		for(int i = 0;i < n;i++){
			par.push_back(i);
			rank.push_back(0);
		}
	}

	int find(int x){
		if(par[x] == x){
			return x;
		}else{
			return par[x] = find(par[x]);
		}
	}

	void unite(int x, int y){
		x = find(x);
		y = find(y);
		if(x == y) return;

		if(rank[x] < rank[y]){
			par[x] = y;
		}else{
			par[y] = x;
			if(rank[x] == rank[y]) rank[x]++;
		}
	}

	bool same(int x, int y){
		return find(x) == find(y);
	}
};

int main(void){
	ll N, T, Q;
	ll A[100000], D[100000];
	ll X[100000];
	vector<ll> p;
	cin >> N >> T >> Q;
	REP(i, N){
		cin >> A[i] >> D[i];
	}
	REP(i, Q){
		cin >> X[i];
	}
	p.push_back(-1*INF_LL);

	REP(i, N-1){
		if(D[i] == 1 && D[i+1] == 2){
			p.push_back((A[i]+A[i+1])/2);
		}
	}
	p.push_back(INF_LL);
	REP(i, Q){
		if(D[X[i]-1] == 1){
			REP(j, p.size()){
				if(p[j] >= A[X[i]-1]){
					if(abs(p[j] - A[X[i]-1]) <= T){
						cout << p[j] << endl;
					}else{
						cout << A[X[i]-1] + T << endl;
					}
					j = p.size();
				}
			}
		}else{
			REP(j, p.size()){
				if(p[j] > A[X[i]-1]){
					if(abs(A[X[i]-1] - p[j-1]) <= T){
						cout << p[j-1] << endl;
					}else{
						cout << A[X[i]-1] - T << endl;
					}
					j = p.size();
				}
			}
		}
	}
}

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);
			if(*itr <= pos[input]){
				res = *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;
}