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 <cstdio>
#include <cstdlib>
#include <cstring>
#include <cassert>
#include <cmath>
#include <ctime>
#include <cctype>
#include <climits>
#include <iostream>
#include <string>
#include <array>
#include <vector>
#include <map>
#include <list>
#include <queue>
#include <deque>
#include <algorithm>
#include <numeric>
#include <utility>
#include <complex>
#include <memory>
#include <functional>

using namespace std;

#define ALL(a) (a).begin(),(a).end()
#define REP(index, begin, end) for (int (index) = (begin); (index) < (end); (index)++)
#define RE(index, end) REP(index, 0, end)
#define INF INT_MAX / 3
#define DINT(x) printf(#x " = %d\n", x);
#define GETINT(x) scanf("%d", &x)
#define GETLL(x) scanf("%lld", &x)
#define GETSTR(x) scanf("%s", x)

typedef vector<int> vi;
typedef vector<long long> vl;
typedef vector<double> vd;
typedef pair<int, int> pii;
typedef pair<long, long> pll;
typedef long long ll;

struct ie_t
{
	ll pos;
	char dir;
};

int main()
{
	int ie_num, hito_num;
	ll time;
	vector<ie_t> ie;
	vi hito;
	GETINT(ie_num);
	GETLL(time);
	GETINT(hito_num);
	ie.reserve(ie_num);
	hito.reserve(hito_num);
	RE(i, ie_num)
	{
		ll pos;
		int dir;
		GETLL(pos);
		GETINT(dir);
		ie_t temp;
		temp.pos = pos;
		switch (dir)
		{
		case 1:
			temp.dir = 1;
			break;
		case 2:
			temp.dir = -1;
			break;
		}
		ie.push_back(temp);
	}
	RE(i, hito_num)
	{
		int temp;
		GETINT(temp);
		hito.push_back(temp - 1);
	}
	vector<ll> result;
	RE(hito_cur, hito_num)
	{
		int hito_ieidx = hito[hito_cur];
		ie_t hito_ie = ie[hito_ieidx];
		if ((hito_ieidx >= ie_num - 1 && hito_ie.dir == 1) ||
			(hito_ieidx <= 0 && hito_ie.dir == -1))
		{
			result.push_back(hito_ie.pos + hito_ie.dir * time);
			continue;
		}
		int hito_n_ieidx = hito_ieidx + hito_ie.dir;
		ie_t hito_n_ie = ie[hito_n_ieidx];
		while (hito_ie.dir == hito_n_ie.dir)
		{
			if ((hito_ieidx >= ie_num - 1 && hito_ie.dir == 1) ||
				(hito_ieidx <= 0 && hito_ie.dir == -1))
			{
				result.push_back(hito_ie.pos + hito_ie.dir * time);
				continue;
			}
			if (hito_ie.pos - hito_n_ie.pos >= time &&
				hito_n_ie.pos - hito_ie.pos >= time)
			{
				result.push_back(hito_ie.pos + hito_ie.dir * time);
				continue;
			}
			hito_n_ieidx += hito_ie.dir;
			hito_n_ie = ie[hito_n_ieidx];
		}
		ie_t hito_nprev_ie = ie[hito_n_ieidx - hito_ie.dir];
		ll len = (hito_n_ie.pos + hito_nprev_ie.pos) / 2 - hito_ie.pos;
		if (len * hito_ie.dir > time)
		{
			len = time * hito_ie.dir;
		}
		result.push_back(len + hito_ie.pos);
	}
	RE(i, hito_num)
	{
		printf("%lld\n", result[i]);
	}
	return 0;
}

C++: 
#include <iostream>
#include <algorithm>
#include<cstdio>
#include<string>
#include<vector>
using namespace std;
int main() {
	long long int n, t;
	int q;
	cin >>n >>t >>q;
	vector<long long int> w;
	
	int home[100001] = { 0 };
	
	
	for (long long int i = 0; i < n; i++) {
		long long int a;
		cin >> a >> home[i];
		w.push_back(a);
	}
	
	int key[1005] = {0};
	for (int i = 0; i < q; i++) {
		int a;
		cin >> a;
		key[a - 1] = 1;
	}
	int west = 0;
	int east = 0;
	int west2 = 0;
	int east2 = 0;
	vector<long long int> talkpoint;
	long long int tp;
	for (long long int i = 0; i < n; i++) {
		if (i < 50000) {
			if (home[i]==2) {
				if (west==0) {
					
						long long int b = w[i] - t;
						talkpoint.push_back(b);
				
				}
				else {
					talkpoint.push_back(tp);
					
					east = 1;
				}
			}
			if (home[i]==1) {
				
					if (home[i + 1] == 2) {
						tp = (w[i] + w[i + 1]) / 2;
						for (long long int j = 0; j <= east2; j++) {
							talkpoint.push_back(tp);
						}
						west = 1;
					}
					else if (i == n - 1) {
						long long int d = w[i] + t;
						talkpoint.push_back(d);
					}
					else {
						east2++;
						west = 1;
					}
				

			}

		}
	}
	for (long long int i = 0; i < n; i++) {
		if (key[i] == 1) {
			
			long long int e;
			if (talkpoint[i] < w[i]) {
				e = w[i] - talkpoint[i];
			}
			else {
				e = talkpoint[i] - w[i];
			}
			if (e <= t) {
				cout << talkpoint[i] << endl;
			}
			else {
				if (home[i] == 1) {
					cout << w[i] + t << endl;
				}
				else {
					cout << w[i] - t << endl;
				}
			}
		}
	}
	return 0;
}

C++: // 2019/12/05 Tazoe

#include <iostream>
using namespace std;

int main()
{
	int N;
	cin >> N;

	long long T;
	cin >> T;

	int Q;
	cin >> Q;

	long long A[100002];
	for(int i=1; i<=N; i++){
		cin >> A[i];

		int D;
		cin >> D;

		if(D==1){
			A[i] += T;
		}
		else{
			A[i] -= T;
		}
	}
	A[0] = A[1]-1;
	A[N+1] = A[N]+1;

	for(int i=1; i<=N-1; i++){
		if(A[i]<=A[i+1])
			continue;

		A[i] = A[i+1] = (A[i]+A[i+1])/2;

		int j = i;
		while(A[j-1]>A[j]){
			A[j-1] = A[j];
			j--;
		}

		j = i+1;
		while(A[j]>A[j+1]){
			A[j+1] = A[j];
			j++;
		}
	}
/*
	for(int i=1; i<=N; i++){
		cout << A[i] << endl;
	}
*/

	for(int i=0; i<Q; i++){
		int X;
		cin >> X;

		cout << A[X] << endl;
	}

	return 0;
}

C++: #include <bits/stdc++.h>
#define	f first
#define	s second
#define int long long
using namespace	std;
const int INF=1e18;
typedef pair<int,int> P;
signed main(){

  vector<int> match;
  P ad[100000];
  int n,t,q,wh[100000];
  cin>>n>>t>>q;
  for(int i=0,a,b;i<n&&cin>>a>>b;i++) ad[i]=P(a,b);
  for(int i=0;i<n-1;i++) if(ad[i].s==1&&ad[i+1].s==2) match.push_back((ad[i].f+ad[i+1].f)/2);
  for(int i=0;i<n;i++){
    if(ad[i].s==1){
      auto it=lower_bound(match.begin(),match.end(),ad[i].f);
      if(it==match.end()) wh[i]=INF;
      else wh[i]=*it;
    }
    else{
      auto it=upper_bound(match.begin(),match.end(),ad[i].f);
      if(it==match.end()) wh[i]=*match.rbegin();
      else if(it==match.begin()) wh[i]=-INF;
      else wh[i]=*(it-1);
    }
  }
  for(int i=0,a;i<q&&cin>>a;i++){
    a--;
    if(ad[a].s==1){
      if(ad[a].f+t<wh[a]) cout<<ad[a].f+t<<endl;
      else cout<<wh[a]<<endl;
    }
    else{
      if(ad[a].f-t>wh[a]) cout<<ad[a].f-t<<endl;
      else cout<<wh[a]<<endl;
    }
  }
  return 0;
}