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>
#include <algorithm>
using namespace std;

typedef long long ll;

const ll INF = 1ll << 60;
const int MAX_N = 100000;

ll A[MAX_N], D[MAX_N];
ll stops[MAX_N];

int main() {
	ll N, T, Q;
	cin >> N >> T >> Q;

	for (int i = 0; i < N; i++) {
		cin >> A[i] >> D[i];
		stops[i] = INF;
	}

	ll s = -INF;
	if (D[0] == 2) stops[0] = s;

	for (int i = 1; i < N; i++) {

		if (D[i - 1] == 1 && D[i] == 2) {
			s = (A[i] + A[i - 1]) / 2;

			for (int j = i - 1; j >= 0 && D[j] == 1; j--)
				stops[j] = s;
		}
		
		if (D[i] == 2)
			stops[i] = s;

	}

	for (int i = 0; i < Q; i++) {
		ll x;
		cin >> x;
		x--;
		ll ans;
		if (D[x] == 1) ans = min(A[x] + T, stops[x]);
		else ans = max(A[x] - T, stops[x]);
		cout << ans << endl;
	}
}

C++: #include <bits/stdc++.h>
#define	f first
#define	s second
#define int long long
using namespace	std;
const int INF=LLONG_MAX;
typedef pair<int,int> P;
signed main(){

  vector<int> match;
  P ad[100000];
  int n,t,q,wh[100000];
  cin>>n>>t>>q;
  for(int i=0,a,b;i<n&&cin>>a>>b;i++) ad[i]=P(a,b);
  for(int i=0;i<n-1;i++) if(ad[i].s==1&&ad[i+1].s==2) match.push_back((ad[i].f+ad[i+1].f)/2);
  for(int i=0;i<n;i++){
    if(ad[i].s==1){
      auto it=lower_bound(match.begin(),match.end(),ad[i].f);
      if(it==match.end()) wh[i]=INF;
      else wh[i]=*it;
    }
    else{
      auto it=upper_bound(match.begin(),match.end(),ad[i].f);
      if(it==match.end()) wh[i]=*match.rbegin();
      else if(it==match.begin()) wh[i]=-INF;
      else wh[i]=*(it-1);
    }
  }
  for(int i=0,a;i<q&&cin>>a;i++){
    a--;
    if(ad[a].s==1){
      if(ad[a].f+t<wh[a]) cout<<ad[a].f+t<<endl;
      else cout<<wh[a]<<endl;
    }
    else{
      if(ad[a].f-t>wh[a]) cout<<ad[a].f-t<<endl;
      else cout<<wh[a]<<endl;
    }
  }
  return 0;
}

C++: #include<cstdio>
#include<algorithm>
#include<functional>
#include<cstring>
#include<iostream>
using namespace std;
int main(void)
{
	int N,T,Q,house[100001],muki[100001];
	int X[1001];
	long long int list[100001];
	int i,j;
	cin>>N>>T>>Q;
	for(i=1;i<=N;i++) cin>>house[i]>>muki[i];
	for(i=1;i<=Q;i++) cin>>X[i];
	for(i=N;i>=1;i--) {
		if(muki[i]==2) continue;
		if(i==N) list[i]=house[i]+T;
		else if(muki[i+1]==2) list[i]=min(house[i]+T,(house[i]+house[i+1])/2);
		else list[i]=min(house[i]+T,list[i+1]);
	}
	for(i=1;i<=N;i++) {
		if(muki[i]==1) continue;
		if(i==1) list[i]=house[i]-T;
		else if(muki[i-1]==1) list[i]=max(house[i]-T,(house[i]+house[i-1])/2);
		else list[i]=max(house[i]-T,list[i-1]);
	}
	for(i=1;i<=Q;i++) {
		cout<<list[X[i]]<<endl;
	}
	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;
    crash.push(-(1<<21));
    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>
#define INF 1000100010001000
using namespace std;
long long int p[100010],ans[100010],dire[100010],n,t,q,x[100010],mm,s1;


int main(){

    for(int i=0;i<100010;i++)ans[i]=INF;
    mm=INF;
    cin >> n >> t >> q;
    for(int i=1;i<=n;i++)cin >> x[i] >> dire[i];

    for(int i=1;i<=n;i++){
        if(dire[i]==1&&dire[i+1]==2){
            mm=(x[i]+x[i+1])/2;
            ans[i]=mm,ans[i+1]=mm;

            for(int j=i-1;j>0;j--){
                if(dire[j]==1)ans[j]=mm;
                else break;
            }
        }else if(dire[i]==2)ans[i]=mm;
    }

    for(int i=0;i<q;i++){
        cin >> mm;
        s1=x[mm]-ans[mm];
        if(s1<0)s1=-s1;
        //cout << "??????????????? " << x[mm] << "  ?????????????????? "  << x[mm]-t << "  ??¶????????£?????´??? " << ans[mm] << endl;
        if(t<s1){
            if(dire[mm]==1)cout << x[mm]+t << endl;
            else cout << x[mm]-t << endl;
        }else cout << ans[mm] << endl;
    }

return 0;
}