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]);
      rleft.push_back(-A[i]);
    } else {
      left.push_back(A[i]);
      rright.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>

using namespace std;

#define INF (1LL << 62)

struct Human {
	int direct;
	long long position;
	long long save;
} human[100001];

int main() {
	long long t;
	int n, q;

	cin >> n >> t >> q;
	for (int i = 1; i <= n; ++i) {
		cout << i << endl;
		cin >> human[i].position >> human[i].direct;
	}
	human[0].direct = 1;
	human[0].position = -INF;
	human[n + 1].direct = 2;
	human[n + 1].position = INF;

	for (int i = 0; i <= n; ++i) {
		if (human[i].direct == 1 && human[i + 1].direct == 2) {
			long long d = (human[i].position + human[i + 1].position) / 2;
			for (int j = i; j >= 1 && human[j].direct == 1; --j) {
				human[j].save = d;
			}
			for (int j = i + 1; j <= n && human[j].direct == 2; ++j) {
				human[j].save = d;
			}
		}
	}

	int id;
	for (int i = 0; i < q; ++i) {
		cin >> id;
		if (human[id].direct == 1) {
			cout << (human[id].position + t < human[id].save ? human[id].position + t : human[id].save) << endl;
		}
		else {
			cout << (human[id].position - t > human[id].save ? human[id].position - t : human[id].save) << endl;
		}
	}

	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;

#define int long long

#define rep(i,n) for(int i=0;i<(n);i++)
#define pb push_back
#define all(v) (v).begin(),(v).end()
typedef vector<int>vint;
typedef pair<int,int>pint;

const int SIZE=100000;


int N,T,Q;
int A[SIZE],D[SIZE];

int dp[SIZE];

signed main(){
    cin>>N>>T>>Q;
    rep(i,N)cin>>A[i]>>D[i];

    rep(i,N){
        if(D[i]!=2)continue;
        if(i==0)dp[i]=A[i]-T;
        else if(D[i-1]==1)dp[i]=max(A[i]-T,A[i]-(A[i]-A[i-1])/2);
        else dp[i]=max(A[i]-T,dp[i-1]);
    }

    for(int i=N-1;i>=0;i--){
        if(D[i]!=1)continue;
        if(i==N-1)dp[i]=A[i]+T;
        else if(D[i+1]==2)dp[i]=min(A[i]+T,A[i]+(A[i+1]-A[i])/2);
        else dp[i]=min(A[i]+T,dp[i+1]);
    }

    while(Q--){
        int x;cin>>x;x--;
        cout<<dp[x]<<endl;
    }
    return 0;
}

C++: #include<bits/stdc++.h>
#define PB push_back
#define MP make_pair
#define FI first
#define SE second
using namespace std;
typedef pair<int,int> pii;
static const int inf = 1ll<<60;
static const int MAX_N = 100000;

int N,T,Q;
vector<pii> A;
int flg[MAX_N+5];
int table[MAX_N+5];
int next[MAX_N+5];

signed main(){
    memset(next,-1,sizeof(next));
    cin>>N>>T>>Q;
    for(int i=0;i<N;++i){
        int a,d;
        cin>>a>>d;
        if(d!=1)d=-1;
        A.PB(pii(a,d));
    }
    sort(A.begin(),A.end());
    int maxd=0;
    int t=0;
    for(int i=0;i<N;++i){
        if(flg[i]==1)continue;
        if(A[i].SE!=1||A[i+1].SE!=-1){
            if(A[i].SE==1)table[i]=t;
            else table[i]=t-1;
            continue;
        }
        int D=abs(A[i].FI-A[i+1].FI)/2;
        if(D<=T){
            maxd=max(D,maxd);
            A[i].FI+=A[i].SE*D;
            A[i+1].FI=A[i].FI;
            flg[i]=flg[i+1]=1;
            next[t]=A[i].FI;
            t++;
        }
    }
    for(int i=0;i<N;++i){
        if(flg[i]==1)continue;
        if(A[i].SE==1)A[i].FI+=maxd;
        else A[i].FI-=maxd;
    }
    T-=maxd;
    for(int i=0;i<N;++i){
        if(flg[i]==1)continue;
        if(A[i].SE==1&&A[i+1].SE==-1){
            A[i].FI+=T;
            A[i-1].SE-=T;
            flg[i]=flg[i+1]=1;
            continue;
        }
        if(A[i].SE==1){
            int k=table[i];
            if(next[k]==-1)A[i].FI+=T;
            else{
                int d=abs(A[i].FI-next[k]);
                if(d<=T)A[i].FI=next[k];
                else A[i].FI+=T;
            }
        }
        else{
            int k=table[i];
            if(next[k]==-1)A[i].FI-=T;
            else{
                int d=abs(A[i].FI-next[k]);
                if(d<=T)A[i].FI=next[k];
                else A[i].FI-=T;
            }
        }
    }
    for(int i=0;i<Q;++i){
        int q;
        cin>>q;
        q--;
        cout<<A[q].FI<<endl;
    }
}