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>
using namespace std;
long long a[200000],b[200000],p[200000],n,T,k;
int main(){
	cin>>n>>T>>k;
	for(int i=1;i<=n;i++)cin>>a[i]>>b[i];
	long long L=1LL<<62,M=0;
	for(int i=n;i>=1;i--){
		if(b[i]==2){L=a[i];M=0;}
		if(b[i]==1 && M==0){L=(a[i]+L)/2;p[i]=L;M++;}
		if(b[i]==1 && M==1){p[i]=L;}
	}
	long long L=-1LL<<62;
	for(int i=1;i<=n;i++){
		if(b[i]==1)L=p[i];
		if(b[i]==2)p[i]=L;
	}
	for(int i=0;i<k;i++){
		int r;cin>>r;
		if(b[r]==1){
			if((p[r]-a[r])>=T)cout<<a[r]+T<<endl;
			else cout<<p[r]<<endl;
		}
		if(b[r]==2){
			if((a[r]-p[r])>=T)cout<<a[r]-T<<endl;
			else cout<<p[r]<<endl;
		}
	}
	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("%lld %lld %d",&n,&t,&q);
	
	for (int i = 0; i < n; i++) {
		scanf("%lld %lld", &walk[0][i],&home[i]);
	}
	for (int i = 0; i <q; i++) {
		scanf("%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<bits/stdc++.h>
using namespace std;
typedef long long int64;
typedef pair< int64, int64 > Pi;
const int64 INF = 3000000000000000000LL;
  
int64 N, T, Q;
vector< Pi > data;
int64 dp[100002];
  
int64 rec(int idx, const int d)
{
  if(~dp[idx]) {
    return(dp[idx]);
  } else if(data[idx].second != data[idx + d].second) {
    return(dp[idx] = (data[idx].first + data[idx + d].first) / 2LL);
  } else {
    return(dp[idx] = rec(idx + d, d));
  }
}
  
int main()
{
  memset(dp, -1, sizeof(dp));
  cin >> N >> T >> Q;
  data.push_back(Pi(-INF, 1));
  for(int i = 0; i < N; i++) {
    int64 A, D;
    cin >> A >> D;
    data.push_back(Pi(A, D));
  }
  data.push_back(Pi(INF, 2));
  while(Q--) {
    int64 X;
    cin >> X;
    if(data[X].second == 1) cout << min(data[X].first + T, rec(X, +1)) << endl;
    else                    cout << max(data[X].first - T, rec(X, -1)) << endl;
  }
}

C++: #include<stdio.h>


int main() {
	int N, T, Q, flag = 0;
	long int A[10000], D[10000], X[10000];
	scanf("%d %d %d", &N, &T, &Q);
	for (int i = 1; i <= N; i++) {
		scanf("%d %d", &A[i], &D[i]);
	}
	for (int i = 1; i <= Q; i++) {
		scanf("%d", &X[i]);
	}
	for (int i = 1; i <= Q; i++) {
		if (D[X[i]] == 1) {
			for (int j = X[i]+1; j <= N;j++) {
				if (D[j] == 2) {
					if ((A[j-1] + A[j]) / 2 <= A[X[i]] + T) {
						printf("%ld\n", (A[j-1] + A[j]) / 2);
						flag = 1;
					}
					break;
				}
			}
			if (flag == 0) {
				printf("%ld\n", A[X[i]] + T);
			}
		}
		if (D[X[i]] == 2) {
			for (int j = X[i] -1; j >=1; j--) {
				if (D[j] == 1) {
					if ((A[j+1] + A[j]) / 2 >= A[X[i]] - T) {
						printf("%ld\n", (A[j+1] + A[j]) / 2);
						flag = 1;
					}
					break;
				}
			}
			if (flag == 0) {
				printf("%ld\n", A[X[i]] - T);
			}
	    }
		flag = 0;
    }
	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] = -1000000000000000000LL;
	d[0] = 1;
	a[n + 1] = 1000000000000000000LL;
	d[n + 1] = 2;
	for (int i = 0; i <= n; i++) {
		if (d[i] == 1 && d[i + 1] == 2) {
			ll tmp = (a[i] + a[i + 1]) / 2;
			for (int j = i; j >= 0 && 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 + 1 && 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>
#define ll long long
#define rep(i,n) for(int i=0;i<n;i++)
#define INF 100000005
#define MAX 5001
#define fi first
#define se second
using namespace std;
//__gcd(a,b), __builtin_popcount(a);

pair<ll, int> p[100002];
ll x[1002], d[100002];
int main(){
	ll n, T, Q, t1, t2;
	scanf("%lld%lld%lld", &n, &T, &Q);
	for(int i = 0;i < n;i++){
		scanf("%lld%d", &p[i].fi, &p[i].se);
	}
	for(int i = 0;i < Q;i++)scanf("%lld", &x[i]);
	
	ll f[2] = {0, 0}, tt;
	for(int i = 0;i < n;i++){
		if(p[i].se == 1 && f[0] == 0)f[0] = 1;
		if(p[i].se == 2 && f[0] == 1){
			f[1] = (p[i-1].fi+p[i].fi)/2;
			tt = i-1;
			while(tt != -1 && p[tt].se == 1){
				d[tt] = f[1];
				tt--;
			}
			tt = i;
			while(tt < n && p[tt].se == 2){
				d[tt] = f[1];
				tt++;
			}
			i = tt;
		}
		if(i == n-1){
			tt = i;
			while(tt != -1 && p[tt].se == 1){
				d[tt] = p[i].fi+T;
				tt--;
			}
		}
		if(p[i].se == 2 && f[0] == 0)d[i] = p[i].fi-T;
	}
	
	//for(int i = 0;i < n;i++)printf("%d ", d[i]);
	ll tmp = 0;
	for(int i = 0;i < Q;i++){
		tmp = x[i]-1;
		if(p[tmp].fi < d[tmp])d[tmp] = min(p[tmp].fi+T, d[tmp]);
		if(p[tmp].fi > d[tmp])d[tmp] = max(p[tmp].fi-T, d[tmp]);
		printf("%lld\n", d[tmp]);
	}
	return 0;
}