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

C++: #include <algorithm>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define rep(i, n) for(int(i) = 0; (i) < (n); (i)++)
#define FOR(i, m, n) for(int(i) = (m); (i) < (n); (i)++)
#define All(v) (v).begin(), (v).end()
#define pb push_back
#define MP(a, b) make_pair((a), (b))
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
const int INF = 1 << 30;
const ll LINF = 1LL << 60;
const int MOD = 1e9 + 7;

int main() {
    ll N, T, Q;
    cin >> N >> T >> Q;
    vector<ll> A(N), D(N);
    vector<ll> B(N);
    vector<ll> X(Q);
    rep(i, N) cin >> A[i] >> D[i];
    rep(i, Q) {
        cin >> X[i];
        X[i]--;
    }
    ll now = -LINF;
    ll st = 0;
    rep(i, N) {
        if(D[i] == 1) {
            now = A[i];
            st = -LINF;
        } else {
            if(now == -LINF) {
                B[i] = -LINF;
            } else if(st == -LINF) {
                st = (now + A[i]) / 2;
                B[i] = st;
            } else {
                B[i] = st;
            }
        }
    }
    now = LINF;
    st = LINF;
    for(int i = N - 1; i >= 0; i--) {
        if(D[i] == 2) {
            now = A[i];
            st = LINF;
        } else {
            if(now == -LINF) {
                B[i] = LINF;
            } else if(st == LINF) {
                st = (now + A[i]) / 2;
                B[i] = st;
            } else {
                B[i] = st;
            }
        }
    }

    rep(i, Q) {
        if(D[X[i]] == 1) {
            cout << min(A[X[i]] + T, B[X[i]]) << endl;
        } else {
            cout << max(A[X[i]] - T, B[X[i]]) << endl;
        }
    }

    return 0;
}

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

#define int long long
#define loop(i,n) for(int i=1;i<=n;i++)
int a[100002];
int d[100002];

signed main(){
  int n,t,q,x;
  cin>>n>>t>>q;
  loop(i,n)cin>>a[i]>>d[i];
  a[0]=(int)(-5e18);
  a[n+1]=(int)(5e18);
  d[0]=1;
  d[n+1]=2;
  loop(i,q){
    cin>>x;
    if(d[x]==1){
      int y=x;
      while(d[y]==1)y++;
      cout<<min(a[x]+t,(a[y-1]+a[y])/2)<<endl;
    }
    if(d[x]==2){
      int y=x;
      while(d[y]==2)y--;
      cout<<max(a[x]-t,(a[y]+a[y+1])/2)<<endl;
    }
  }
  return 0;
}

C++: #include<iostream>
#include<string>
#include<cstdio>
using namespace std;
int main() {
	long long int n, t;
	int q;
	scanf_s("%lld %lld %d",&n,&t,&q);
	long long int home[100005] = {0};
	int keyman[100005] = { 0 };
	long long int walk[2][100005] = {0};
	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;
}