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 <iomanip>
#include <string>
#include <vector>
#include <array>
#include <queue>
#include <algorithm>
#include <utility>
#include <cmath>
#include <map>
#include <set>

#define INF_LL 9000000000000000000
#define INF 2000000000

#define REP(i, n) for(int i = 0;i < (n);i++)
#define FOR(i, a, b) for(int i = (a);i < (b);i++)

using namespace std;

typedef long long ll;
typedef pair<int, int> PII;

class Union_find{
private:
	vector<int> par;
	vector<int> rank;
	int n;

public:
	Union_find(int a){
		n = a;
		for(int i = 0;i < n;i++){
			par.push_back(i);
			rank.push_back(0);
		}
	}

	int find(int x){
		if(par[x] == x){
			return x;
		}else{
			return par[x] = find(par[x]);
		}
	}

	void unite(int x, int y){
		x = find(x);
		y = find(y);
		if(x == y) return;

		if(rank[x] < rank[y]){
			par[x] = y;
		}else{
			par[y] = x;
			if(rank[x] == rank[y]) rank[x]++;
		}
	}

	bool same(int x, int y){
		return find(x) == find(y);
	}
};

int main(void){
	ll N, T, Q;
	ll A[100000], D[100000];
	ll X[100000];
	vector<ll> p;
	cin >> N >> T >> Q;
	REP(i, N){
		cin >> A[i] >> D[i];
	}
	REP(i, Q){
		cin >> X[i];
	}

	REP(i, N-1){
		if(D[i] == 1 && D[i+1] == 2){
			p.push_back((A[i]+A[i+1])/2);
		}
	}
	REP(i, Q){
		if(D[X[i]-1] == 1){
			REP(j, p.size()){
				if(p[j] >= A[X[i]-1]){
					if(p[j] - A[X[i]-1] <= T){
						cout << p[j] << endl;
					}else{
						cout << A[X[i]-1] + T << endl;
					}
					break;
				}
			}
		}else{
			REP(j, p.size()){
				if(p[j] > A[X[i]-1]){
					if(A[X[i]-1] - p[j-1] <= T){
						cout << p[j-1] << endl;
					}else{
						cout << p[j-1] - T << endl;
					}
					break;
				}
			}
		}
	}
}

C++: //
//  main.cpp

#include <iostream>
using namespace std;
using ll = long long;

int main(){	
	int N,Q;
	ll T;
	cin >> N >> T >> Q;
	
	ll people[1001];
	ll A[100001];
	int D[100001];
	for(int i=0;i<N;i++){
		cin >> A[i] >> D[i];
	}
	for(int i=0;i<Q;i++){
		cin >> people[i];
		people[i]--;
	}
	
	ll east = 0;
	ll west = 0;
	ll t = 0;
	for(int i=0;i<Q;i++){
		if(D[people[i]] == 1){
			east = A[people[i]];
			for(ll j=people[i]+1;j<N;j++){
				if(D[j] == 1){
					east = A[j];
				}else{
					t = (east + A[j])/2;
					break;
				}
			}
			if(T < abs(t - A[people[i]])){
				cout << A[people[i]] + T << endl;
			}else{
				cout << t << endl;
			}
		}else{
			west = A[people[i]];
			for(ll j=people[i]-1;j>=0;j--){
				if(D[j] == 1){
					t = (west + A[j])/2;
					break;
				}else{
					west = A[j];
				}
			}
			if(T < abs(A[people[i]] - t)){
				cout << A[people[i]] - T << endl;
			}else{
				cout << t << endl;
			}
		}
	}

	return 0;
}

C++: #include <cstdio>
#include <vector>
#include <cstdlib>
#include <algorithm>
using namespace std;

int n, t, q;
long long int a[100000];
int d[100000];
int x[100000];

vector<long long int> v;

int main(){
	scanf("%d %d %d", &n, &t, &q);
	for(int i=0; i<n; i++) scanf("%lld %d", &a[i], &d[i]);
	for(int i=0; i<q; i++) scanf("%d", &x[i]);

	bool f = false;
	long long int p;
	for(int i=0; i<n; i++){
		if(d[i] == 1){
			p = a[i];
			f = true;
		}else if(f){
			v.push_back((p + a[i]) / 2);
			f = false;
		}
	}

	long long int s;
	for(int i=0; i<q; i++){
		if(d[x[i]-1] == 1){
			s = *upper_bound(v.begin(), v.end(), a[x[i]-1]);
			if(a[x[i]-1] < s && s - a[x[i]-1] < t) printf("%lld\n", s);
			else printf("%lld\n", a[x[i]-1] + t);
		}else{
			s = *(lower_bound(v.begin(), v.end(), a[x[i]-1])-1);
			if(s < a[x[i]-1] && a[x[i]-1] - s < t) printf("%lld\n", s);
			else printf("%lld\n", a[x[i]-1] - t);
		}
	}
/*
	printf("\n");
	for(int i=0; i<v.size(); i++)
		printf("%lld\n", v[i]);
*/	return 0;
}

C++: #include<bits/stdc++.h>
#define FF first.first
#define FS first.second
#define SF second.first
#define SS second.second
#define LL long long 
using namespace std;
typedef pair<LL,int>P1;
typedef pair<bool,LL>P2;
typedef pair<P1,P2>PP;
PP pp[100001];
int main(void){
	int n;
	LL t;
	int q;
	int vip[1001];
	int i,j;
	cin>>n>>t>>q;
	for(i=0;i<n;i++){
		cin>>pp[i].FF>>pp[i].FS;
		pp[i].SF=false;
		pp[i].SS=0;
	}
	for(i=0;i<q;i++){
		cin>>vip[i];
		vip[i]--;
	}
	for(i=0;i<n-1;i++){
		if(pp[i].FS<pp[i+1].FS){
			//cout<<pp[i].FF<<" "<<pp[i+1].FF<<endl;
			LL sa=(pp[i].FF+pp[i+1].FF)/2;
			//cout<<sa<<endl;
			if(sa<pp[i].FF+t){
				pp[i].SF=true;
				pp[i].SS=sa;
				pp[i+1].SF=true;
				pp[i+1].SS=sa;
			}
		}
	}
	for(i=0;i<q;i++){
		//cout<<"PPVIP="<<pp[vip[i]].FF<<endl;
		//cout<<"PPFSVIP="<<pp[vip[i]].FS<<endl;
		LL x=pp[vip[i]].FF;
		//cout<<pp[vip[i]].SS<<endl;
		if(!pp[vip[i]].SF){
			if(pp[vip[i]].FS==2){
				j=vip[i]-1;
				while(1){
					if(pp[j].SF){
			//			cout<<"X-T="<<x-t<<pp[j].SS<<endl;
						if(x-t<pp[j].SS){
							//pp[vip[i]].SS=x-t;
							pp[vip[i]].SS=pp[j].SS;
						}
						else{
							//pp[vip[i]].SS=pp[j].SS;
							pp[vip[i]].SS=x-t;
						}
						break;
					}
					if(j<0)	{
						pp[vip[i]].SS=x-t;
						break;
					}
					
					j--;
				}
			}
			else{
				j=vip[i];
				while(1){
					if(pp[j].SF){
				//		cout<<"X+T="<<x+t<<" "<<pp[j].SS<<endl;
						if(x+t>pp[j].SS){
							pp[vip[i]].SS=pp[j].SS;
						}
						else{
							pp[vip[i]].SS=x+t;
						}
						break;
					}
					if(j==n){
						pp[vip[i]].SS=x+t;
						break;
					}
					j++;
				}
			}
		}
	}
		
		/*for(j=0;j<n;j++){
			
			if(pp[j].SF){
				cout<<"j="<<j<<endl;
				cout<<"PPFSVIP="<<pp[vip[i]].FS<<endl;
				
					cout<<"X-T="<<x-t<<pp[j].SS<<endl;
					if(x-t<pp[j].SS&&(x-t>=0&&pp[j].SS>=0)||(x-t<0&&pp[j].SS<0)){
						pp[vip[i]].SS=pp[j].SS;
					}
					else{
						pp[vip[i]].SS=x-t;
					}
				}
				else{
					cout<<"X+T="<<x+t<<" "<<pp[j].SS<<endl;
					if(x+t>pp[j].SS&&(x+t>=0&&pp[j].SS>=0)||(x+t<0&&pp[j].SS<0)){
						pp[vip[i]].SS=pp[j].SS;
					}
					else{
						pp[vip[i]].SS=x+t;
					}
				}
			}
		}
	}*/
	
	for(i=0;i<q;i++){
		cout<<pp[vip[i]].SS<<endl;
	}
	return 0;
}