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=n;i>0;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<=n;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<stdio.h>
#include<stdlib.h>
#include<algorithm>
using namespace std;
long long n,t,q,a[100001],d[100001],x[1001],s[100001],k,f[1001],flg,i,j;
int main(void)
{
	scanf("%d %d %d",&n,&t,&q);
	for(i=1;i<=n;i++){
		scanf("%d %d",&a[i],&d[i]);
	}
	for(i=1;i<=q;i++){
		scanf("%d",&x[i]);
	}
	k=0;
	for(i=0;i<n-1;i++){
		if(d[i]==1&&d[i+1]==2&&a[i]+t*2>=a[i+1]){
			s[k]=a[i]-(a[i]-a[i+1])/2;
			k++;
		}
	}
	for(j=1;j<=q;j++){
		flg=0;
		for(i=0;i<k;i++){
			if(d[x[j]]==1&&s[i]>=a[x[j]]&&s[i]-t<=a[x[j]]){
				f[j]=s[i];
				flg=1;
				break;
			}
			else if(d[x[j]]==2&&s[i]<=a[x[j]]&&s[i]+t>=a[x[j]]){
				f[j]=s[i];
				flg=1;
				break;
			}
		}
		if(flg==0){
			if(d[x[j]]==1) f[j]=a[x[j]]+t;
			else f[j]=a[x[q]]-t;
		}
	}
	for(i=1;i<=q;i++){
		printf("%d\n",f[i]);
	}
	return 0;
}

C++: //============================================================================
// Name        : JOI.cpp
// Author      :
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
// 1. dp?????????
ll dp[100001];
// 2. n?????????
ll N,T,Q;
ll A[10001];
int D[10001];

// 7. calc?????????
ll calc(int k){
	// 8. dp???????????°??????
	if(dp[k]!=A[k])return dp[k];
	// 9. dp????¨????

	if(D[k]==1){
		if(D[k+1]==1){
			dp[k]=min(A[k]+T,calc(k+1));
		}else{
			dp[k]=min((A[k]+A[k+1])/2,A[k]+T);
		}
	}
	if(D[k]==2){
		if(D[k-1]==2){
			dp[k]=max(A[k]-T,calc(k-1));
		}else{
			dp[k]=max((A[k]+A[k-1])/2,A[k]-T);
		}
	}

	return dp[k];
}
int main() {
	cin>>N>>T>>Q;

	//3.?????????

	//4.??\???
	for(int i=1;i<N+1;i++){
		cin>>A[i]>>D[i];
		dp[i]=A[i];
	}

	//5.???????????¨???

	if(D[1]==2){
		dp[1]-=T;
	}

	if(D[N]==1){
		dp[N]+=T;
	}


	//6.??????
	int X;
	for(int i=1;i<Q+1;i++){
		cin>>X;
		cout<<calc(X)<<endl;
	}

	return 0;
}

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;}
	}
	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 <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; j >= 0; --j) {
				if (D[j] == 1) {
					t[j] = t_mem;
				}
				else { break; }
			}
			for (long long int j = i; 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<cstdio>
#include<algorithm>
#include<functional>
#include<cstring>
#include<iostream>
using namespace std;
int main(void)
{
	int N,T,Q,house[100001],muki[100001];
	int X[1001];
	int list[100001];
	
	cin>>N>>T>>Q;
	for(int i=1;i<=N;i++) cin>>house[i]>>muki[i];
	for(int i=1;i<=Q;i++) cin>>X[i];
	
	for(int i=1;i<=N;i++) {
		if(muki[i]==1) continue;
		if(i==1) list[i]=house[i]-T;
		else if(muki[i-1]==1) list[i]=max(house[i]-T,(house[i]+house[i-1])/2);
		else list[i]=max(house[i]-T,list[i-1]);
	}
	
	for(int i=N;i>=1;i--) {
		if(muki[i]==2) continue;
		if(i==N) list[i]=house[i]+T;
		else if(muki[i+1]==2) list[i]=min(house[i]+T,(house[i]+house[i+1])/2);
		else list[i]=min(house[i]+T,list[i+1]);
	}
	
	for(int i=1;i<=Q;i++) {
		cout<<list[X[i]]<<endl;
	}
	
	return 0;
}