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;

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;
}

C++: #include<bits/stdc++.h>
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
#define pb push_back
int dy[]={0, 0, 1, -1, 1, 1, -1, -1};
int dx[]={1, -1, 0, 0, 1, -1, -1, 1};

#define FOR(i,a,b) for (int i=(a);i<(b);i++)
#define RFOR(i,a,b) for (int i=(b)-1;i>=(a);i--)
#define REP(i,n) for (int i=0;i<(n);i++)
#define RREP(i,n) for (int i=(n)-1;i>=0;i--)
#define mp make_pair
#define fi first
#define sc second
ll n,t,q,a[100100],d[100100];
ll y[100100];
bool done[100100];
ll saiki(ll i){
    if(done[i])
        return y[i];

    if(i + d[i] < 0){
        done[i] = true;
        return y[i] = a[i] - t;
    }
    if(i + d[i] >= n){
        done[i] = true;
        return y[i] = a[i] + t;
    }

    if(d[i + d[i]] * d[i] == -1){
        done[i] = true;
        return y[i] = a[i] + d[i] * min(t,abs(a[i + d[i]] - a[i]) / 2);
    }
    y[i] = a[i] + d[i] * min(t,abs(saiki(i + d[i]) - a[i]));
    done[i] = true;
    return y[i];
}
int main(){
    cin >> n >> t >> q;
    REP(i,n){
        cin >> a[i] >> d[i];
        if(d[i] == 2)
            d[i] = -1;
    }
    REP(i,n)
        done[i] = false;
    REP(i,n){
        saiki(i);
    }
    REP(i,q){
        int x;
        cin >> x;
        x--;

        cout << y[x] << endl;
    }
    return 0;
}

C++: #include<stdio.h>
long long t,tt;
long long a[100001],s[100001];
int b[100001],flg[100001];
int main(void)
{
	int n,q,i,j,c[1000],x;
	scanf("%d %lld %d",&n,&t,&q);
	for(i=1;i<=n;i++){
		scanf("%lld %d",&a[i],&b[i]);
		flg[i]=0;
	}	
	for(i=1;i<=q;i++)	scanf("%d",&c[i]);
	for(i=1;i<n;i++){
		if(i+1<=n){
			if(b[i]==1&&b[i+1]==2){
				flg[i]=1; flg[i+1]=1;
				s[i]=s[i+1]=(a[i]+a[i+1])/2;
			}
		}
	}
	x=0;
	for(i=q;i>0;i--){
		if(flg[c[i]]==1){
			tt=s[c[i]];	x=1;
		}
		else{
			if(b[c[i]]==1&&x==1&&a[c[i]]+t>tt){
				s[c[i]]=tt;	flg[c[i]]=2;
			}
		}
	}
	x=0;
	for(i=1;i<=q;i++){
		if(flg[c[i]]==1){
			tt=s[c[i]];	x=1;
		}
		else{
			if(b[c[i]]==2&&x==1&&a[c[i]]-t<tt){
				s[c[i]]=tt;	flg[c[i]]=2;
			}
		}
	}
	for(i=1;i<=n;i++){
		if(flg[i]==0){
			if(b[i]==1) s[i]=a[i]+t;
			else s[i]=a[i]-t;
		}
	}	
	for(i=1;i<=q;i++) printf("%lld\n",s[c[i]]);
}

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