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 <cmath>
#include <queue>

constexpr int MAX_N = 100000;
constexpr long long MAX_T = 1000000000000000000;
constexpr int MAX_Q = 1000;

typedef std::pair<long long, int> P;

int N, Q, target[MAX_Q];
long long T, ans[MAX_N];
P p[MAX_N];

void solve();

int main(){
	scanf("%d %lld %d", &N, &T, &Q);

	for(int i = 0; i < N; ++i){
		scanf("%lld %d", &p[i].first, &p[i].second);
	}
	for(int i = 0; i < Q; ++i){
		scanf("%d", &target[i]);
	}

	solve();
	return 0;
}

void solve(){
	for(int i = 0; i < N - 1; ++i){
		if(p[i].second == 1 && p[i + 1].second == 2 && p[i + 1].first - p[i].first <= T * 2){
			int tmp = ans[i] = ans[i + 1] = (p[i + 1].first + p[i].first) / 2;
			p[i].second = p[i + 1].second = 0;
			for(int j = i - 1; j >= 0; --j){
				if(p[j].second == 1 && tmp - p[j].first <= T){
					p[j].second = 0;
					ans[j] = tmp;
				}
				else{
					break;
				}
			}
			for(int j = i + 2; j < N; ++j){
				if(p[j].second == 2 && p[j].first - tmp <= T){
					p[j].second = 0;
					ans[j] = tmp;
				}
				else{
					break;
				}
			}
		}
	}

	for(int i = 0; i < N; ++i){
		if(ans[i] == 0 && p[i].second != 0){
			if(p[i].second == 1){
				ans[i] = p[i].first + T;
			}
			else{
				ans[i] = p[i].first - T;
			}
		}
	}

	for(int i = 0; i < Q; ++i){
		printf("%lld\n", ans[target[i] - 1]);
	}
}

C++: #include <bits/stdc++.h>
using namespace std;
const int dx[]={1,0,-1,0,1,-1,-1,1};
const int dy[]={0,1,0,-1,1,1,-1,-1};
const int INF = 1e9;
const long long LINF = 4*1e18;
const double EPS = 1e-8;
#define PB push_back
#define mk make_pair
#define fr first
#define sc second
#define ll long long
#define reps(i,j,k) for(int i = (j); i < (k); ++i)
#define rep(i,j) reps(i,0,j)
#define all(a) (a).begin(),(a).end()
#define MOD 1000000007
typedef pair<int,int> Pii;
typedef vector<int> vi;
typedef pair<string, int> Psi;
typedef pair<ll, ll> Pll;
  
ll A[100001];
int way[100001];
  
vector<ll> points;
  
ll search(int idx,ll T){
    ll l = 0;
    ll r = points.size();
    while(l + 1 != r){
        int med = (l+r)/2;
        if(points[med] > A[idx]){
            r = med;
        }
        else{
            l = med;
        }
    }
  
    int s;
    ll c;
    if(way[idx] == 1){
        s = l + 1;
        c = 1;
    }
    else if(way[idx] == 2){
        s = l;
        c = -1;
    }
    if(T >= abs(A[idx]-points[s])){
        return points[s];
    }
    return A[idx]+c*T;
}
  
int main(){
    int N,Q;
    ll T;
    scanf("%d%lld%d",&N,&T,&Q);
    points.PB(-LINF);
    rep(i,N){
        scanf("%lld%d",&A[i],&way[i]);
        if(i > 0 && way[i-1] == 1 && way[i] == 2){
            points.PB(A[i-1]+(A[i]-A[i-1])/2);
        }
    }
  
    points.PB(LINF);
    rep(i,Q){
        int x;
        scanf("%d",&x);
        --x;
        printf("%lld\n",search(x,T));
    }
  
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < (int)n; i++)
#define repr(i, n) for(int i = n; i>=0; i--)
#define all(x) (x).begin(), (x).end()
#define INF 100100100
typedef long long ll;
typedef pair<int, int> p;
 
int main(void) {
	int n, q, d[10002];
	ll t, a[10002], stop[10002];
	scanf("%d%lld%d", &n, &t, &q);
	for (int i = 1; i <= n; i++) {
		scanf("%lld%d", &a[i], &d[i]);
	}
	a[0] = -1000000000000000000;
	d[0] = 1;
	a[n + 1] = 1000000000000000000;
	d[n + 1] = 2;
	for (int i = 1; i <= n; i++) {
		if (d[i] == 1 && d[i + 1] == 2) {
			ll tmp = (a[i] + a[i + 1]) / 2;
			for (int j = i; j >= 1 && d[j] == 1; j--) {
				if (a[j] + t >= tmp) stop[j] = tmp;
				else stop[j] = a[j] + t;
			}
			for (int j = i + 1; j <= n && d[j] == 2; j++) {
				if (a[j] - t <= tmp) stop[j] = tmp;
				else stop[j] = a[j] - t;
			}
		}
	}
	for (int i = 0; i < q; i++) {
		int x;
		scanf("%d", &x);
		printf("%lld\n", stop[x]);
	}
	
	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
#define MAX_N 100005
typedef long long ll;
ll INF=(2e18);
ll N,T,Q;
ll A[MAX_N],L[MAX_N],R[MAX_N];
int D[MAX_N],target;

int main(){
  scanf("%lld %lld %lld",&N,&T,&Q);
  for(int i=1;i<=N;i++){
    scanf("%lld %d",&A[i],&D[i]);
  }
  L[1]=-INF;
  for(int i=2;i<=N;i++){
    if(D[i]==D[i-1])L[i]=L[i-1];
    else L[i]=(A[i]+A[i-1])/2;
  }
  R[N]=INF;
  for(int i=N-1;i>=1;i--){
    if(D[i]==D[i+1])R[i]=R[i+1];
    else R[i]=(A[i]+A[i+1])/2;
  }
  while(Q--){
    scanf("%d",&target);
    ll ans=A[target];
    if(D[target]==1){
      ans=min(ans+T,R[target]);
    }else{
      ans=max(ans-T,L[target]);
    }
    printf("%lld\n",ans);
  }
  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(b[i]==1&&b[i+1]==2&&a[i+1]-a[i]<t*2){
			flg[i]=1; flg[i+1]=1;
			s[i]=s[i+1]=(a[i]+a[i+1])/2;
		}
	}
	x=0;
	for(i=n;i>=1;i--){
		if(flg[i]==1){
			tt=s[i];	x=1;
		}
		else{
			if(b[i]==1&&x==1&&a[i]+t>tt){
				s[i]=tt;	flg[i]=2;
			}
		}
	}
	x=0;
	for(i=1;i<=n;i++){
		if(flg[i]==1){
			tt=s[i];	x=1;
		}
		else{
			if(b[i]==2&&x==1&&a[i]-t<tt){
				s[i]=tt;	flg[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]]);
}