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++: //Walking in JOI Kingdom
#include <iostream>

using namespace std;
typedef long long ll;

static const int MAX = 100;

enum DIRECTION {
    EAST = 1,
    WEST = 2
};

struct House {
    ll a;
    int d;
    House() {}
    House(ll a, int d): a(a), d(d) {}
};

int N, Q;
ll T;
House H[MAX + 1];
ll A[MAX + 1];
bool Traced[MAX + 1];

ll trace(int target) {
    if(Traced[target]) return A[target];
    ll a;
    if(H[target].d == EAST) {
        if(target == N) a = H[target].a + T;
        else {
            int neighbor = target + 1;
            if(H[neighbor].d == EAST) {
                if(H[neighbor].a - H[target].a >= T) {
                    a = H[target].a + T;
                } else {
                    a = min(H[target].a + T, trace(neighbor));
                }
            }
            else a = min(H[target].a + T, (H[target].a + H[neighbor].a) / 2);
        }
    } else if(H[target].d == WEST) {
        if(target == 1) a = H[target].a - T;
        else {
            int neighbor = target - 1;
            if(H[neighbor].d == WEST) {
                if(H[target].a - H[neighbor].a >= T) {
                    a = H[target].a - T;
                } else {
                    a = max(H[target].a - T, trace(neighbor));
                }
            }
            else a = max(H[target].a - T, (H[target].a + H[neighbor].a) / 2);
        }
    }
    Traced[target] = true;
    A[target] = a;
    return a;
}

int main() {
    scanf("%d %lld %d", &N, &T, &Q);

    ll a;
    int d;
    for(int i = 1; i < N + 1; i++) {
        scanf("%lld %d", &a, &d);
        H[i] = House(a, d);
        Traced[i] = false;
    }

    int target;
    for(int i = 0; i < Q; i++) {
        scanf("%d", &target);
        cout << trace(target) << endl;
    }
}

C++: #include <stdio.h>
int main(){
  long long a[50000],T,ans[1001];
  int j,k,d[50000],x[50000],Q,i,N,stop,s;
  //in
  scanf("%d %lld %d",&N,&T,&Q);  
  for(i=0;i<N;i++){
  scanf("%lld %d",&a[i],&d[i]);
  }
  for(i=0;i<Q;i++){
    scanf("%d",&x[i]);
    x[i]--;
  }
  //in end
  for(i=0;i<Q;i++){
    s=0;
    if(d[x[i]]==1){
      for(j=x[i];j<N;j++){
	if(d[j]==2){
	  stop=(a[j]+a[j-1])/2;
	  //printf("stop %d\n",stop);
	  if(a[x[i]]+T>(long long)stop){
	  ans[i]=stop;
	  s=1;
	  break;
	  }
	}
      }
      if(s==0)ans[i]=a[x[i]]+T;
    }

    if(d[x[i]]==2){
      for(j=x[i];j>=0;j--){
	if(d[j]==1){
	  stop=(a[j]+a[j+1])/2;
	  //printf("st %d\n",stop);//
	  if(a[x[i]]-T<(long long)stop){
	  ans[i]=stop;
	  s=1;
	  break;
	  }
	}
      }
      if(s==0)ans[i]=a[x[i]]-T;
    }
  }

  for(i=0;i<Q;i++){
    printf("%lld\n",ans[i]);
  }
  return 0;
}

C++: #include <iostream>
#include <queue>
#include <set>
using namespace std;

struct person{
    long long start;
    long long end;
    long long way;
    bool finished;
};
person person[100005];
long long n,t,q,key[1005];
int main(){
    //cout<<endl;
    cin>>n>>t>>q;
    priority_queue<long long> crash;
    //crash.push(-100000000000000000000000);
    for(int i=1; i<=n; i++) {cin>>person[i].start>>person[i].way; person[i].finished=false;}
    for(int i=1; i<=n; i++){
        if(i==1&&person[i].way==2) {person[i].end=person[i].start-t; person[i].finished=true; continue;}
        if(person[i].way==2){
            if(person[i-1].way==1){
                if((person[i].start-person[i-1].start)/2>t) {
                    person[i].end=person[i].start-t;
                    person[i-1].end=person[i-1].start+t;
                }else{
                    person[i].end=(person[i].start+person[i-1].start)/2;
                    person[i-1].end=(person[i].start+person[i-1].start)/2;
                    crash.pop();
                    crash.push((person[i].start+person[i-1].start)/2);
                    for(int j=1; j<i-1; j++){
                        if(person[j].finished!=true&&person[j].way==1) {
                            if(person[j].start+t>=crash.top()){person[j].end=crash.top(); person[j].finished=true;}
                            else person[j].end=person[j].start+t; person[j].finished=true;
                        }
                    }
                }
                person[i].finished=true;
                person[i-1].finished=true;
                continue;
            }else if(person[i].start-t<=crash.top()) {person[i].end=crash.top(); person[i].finished=true; continue;
            }else {
                person[i].end=person[i].start-t;
                person[i].finished=true;
            }}
    }
    for(int i=1; i<=n; i++){
        if(person[i].finished!=true){
            if(person[i].way==1) {person[i].end=person[i].start+t; person[i].finished=true;}
            else {person[i].end=person[i].start-t; person[i].finished=true;}
        }
    }
    //for(int i=1; i<=n; i++) cout<<"person["<<i<<"].end=="<<person[i].end<<endl;
    for(int i=1; i<=q; i++) {
        cin>>key[i];
        cout<<person[key[i]].end<<endl;
    }
}

C++: #include<stdio.h>
int main(void)
{
	int n,t,q,a[100001],d[100001],flgd[100001],Q[1001],x,i,s[1001],s1[1001],cnt;
	scanf("%d%d%d",&n,&t,&q);
	for(i=0;i<n;i++){
		scanf("%d%d",&a[i],&flgd[i]);
		d[i]=flgd[i];
	}
	for(i=0;i<q;i++) scanf("%d",&Q[i]);
	cnt=0;
	for(i=0;i<n;i++){
	//	printf("%d=%d %d\n",i,flgd[i],flgd[i+1]);
		if(flgd[i]==1&&flgd[i+1]==2){
			flgd[i+1]=0;
			a[i]+=t; a[i+1]-=t;
			if(a[i]>=a[i+1]){
				x=0;
				a[i]-=t; a[i+1]+=t;
				x=(a[i+1]-a[i])/2;
				a[i]+=x; a[i+1]-=x;
				s[cnt]=a[i]; s1[cnt]=i;
				cnt++;
			}
		}
		else if(flgd[i]==0&&flgd[i+1]==2){
			flgd[i+1]=0; a[i+1]-=t;
			if(a[i]>=a[i+1]){
				a[i+1]=a[i];
			}
		}
		else{
			if(flgd[i]==1) a[i]+=t;
			if(flgd[i]==2) a[i]-=t;
			flgd[i]=0;
		}
	}
	x=cnt; cnt=0;
	for(i=0;i<n;i++){
		if(i>s1[cnt]&&cnt!=x) cnt++;
		if(d[i]==1&&cnt!=x){
			if(a[i]>s[cnt]) a[i]=s[cnt];
		}
		else if(d[i]==2&&cnt!=0){
			if(a[i]<s[cnt-1]) a[i]=s[cnt-1];
		}
		//printf("cnt=%d\n",cnt);
	}
	for(i=0;i<n;i++){
		printf("%d\n",a[i]);
	}
	/*for(i=0;i<q;i++){
		printf("%d\n",a[Q[i]-1]);
	}*/
	return 0;
}