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>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<=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++: // aaaaaaaaaaaaa
#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("%lld %lld %lld",&n,&t,&q);
	for(i=1;i<=n;i++){
		scanf("%lld %lld",&a[i],&d[i]);
	}
	for(i=1;i<=q;i++){
		scanf("%lld",&x[i]);
	}
	k=0;
	for(i=1;i<n;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;
//				printf("%lld %lld\n",f[j],s[i]);
				break;
			}
			if(d[x[j]]==2&&s[i]<=a[x[j]]&&s[i]+t>=a[x[j]]){
				f[j]=s[i];
				flg=1;
//				printf("%lld %lld\n",f[j],s[i]);
				break;
			}
		}
		if(flg==0){
			if(d[x[j]]==1) f[j]=a[x[j]]+t;
			else f[j]=a[x[j]]-t;
//			printf("%lld\n",f[j]);
		}
	}
	for(i=1;i<=q;i++){
		printf("%lld\n",f[i]);
	}
	return 0;
}

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][1005] = {0};
int main() {
	long long int n, t;
	int q;
	scanf_s("%lld %lld %d",&n,&t,&q);
	
	for (int i = 0; i < n; i++) {
		scanf_s("%lld %lld", &walk[0][i],&home[i]);
	}
	for (int i = 0; i <q; i++) {
		scanf_s("%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 <iostream>
#include <algorithm>
#include <climits>
#include <string>
#include <vector>
#include <cmath>
using namespace std;

int N, Q;
long long T;
long long A[100010];
bool dir[100010];
long long st[100010];

long long query(int i) {
  if (st[i] != -1) return st[i];
  long long ans;
  // ->
  if (dir[i]) {
    if (i == N-1)       ans = 0;
    else if (!dir[i+1]) ans = (A[i+1] - A[i]) / 2;
    else {
      long long t = query(i+1);
      int t = query(i+1);
      if (t == 0) ans = 0;
      else        ans = (A[i+1] - A[i]) + t;
    }
  }
  // <-
  else {
    if (i == 0)        ans = 0;
    else if (dir[i-1]) ans = (A[i] - A[i-1]) / 2;
    else {
      long long t = query(i-1);
      if (t == 0) ans = 0;
      else        ans = (A[i] - A[i-1]) + t;
    }
  }
  return st[i] = ans;
}

int main() {
  cin >> N >> T >> Q;
  for (int i=0; i<N; i++) {
    int d;
    cin >> A[i] >> d;
    if (d == 1) dir[i] = true;
    st[i] = -1;
  }

  for (int i=0; i<Q; i++) {
    int x;
    cin >> x;
    long long t = query(x-1);
    cout << A[x-1] + (t == 0 ? T : min(t, T))*(dir[x-1]?1L:-1L) << "\n";
  }
  return 0;
}

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

typedef long long lint;

int N, Q, D[100005];
lint T, A[100005];

void solve(int x) {
	int i = x + D[x];
	while (0 <= i && i < N && D[i] == D[x]) i += D[x];
	lint ans = A[x] + T * D[x];
	if ((D[x] == 1 && i < N) || (D[x] == -1 && 0 <= i)) {
		int li = (D[x] == -1 ? i : i-1), ri = (D[x] == -1 ? i+1 : i);
		lint L = A[li] + T, R = A[ri] - T;
		if (L > R) {
			lint C = A[li] + (A[ri] - A[li]) / 2;
			ans = (D[x] == -1 ? max(ans, C) : min(ans, C));
		}
	}
	cout << ans << endl;
}

int main() {
	cin >> N >> T >> Q;
	for (int i=0; i<N; ++i) {
		cin >> A[i] >> D[i];
		if (D[i] == 2) D[i] = -1;
	}
	
	for (int i=0; i<Q; ++i) {
		int x;
		cin >> x;
		--x;
		solve(x);
	}
}

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>=1;i--){
		if(flg[i]==1){
			tt=s[i];	x=1;
		}
		else{
			if(b[i]==1&&x==1&&a[i]+t>tt){
				s[i]=tt;	flg[i]=2;
			}
		}
	}
	x=0;
	for(i=1;i<=n;i++){
		if(flg[i]==1){
			tt=s[i];	x=1;
		}
		else{
			if(b[i]==2&&x==1&&a[i]-t<tt){
				s[i]=tt;	flg[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]]);
}