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<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<=q;i++){
		if(flg[c[i]]==0){
			if(b[c[i]]==1) s[c[i]]=a[c[i]]+t;
			else s[c[i]]=a[c[i]]-t;
		}
	}	
	for(i=1;i<=q;i++) printf("%lld\n",s[c[i]]);
}

C++: #include "bits/stdc++.h"
#define in std::cin
#define out std::cout
#define rep(i,N) for(LL i=0;i<N;++i)
typedef long long int LL;

struct group { LL begin, end, pos; };

LL N, T, Q, A[112345], D[112345], X[1123];
std::vector<group>list;

int main()
{
	in >> N >> T >> Q;
	rep(i, N) { in >> A[i] >> D[i]; }
	rep(i, Q) { in >> X[i]; }

	rep(i, N)
	{
		if (D[i] == 1)
		{
			LL p;
			bool eastFlag = true, meetFlag = true;
			for (LL j = i; j < N; ++j)
			{
				if (eastFlag)
				{
					if (D[j] == 2)
					{
						if (A[j] - A[j - 1] <= T * 2) { p = (A[j] + A[j - 1]) / 2; }
						else { meetFlag = false; }
						eastFlag = false;
					}
				}
				else
				{
					if (D[j] == 1)
					{
						if (meetFlag) { list.push_back({ i,j - 1,p }); }
						i = j - 1;
						break;
					}
					if (j == N - 1)
					{
						if (meetFlag) { list.push_back({ i,j,p }); }
						i = j;
						break;
					}
				}
			}
		}
	}
	LL seek = 0;
	rep(i, N)
	{
		if (seek < list.size())
		{
			if (list[seek].begin <= i && i <= list[seek].end)
			{
				if (abs(A[i] - list[seek].pos) <= T) { A[i] = list[seek].pos; }
				else { A[i] += (D[i] == 1 ? T : -T); }
			}
			else if (list[seek].end < i) { ++seek; --i; }
			else { A[i] += (D[i] == 1 ? T : -T); }
		}
		else { A[i] += (D[i] == 1 ? T : -T); }
	}

	rep(i, Q) { out << A[X[i] - 1] << std::endl; }
	return 0;
}

C++: //JOI国のお散歩事情
#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<set>
#include<utility>
#include<map>
#include<queue>
#include<stack>
#include<cmath>
using namespace std;
#define MP make_pair
#define PB push_back
#define F_d first
#define S_d second
#define LARGE 100000000
#define MOD 100000
#define LL long long

LL N, T, Q, A[100009], D[100009];
//D 1:-> 2:<-
int main() {
	cin >> N >> T >> Q;
	for (int i = 0; i < N; i++) cin >> A[i] >> D[i];
	for (int i = 0; i < Q; i++) {
		int X;  cin >> X;  X--;
		LL ans, stop, tmp;
		//stop=止まる場所、tmp=stopに最初に着くDがXと同じ人
		if (D[X] == 1) {// ---->
			for (int i = X; i < N; i++) {
				if (D[i] == 1) tmp = i;
				else {
					stop = (A[tmp] + A[i]) / 2;
					//
					//cout << tmp << " " << i << " " << stop << endl;
					//
					if (stop - A[X] <= T)ans = stop;
					else ans = A[X] + T;
					break;
				}
			}
			if (tmp == N - 1) ans = A[X] + T;
		}

		else {//<----
			for (int i = X; i >= 0; i--) {
				if (D[i] == 2) tmp = i;
				else {
					stop = (A[tmp] + A[i]) / 2;
					//
					//cout << tmp << " " << i << " " << stop << endl;
					//
					if (A[X] - stop <= T)ans = stop;
					else ans = A[X] - T;
					break;
				}
			}
			if (tmp == 0) ans = A[X] - T;
		}
		cout << ans << endl;
	}

	return 0;
}

C++: #include<bits/stdc++.h>
#define rep(i,n)for(ll i=0;i<n;i++)
using namespace std;
typedef long long ll;

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

int main() {
	scanf("%lld%lld%lld", &n, &t, &q);
	rep(i, n)scanf("%lld%d", &a[i], &d[i]);
	rep(i, q)scanf("%lld", &x[i]);
	queue<int>que;
	rep(i, n - 1) {
		if (d[i] == 1 && d[i + 1] == 2 && a[i + 1] - a[i] <= t * 2) {
			d[i] = d[i + 1] = 0;
			a[i] = a[i + 1] = a[i] + (a[i + 1] - a[i]) / 2;
			que.push(i); que.push(i + 1);
		}
	}
	while(!que.empty()) {
		int j = que.front(); que.pop();
		for (int t = -1; t <= 1; t++) {
			int i = j + t;
			if (i < n - 1 && d[i] == 1 && !d[i + 1] && a[i + 1] - a[i] <= t) {
				a[i] = a[i + 1];
				d[i] = 0;
				que.push(i);
			}
			if (0 < i&&d[i] == 2 && !d[i - 1] && a[i] - a[i - 1] <= t) {
				a[i] = a[i - 1];
				d[i] = 0;
				que.push(i);
			}
		}
	}
	rep(j, q) {
		int i = x[j] - 1;
		if (d[i] == 1)printf("%lld\n", a[i] + t);
		else if (d[i])printf("%lld\n", a[i] - t);
		else printf("%lld\n", a[i]);
	}
}

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

ll A[100010],res[1010];int D[100010],imp[1010];
int main(){
	ll N,T,Q;
	cin>>N>>T>>Q;
	for(int i=1;i<=N;i++){
		cin>>A[i]>>D[i];
	}
	
	for(int i=1;i<=Q;i++){
		cin>>imp[i];
	}

	vector<ll> vpl;
	for(int i=1;i<N;i++){
		if(D[i]==1&&D[i+1]==2){
			vpl.push_back((A[i]+A[i+1])/2);
		}
	}

	auto it=vpl.begin();
	for(int i=1;i<=Q;i++){
		while(it!=vpl.end()&&(*it)<A[imp[i]]) it++;
		if(D[imp[i]]==2&&it==vpl.begin()){
			res[i]=A[imp[i]]-T;
			continue;
		}
		else if(D[imp[i]]==2) it--;
		if(it==vpl.end()){
			res[i]=A[imp[i]]+T;
			continue;
		}
		if(D[imp[i]]==1){
			res[i]=min(A[imp[i]]+T,*it);
		}
		else{
			res[i]=max(A[imp[i]]-T,*it);
		}
	}
	for(int i=1;i<=Q;i++) cout<<res[i]<<endl;
	return 0;
}