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++: 
//16-4 JOI国のお散歩事情
#include<iostream>
#include<fstream>
#include<stdio.h>
#include<math.h>
#include<time.h>
#include<string>
#include<vector>
#include<map>
#include<list>
#include<set>
#include<stack>
#include<queue>
#include<cstdlib>
#include<algorithm>
#include<random>
#include<cassert>
#include <boost/multiprecision/cpp_int.hpp>
namespace mp = boost::multiprecision;
using namespace std;
#define LL long long
#define segment_size 65536
#define ROOP() while(true)

LL N,T,Q;
pair<mp::cpp_int,int> A[100000];
set<mp::cpp_int> stop;
LL X[1000];

int main(){
    cin >> N >> T >> Q;
    for(LL i=0; i<N; i++){
        mp::cpp_int a;
        int b;
        cin >> a >> b;
        A[i] = make_pair(a,b);
    }
    for(LL i=0; i<Q; i++){
        cin >> X[i];
        X[i]--;
    }

    int before = 2;
    for(LL i=0; i<N; i++){
        if(before==1 && A[i].second==2){
            stop.insert((A[i].first + A[i-1].first) /2);
        }
        before = A[i].second;
    }

    for(LL i=0; i<Q; i++){
        mp::cpp_int now = A[X[i]].first;
        if(A[X[i]].second == 1){
            auto itr = stop.upper_bound(now);
            if(itr == stop.end()) cout << now+T << endl;
            else{
                mp::cpp_int st = *itr;
                if(now+T > st) cout << st << endl;
                else cout << now+T << endl;
            }
        }
        else{
            auto itr = stop.upper_bound(now);
            if(itr == stop.begin()) cout << now-T << endl;
            else{
                itr--;
                mp::cpp_int st = *itr;
                if(now-T < st) cout << st << endl;
                else cout << now-T << 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);
			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;
}

C++: //
//  main.cpp

#include <iostream>
#include <limits.h>
using namespace std;
using ll = long long;
const ll MIN = -(LONG_LONG_MAX-1);

int main(){	
	int N,Q;
	ll T;
	cin >> N >> T >> Q;
	
	ll people[1001];
	ll A[100001];
	int D[100001];
	for(int i=0;i<N;i++){
		cin >> A[i] >> D[i];
	}
	for(int i=0;i<Q;i++){
		cin >> people[i];
		people[i]--;
	}
	
	ll east = MIN;
	ll west = MIN;
	ll t;
	for(int i=0;i<Q;i++){
		if(D[people[i]] == 1){
			east = A[people[i]];
			for(ll j=people[i]+1;j<N;j++){
				if(D[j] == 1){
					east = A[j];
				}else{
					t = (east + A[j])/2;
					break;
				}
			}
			if(T < abs(t - A[people[i]])){
				cout << A[people[i]] + T << endl;
			}else{
				cout << t << endl;
			}
		}else{
			west = A[people[i]];
			for(ll j=people[i]-1;j>=0;j--){
				if(D[j] == 1){
					t = (west + A[j])/2;
					break;
				}else{
					west = A[j];
				}
			}
			if(T < abs(A[people[i]] - t)){
				cout << A[people[i]] - T << endl;
			}else{
				cout << t << endl;
			}
//			cout << min(T - (t - A[people[i]]),t - A[people[i]]) << endl;;
		}
	}
	
	/*
	ll answer[100001]={};
	ll lasteast = MIN;
	ll laststate = MIN;
	for(int i=0;i<N;i++){
		if(D[i] == 1){
			if(lasteast == MIN && laststate == MIN){
				answer[i] = A[i] - T;
			}else if(lasteast != MIN && laststate == MIN){
				if(T < abs(abs(lasteast) - abs(A[i]))/2){
					answer[i] = A[i] - T;
				}else{
					answer[i] = (A[i]+lasteast)/2;
				}
			}
		}else{
			
		}
		
	}*/
	
	return 0;
}

C++: #include <iostream>
#include <fstream>
#include <typeinfo>
#include <vector>
#include <cmath>
#include <set>
#include <map>
#include <string>
#include <algorithm>
#include <cstdio>
#include <queue>
#include <iomanip>
#include <cctype>
#define syosu(x) fixed<<setprecision(x)
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
typedef pair<double,double> pdd;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<char> vc;
typedef vector<vc> vvc;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<P> vp;
typedef vector<vp> vvp;
typedef pair<int,P> pip;
typedef vector<pip> vip;
const int inf=1<<25;
const ll INF=1ll<<50;
const double pi=acos(-1);
const double eps=1e-8;
const vi emp;

typedef vector<pair<ll,bool> > VP;

ll n,t,q;
VP a;
vi b;
vector<pair<ll,ll> > ans;

int main(){
	cin>>n>>t>>q;
	a=VP(n);
	b=vi(q);
	ans=vector<pair<ll,ll> >(n);
	for(int i=0;i<n;i++){
		int tmp;
		cin>>a[i].first>>tmp;
		if(tmp==1) a[i].second=1;
	}
	for(int i=0;i<q;i++){
		cin>>b[i];
		b[i]--;
	}
	ll stop;
	int num=-1;
	for(int i=0;i<n;i++){
		bool B=a[i].second;
		if(!i||!a[i-1].second&&B) num=i;
		if(num!=-1&&!B){
			if(a[i-1].second){
				stop=(a[i-1].first+a[i].first)/2;
				for(int j=num;j<=i;j++){
					ans[j].first=stop;
					ans[j].second=abs(a[j].first-stop);
				}
			}
			else{
				ans[i].first=stop;
				ans[i].second=abs(a[i].first-stop);
			}
		}
	}
	for(int i=0;i<q;i++){
		ll res;
		pair<ll,ll> p=ans[b[i]];
		if(p.first&&p.second<=t) res=p.first;
		else res=a[b[i]].first+((a[b[i]].second)?t:-t);
		cout<<res<<endl;
	}
}