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;

ll N,T,Q;
set<ll> S;

ll a[100005];
ll d[100005];

int main(){
  cin>>N>>T>>Q;
  for(int i=0;i<N;i++){
    cin>>a[i]>>d[i];
    if(i>0 && d[i-1]==1 && d[i]==2 ){
      S.insert( a[i]/2LL + a[i-1]/2LL);
    }
  }

  ll INF = 1000000000000000001LL;
  S.insert(INF);
  S.insert(-INF);

  for(int i=0;i<Q;i++){
    ll id;
    scanf("%lld",&id);
    id--;
    if( d[id] == 1 ){
      ll target = *S.lower_bound(a[id]);
      cout<<min(a[id]+T,target)<<endl;
    }else{
      set<ll> :: iterator it;
      it=S.lower_bound(a[id]);
      it--;
      ll target = *it;
      cout<<max(a[id]-T,target)<<endl;
    }
  }
  return 0;
}

C++: #include<cstdio>
#include<vector>
#include<map>
#include<algorithm>
using namespace std;


int main(){
  int n,q,d[100000],x[1000];
  long long a[100000],t;
  scanf("%d %lld %d",&n,&t,&q);
  for(int i=0;i<n;i++) scanf("%lld %d",a+i,d+i);
  for(int i=0;i<q;i++){
    scanf("%d",x+i);
    x[i]--;
  }
  vector<long long> v;
  for(int i=0;i<n-1;i++){
    if(d[i]==1&&d[i+1]==2){
      v.push_back((a[i]+a[i+1])/2);
    }
  }
  for(int i=0;i<q;i++){
    int id = x[i];
    long long to;
    if(d[id]==1){
      if(upper_bound(v.begin(),v.end(),a[id])!=v.end()){
        to = *upper_bound(v.begin(),v.end(),a[id]);
        if(to-a[id]<t){
          printf("%lld\n",to);
          continue;
        }
      }
      to = a[id]+t;
    }else if(d[id]==2){
      if(lower_bound(v.begin(),v.end(),a[id])!=v.begin()){
        to = *(lower_bound(v.begin(),v.end(),a[id])-1);
        if(a[id]-to<t){
          printf("%lld\n",to);
          continue;
        }
      }
      to = a[id]-t;
    }
    printf("%lld\n",to);
  }
  return 0;
}

C++: #include<iostream>
#include<string>
#include<cstdio>

using namespace std;
long long int home[100005] = {0};
int keyman[100005] = { 0 };
long long int walk[2][100005] = {0};
int main() {
	long long int n, t;
	int q;
	scanf_s("%lld %lld %d",&n,&t,&q);
	
	for (int i = 0; i < n; i++) {
		scanf_s("%lld %lld", &walk[0][i],&home[i]);
	}
	for (int i = 0; i <q; i++) {
		scanf_s("%lld", &keyman[i]);
	}
	
	for (int i = 1; i <= t; i++) {
		for (int j = 0; j < n; j++) {
			if (walk[1][j] != 1) {
				if (home[j]== 1) {
					walk[0][j]++;
				}
				else {
					walk[0][j]--;
				}
				
			}
			for (int k = 0; k < j; k++) {
					if (walk[0][k] == walk[0][j]) {
						walk[1][k] = 1;
						walk[1][j] = 1;
					}
				}
			/*cout << "nowspot" << walk[0][j] << endl;*/
		}
		for (int x = 0; x < n; x++) {
			if (walk[1][x] == 1) {
				/*cout << "freeze:" << x << endl;*/
			}
		}

	}
	for (int i = 0; i < q; i++) {
		printf("%lld\n", walk[0][keyman[i] - 1]);
	}
	return 0;
}

C++: #include <iostream>
#include <queue>
#include <set>
using namespace std;

struct person{
    long long start;
    long long end;
    long long way;
    bool finished;
};
person person[100005];
long long n,t,q,key[1005];
int main(){
    //cout<<endl;
    cin>>n>>t>>q;
    priority_queue<long long> crash;
    set<long long> set;
    crash.push(-100000000000000000000000);
    for(int i=1; i<=n; i++) {cin>>person[i].start>>person[i].way; person[i].finished=false;}
    for(int i=1; i<=n; i++){
        if(i==1&&person[i].way==2) {person[i].end=person[i].start-t; person[i].finished=true; continue;}
        if(person[i].way==2){
            if(person[i-1].way==1){
                if((person[i].start-person[i-1].start)/2>t) {
                    person[i].end=person[i].start-t;
                    person[i-1].end=person[i-1].start+t;
                }else{
                    person[i].end=(person[i].start+person[i-1].start)/2;
                    person[i-1].end=(person[i].start+person[i-1].start)/2;
                    crash.pop();
                    crash.push((person[i].start+person[i-1].start)/2);
                    for(int j=1; j<i-1; j++){
                        if(person[j].finished!=true&&person[j].way==1) {
                            if(person[j].start+t>=crash.top()){person[j].end=crash.top(); person[j].finished=true;}
                            else person[j].end=person[j].start+t; person[j].finished=true;
                        }
                    }
                }
                person[i].finished=true;
                person[i-1].finished=true;
                continue;
            }else if(person[i].start-t<=crash.top()) {person[i].end=crash.top(); person[i].finished=true; continue;
            }else {
                person[i].end=person[i].start-t;
                person[i].finished=true;
            }}
    }
    for(int i=1; i<=n; i++){
        if(person[i].finished!=true){
            if(person[i].way==1) {person[i].end=person[i].start+t; person[i].finished=true;}
            else {person[i].end=person[i].start-t; person[i].finished=true;}
        }
    }
    //for(int i=1; i<=n; i++) cout<<"person["<<i<<"].end=="<<person[i].end<<endl;
    for(int i=1; i<=q; i++) {
        cin>>key[i];
        cout<<person[key[i]].end<<endl;
    }
}

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

using namespace std;
typedef long long ll;

int main(void) {
  ll n, t, q;
  cin >> n >> t >> q;

  vector<ll> a(n + 2), d(n + 2);
  a[0] = -(1LL << 62);
  d[0] = 1;
  a[n + 1] = 1LL << 62;
  d[n + 1] = 2;
  for (int i = 1; i <= n; i++) {
    cin >> a[i] >> d[i];
  }

  vector<int> x(q);
  for (int i = 0; i < q; i++) {
    cin >> x[i];
  }

  vector<ll> g(n + 2);
  int l = 0, r = 0;
  while (l < n + 2) {
    while (r < n + 2 && d[r] == 1) r++;
    ll goal = (a[r - 1] + a[r]) / 2;
    while (r < n + 2 && d[r] == 2) r++;
    for (int i = l; i < r; i++) {
      g[i] = goal;
    }
    l = r;
  }

  for (int i = 0; i < q; i++) {
    if (abs(a[x[i]] - g[x[i]]) <= t) {
      cout << g[x[i]] << endl;
    } else if (d[x[i]] == 1) {
      cout << a[x[i]] + t << endl;
    } else {
      cout << a[x[i]] - t << endl;
    }
  }

  return 0;
}