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 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("%d\n", d[tmp]);
	}
	return 0;
}

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("%d\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("%d\n", (A[j+1] + A[j]) / 2);
						flag = 1;
					}
					break;
				}
			}
			if (flag == 0) {
				printf("%d\n", A[X[i]] - T);
			}
	    }
		flag = 0;
    }
	return 0;
}

C++: #include <stdio.h>
#include <algorithm>

long long int N, T, Q;
long long int A[112345], D[112345];
long long int X[1123];
long long int t[112345];

int main(void) {
	scanf("%lld%lld%lld", &N, &T, &Q);
	for (long long int i = 0; i < N; ++i) { scanf("%lld%lld", &A[i], &D[i]); }
	for (long long int i = 0; i < Q; ++i) { scanf("%lld", &X[i]); }

	for (long long int i = 0; i < N - 1; ++i) {
		if (D[i] == 1 && D[i + 1] == 2) {
			long long int t_mem = (A[i] + A[i + 1]) / 2;
			t[i] = t_mem;
			t[i + 1] = t_mem;
			for (long long int j = i - 1; j >= 0; --j) {
				if (D[j] == 1) {
					t[j] = t_mem;
				}
				else { break; }
			}
			for (long long int j = i + 1; j < N; ++j) {
				if (D[j] == 2) {
					t[j] = t_mem;
				}
				else { break; }
			}
		}
	}

	for (long long int i = 0; i < Q; ++i) {
		if(T<std::abs(A[X[i]]-t[X[i]])){
			if (D[X[i]] == 1) {
				t[X[i]] = A[X[i]] + T;
			}
			if (D[X[i]] == 2) {
				t[X[i]] = A[X[i]] - T;
			}
		}
	}

	for (long long int i = 0; i < Q; ++i) {
		printf("%lld\n", t[X[i] - 1]);
	}

	return 0;
}

C++: #include<stdio.h>


int main() {
	int N, T, Q;
	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]);
		if (D[i] == 2) D[i] = -1;
	}
	for (int i = 1; i <= Q; i++) {
		scanf("%d", &X[i]);
	}
	for (int i = 1; i <= T; i++) {
		for (int j = 1; j <= N; j++) {
			A[j] += D[j];
			if (A[j] == A[j + D[j]]) {
				D[j] = 0;
				D[j + D[j]] = 0;
			}
		}
	}
	for (int i = 1; i <= Q; i++) {
		printf("%d\n", A[X[i]]);
	}
	
	return 0;
}

C++: #include <stdio.h>
#include <algorithm>

long long int N, T, Q;
long long int A[112345], D[112345];
long long int X[1123];
long long int t[112345];

int main(void) {
	scanf("%lld%lld%lld", &N, &T, &Q);
	for (long long int i = 0; i < N; ++i) { scanf("%lld%lld", &A[i], &D[i]); }
	for (long long int i = 0; i < Q; ++i) { scanf("%lld", &X[i]); }

	for (long long int i = 0; i < N - 1; ++i) {
		if (D[i] == 1 && D[i + 1] == 2) {
			long long int t_mem = (A[i] + A[i + 1]) / 2;
			t[i] = t_mem;
			t[i + 1] = t_mem;
			for (long long int j = i - 1; j >= 0; --j) {
				if (D[j] == 1) {
					t[j] = t_mem;
				}
				else { break; }
			}
			for (long long int j = i + 1; j < N; ++j) {
				if (D[j] == 2) {
					t[j] = t_mem;
				}
				else { break; }
			}
		}
	}

	for (long long int i = 0; i < N; ++i) {
		if(T<std::abs(A[i]-t[i])){
			if (D[i] == 1) {
				t[i] = A[i] + T;
			}
			if (D[i] == 2) {
				t[i] = A[i] - T;
			}
		}
	}

	for (long long int i = 0; i < Q; ++i) {
		printf("%d\n", t[X[i] - 1]);
	}

	return 0;
}

C++: #include <bits/stdc++.h>
#define int long long
#define r(i,n) for(int i=0;i<n;i++)
using namespace std;
int n,t,q,x[100001],y[100001],p,z;
vector<int>v;
main(){
	cin>>n>>t>>q;
	v.push_back(-1e18);
	r(i,n){
		scanf("%lld%lld",&x[i],&y[i]);
		if(i&&y[i]!=y[i-1])v.push_back((x[i]+x[i-1])/2);
	}
	v.push_back(1e18);
	r(i,q){
		scanf("%lld",&p);p--;
		if(y[p]==1){
			z=*lower_bound(v.begin(),v.end(),x[p]);
			if(t+x[p]<z)cout<<t+x[p]<<endl;
			else cout<<z<<endl;
		}
		else{
			z=*(lower_bound(v.begin(),v.end(),x[p])-1);
			if(x[p]-t>z)cout<<x[p]-t<<endl;
			else cout<<z<<endl;
		}
	}
}