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<string>
#include<cstdio>

using namespace std;
long long int home[1005] = {0};
int keyman[1005] = { 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 <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <algorithm>
#include <utility>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <functional>

using namespace std;

#define int long long

#define fst first
#define scd second
#define PB push_back
#define MP make_pair
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define omajinai ios::sync_with_stdio(false);cin.tie(0)
#define rep(i,x) for(int i=0;i<(int)(x);++i)
#define rep1(i,x) for(int i=1;i<=(int)(x);++i)

typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
typedef vector<pii> vpii;

template<typename T>T& max(T&a,T&b){if(a>=b)return a;return b;}
template<typename T>T& min(T&a,T&b){if(a<b)return a;return b;}
template<typename T>bool chmax(T&a,T b){if(a<b){a=b;return true;}return false;}
template<typename T>bool chmin(T&a,T b){if(a>b){a=b;return true;}return false;}
template<typename T>T get(){T a;cin>>a;return a;}
template<typename T>T rev(T a){reverse(all(a));return a;}
template<typename T>vector<T>&sort(vector<T>&a){sort(all(a));return a;}

const int inf = 1e9;
const ll linf = 3e18;
const double eps = 1e-9;

int A[100010], D[100010];

vector<int> kabe;

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

    rep(i, N) {
        cin >> A[i] >> D[i];
    }

    rep(i, N - 1) {
        if (D[i] == 1 && D[i + 1] == 2) {
            kabe.PB((A[i] + A[i + 1]) / 2);
        }
    }

    rep(i, Q) {
        int x; cin >> x;
        -- x;

        if (D[x] == 1) {
            vi::iterator itr = upper_bound(all(kabe), A[x]);
            if (itr == kabe.end()) {
                cout << A[x] + T << endl;
            } else {
                cout << min(A[x] + T, *itr) << endl;
            }
        } else {
            vi::iterator itr = lower_bound(all(kabe), A[x]);

            if (itr == kabe.begin()) {
                cout << A[x] - T << endl;
            } else {
                itr -= 1;
                cout << max(A[x] - T, *itr) << endl;
            }
        }
    }
}

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

#define REP(i,n) for(int i=0;i<n;i++)
#define FOR(i,m,n) for(int i=m;i<n;i++)
#define FORR(i,m,n) for(int i=m;i>=n;i--)
#define pb(x) push_back(x) 
#define SORT(x) sort((x).begin(),(x).end())
#define INF 999999999

using namespace std;

#define int long long

struct ad{
	int a;
	int d;
};

int32_t main(){
	int n, t, q;
	vector<struct ad> house;
	vector<int> stop;
	cin >> n >> t >> q;
	REP(i, n){
		int a, d;
		cin >> a >> d;
		house.push_back((ad){a, d});
		if(i && house[i-1].d == 1 && house[i].d == 2){
			stop.push_back((house[i].a + house[i-1].a)/2);
		}
	}

	REP(i, q){
		int x;
		cin >> x;
		x--;
		if(house[x].d == 1){
			auto st = lower_bound(stop.begin(), stop.end(), house[x].a);
			if(st == stop.end()) cout << house[x].a + t << endl; 
			else cout << min(*st, house[x].a + t) << endl;
		}
		else{
			auto st = lower_bound(stop.begin(), stop.end(), house[x].a);
			if(st == stop.begin()) cout << house[x].a - t << endl; 
			else cout << max(*(st-1), house[x].a - t) << endl;
		}
	}
}

C++: #include<stdio.h>
long long t,tt;
long long a[100001],s[100001];
int b[100001],flg[100001];
int main(void)
{
	int n,q,i,j,c[1000],x;
	scanf("%d %lld %d",&n,&t,&q);
	for(i=1;i<=n;i++){
		scanf("%lld %d",&a[i],&b[i]);
		flg[i]=0;
	}	
	for(i=1;i<=q;i++)	scanf("%d",&c[i]);
	for(i=1;i<n;i++){
		if(b[i]==1&&b[i+1]==2&&a[i+1]-a[i]<t*2){
			flg[i]=1; flg[i+1]=1;
			s[i]=s[i+1]=(a[i]+a[i+1])/2;
		}
	}
	x=0;
	for(i=q;i>=1;i--){
		if(flg[c[i]]==1){
			tt=s[c[i]];	x=1;
		}
		else{
			if(b[c[i]]==1&&x==1&&a[c[i]]+t>tt){
				s[c[i]]=tt;	flg[c[i]]=2;
			}
		}
	}
	x=0;
	for(i=1;i<=q;i++){
		if(flg[c[i]]==1){
			tt=s[c[i]];	x=1;
		}
		else{
			if(b[c[i]]==2&&x==1&&a[c[i]]-t<tt){
				s[c[i]]=tt;	flg[c[i]]=2;
			}
		}
	}
	for(i=1;i<=n;i++){
		if(flg[i]==0){
			if(b[i]==1) s[i]=a[i]+t;
			else s[i]=a[i]-t;
		}
	}	
	for(i=1;i<=q;i++) printf("%lld\n",s[c[i]]);
}

C++: #include<iostream>
#include<cmath>
using namespace std;
long long N, T, Q, a[1 << 17], b[1 << 17], c[1 << 17];
int main() {
	cin >> N >> T >> Q;
	for (int i = 0; i < N; i++)cin >> a[i] >> b[i];
	long long G = 1LL << 62, H = 1 << 62;
	for (int i = N - 1; i >= 0; i--) {
		if (b[i] == 2)G = a[i];
		if (b[i] == 1) {
			long long C = (a[i] + G) / 2;
			if (C - a[i] >= T)c[i] = a[i] + T;
			else { c[i] = C; }
			if (i < N - 1) {
				if (b[i] == b[i + 1] && abs(H - a[i]) <= T)c[i] = H;
			}
			H = c[i];
		}
	}
	G = -1 << 62, H = -1 << 62;
	for (int i = 0; i < N; i++) {
		if (b[i] == 1)G = a[i];
		if (b[i] == 2) {
			long long C = (a[i] + G) / 2;
			if (a[i] - C >= T)c[i] = a[i] - T;
			else { c[i] = C; }
			if (i >= 1) {
				if (b[i] == b[i - 1] && abs(H - a[i]) <= T)c[i] = H;
			}
			H = c[i];
		}
	}
	for (int i = 0; i < Q; i++) {
		int p; cin >> p; p--; cout << c[p] << endl;
	}
	return 0;
}