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>
using namespace std;
#define M 100000


long long int n,t,q;
long long int a[M],d[M];
long long int x;
long long int m[M]={0};

long long int meet(int i){
    for (int j=i+1; j<n; j++) {
        if(d[j]==2){
            m[i]=(a[j]+a[j-1])/2;
            return m[i];
        }
    }
    m[i]=a[i]+t;
    return m[i];
}

void output(){
    for (int i=0; i<q; i++) {
        cin>>x;
        x--;
        if(d[x]==1){
            if(a[x]+t<=m[x])cout<<a[x]+t<<endl;
            else cout<<m[x]<<endl;
        }
        else{
            if(a[x]-t>=m[x])cout<<a[x]-t<<endl;
            else cout<<m[x]<<endl;
        }
    }
}

int main(){
    cin>>n>>t>>q;
    for (int i=0; i<n; i++)cin>>a[i]>>d[i];
    long long int tt=a[0]-t;
    for (int i=0; i<n-1; i++) {
        if(d[i]==1)tt=meet(i);
        if(d[i]==2)m[i]=tt;
    }
    if(d[n-1]==1)m[n-1]=a[n-1]+t;
    else m[n-1]=tt;
    output();
    return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>

static const int MOD = 1000000007;
using ll = long long;
using u32 = unsigned;
using namespace std;

template<class T>
constexpr T INF = ::numeric_limits<T>::max() / 64 * 15 + 208;

int main() {
    ll n, t, q;
    cin >> n >> t >> q;
    vector<ll> v(n+2), u(n+2), ans(n+2, -INF<ll>);
    v[0] = -INF<ll>; u[0] = 1;
    v[n+1] = INF<ll>; u[n+1] = 2;
    for (int i = 1; i <= n; ++i) {
        cin >> v[i] >> u[i];
    }
    int l = 0, m = 1;
    ll tmp = 0;
    for (int i = 0; i <= n+1; ++i) {
        if(m == u[i]) continue;
        if(u[i] == 2){
            tmp = (v[i]+v[i-1])/2;
            m = 2;
        }else {
            for (int j = l; j <= i-1; ++j) {
                ans[j] = tmp;
            }
            l = i;
            m = 1;
        }
    }
    if(ans[n+1] == -INF<ll>) for (int j = l; j <= n+1; ++j) {
        ans[j] = tmp;
    }
    for (int i = 0; i < q; ++i) {
        ll x;
        cin >> x;
        if(abs(v[x]-ans[x]) < t) printf("%lld\n", ans[x]);
        else printf("%lld\n", v[x] + (v[x] > ans[x] ? -t : t));
    }
    return 0;
}

C++: #include<iostream>
using namespace std;
typedef long long ll;
struct person{ll s,e,d;};
int main(){
  ll n,i,j,k,t,q;
  cin >> n >> t >> q;
  person ps[100001];
  ll p1=-1,p2=-1;
  for(i=0;i<n;i++){
    cin >> ps[i].s >> ps[i].d;
    if(ps[i].d==1) p1=i;
    else{
      ps[i].e=ps[i].s-t;
      if(p1==-1){
      }else if(p2!=-1){
        if(ps[p1].s<ps[p2].s) 
          ps[i].e=max(ps[i].e,ps[p2].e);
        else 
          ps[i].e=max(ps[i].e,(ps[i].s+ps[p1].s)/2);
      }else{
        ps[i].e=max(ps[i].e,(ps[i].s+ps[p1].s)/2);
      }
      p2=i;
    }
  }
  p1=-1;p2=-1;
  for(i=n-1;i>=0;i--){
    if(ps[i].d==2) p2=i;
    else{
      ps[i].e=ps[i].s+t;
      if(p2==-1){
      }else if(p1!=-1){
        if(ps[p1].s<ps[p2].s) 
          ps[i].e=min(ps[i].e,ps[p1].e);
        else 
          ps[i].e=min(ps[i].e,(ps[i].s+ps[p2].s)/2);
      }else{
        ps[i].e=min(ps[i].e,(ps[i].s+ps[p2].s)/2);
      }
      p1=i;
    }
  }
  //cout<<endl;
  /*
  for(i=0;i<n;i++){
    cout << ps[i].e << endl;
  }
  cout << endl;
  */
  for(i=0;i<q;i++){
    cin >> k;
    cout << ps[k-1].e << endl;
  }
  return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;

typedef long long ll;

int main()
{
  ll N, T, Q;
  cin >> N >> T >> Q;
  vector<ll> A(N), D(N);
  vector<ll> left, right;
  vector<ll> rleft, rright;
  for (int i = 0; i < N; i++) {
    cin >> A[i] >> D[i];
    if (D[i] == 1) {      
      right.push_back(A[i]);
      rright.push_back(-A[i]);
    } else {
      left.push_back(A[i]);
      rleft.push_back(-A[i]);
    }
  } 
  
  sort(left.begin(), left.end());
  sort(right.begin(), right.end());
  
  sort(rleft.begin(), rleft.end());
  sort(rright.begin(), rright.end());

  ll q;
  for (int i = 0; i < Q; i++) {
    cin >> q; q--;    
    if (D[q] == 1) { // go to the right
      // use left 
      ll l = lower_bound(left.begin(), left.end(), A[q]) - left.begin();
      if (l == left.size()) {
	cout << A[q] + T << endl;
	continue;
      }
      // left[l]      
      ll ub = upper_bound(right.begin(), right.end(), left[l]) - right.begin()-1;
      
      // left is now, right is l
      ll now = (right[ub] != A[q] ? right[ub] : A[q]); // right
      if (right[ub] != A[q]) {
	ll g;
	if (now + T >= left[l] - T) {
	  g = (now + left[l]) / 2;
	} else {
	  g = now + T;
	}
	cout << min(A[q] + T, g) << endl;
      } else {
	if (now + T >= left[l] - T) {
	  cout << (now + left[l]) / 2 << endl;
	} else {
	  cout << now + T << endl;
	}
      }   
    } else {  // go to the left
      // use right
      // use left 
      ll l = lower_bound(rleft.begin(), rleft.end(), A[q]) - rleft.begin();
      if (l == rleft.size()) {
	cout << A[q] + T << endl;
	continue;
      }
      // left[l]      
      ll ub = upper_bound(rright.begin(), rright.end(), rleft[l]) - rright.begin()-1;
      
      // left is now, right is l
      ll now = (rright[ub] != A[q] ? rright[ub] : A[q]); // right
      if (rright[ub] != A[q]) {
	ll g;
	if (now - T <= rleft[l] + T) {
	  g = (now - rleft[l]) / 2;
	} else {
	  g = now - T;
	}
	cout << max(A[q] - T, g) << endl;
      } else {
	if (now - T <= rleft[l] + T) {
	  cout << (now - rleft[l]) / 2 << endl;
	} else {
	  cout << now - T << endl;
	}
      } 

      
      
    }
  }  
  return 0;
}