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++: // 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=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("%lld\n",f[i]);
	}
	return 0;
}

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) 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) 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() {
    cin >> N >> T >> Q;

    ll a;
    int d;
    for(int i = 1; i < N + 1; i++) {
        cin >> a >> d;
        H[i] = House(a, d);
        Traced[i] = false;
    }

    int target;
    for(int i = 0; i < Q; i++) {
        cin >> target;
        cout << trace(target) << endl;
    }
}

C++: #include <cstdio>
#include <algorithm>

#define rep(i, n) for(int i = 0; i < (n); ++i)

using namespace std;

typedef long long ll;

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

int main(){
	scanf("%d%lld%d", &n, &t, &q);
	rep(i, n){
		scanf("%lld%d", &a[i], &d[i]);
	}
	rep(i, q){
		scanf("%d", &x[i]);
	}

	int i = 0;
	while(i < n && d[i] == 2){
		a[i++] -= t;
	}
	while(i < n){
		int j = 0;
		while(i + j < n && d[i + j] == 1){
			++j;
		}
		int k = 0;
		while(i + j + k < n && d[i + j + k] == 2){
			++k;
		}
		if(k == 0){
			rep(z, j){
				a[i + z] += t;
			}
		}
		else{
			ll p = (a[i + j - 1] + a[i + j]) / 2;
			rep(z, j){
				a[i + z] = min(a[i + z] + t, p);
			}
			rep(z, k){
				a[i + j + z] = max(a[i + j + z] - t, p);
			}
		}
		i += j + k;
	}

	rep(i, q){
		printf("%lld\n", a[x[i] - 1]);
	}
	return 0;
}

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(i+1<=n){
			if(b[i]==1&&b[i+1]==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++: #include<bits/stdc++.h>
#define PB push_back
#define MP make_pair
#define FI first
#define SE second
using namespace std;
typedef pair<int,int> pii;
static const int inf = 1ll<<60;
static const int MAX_N = 100000;

int N,T,Q;
vector<pii> A;
int flg[MAX_N+5];
int table[MAX_N+5];
int next[MAX_N+5];

signed main(){
    memset(next,inf,sizeof(next));
    cin>>N>>T>>Q;
    for(int i=0;i<N;++i){
        int a,d;
        cin>>a>>d;
        if(d!=1)d=-1;
        A.PB(pii(a,d));
    }
    sort(A.begin(),A.end());
    int maxd=0;
    int t=0;
    for(int i=0;i<N;++i){
        if(flg[i]==1)continue;
        if(A[i].SE!=1||A[i+1].SE!=-1){
            if(A[i].SE==1)table[i]=t;
            else table[i]=t-1;
            continue;
        }
        int D=abs(A[i].FI-A[i+1].FI)/2;
        if(D<=T){
            maxd=max(D,maxd);
            A[i].FI+=A[i].SE*D;
            A[i+1].FI=A[i].FI;
            flg[i]=flg[i+1]=1;
            next[t]=A[i].FI;
            t++;
        }
    }
    for(int i=0;i<N;++i){
        if(flg[i]==1)continue;
        if(A[i].SE==1)A[i].FI+=maxd;
        else A[i].FI-=maxd;
    }
    T-=maxd;
    for(int i=0;i<N;++i){
        if(flg[i]==1)continue;
        if(A[i].SE==1&&A[i+1].SE==-1){
            A[i].FI+=T;
            A[i+1].FI-=T;
            flg[i]=flg[i+1]=1;
            continue;
        }
        if(A[i].SE==1){
            int k=table[i];
            if(next[k]==inf)A[i].FI+=T;
            else{
                int d=abs(A[i].FI-next[k]);
                if(d<=T)A[i].FI=next[k];
                else A[i].FI+=T;
            }
        }
        else{
            int k=table[i];
            if(next[k]==inf)A[i].FI-=T;
            else{
                int d=abs(A[i].FI-next[k]);
                if(d<=T)A[i].FI=next[k];
                else A[i].FI-=T;
            }
        }
    }
    for(int i=0;i<Q;++i){
        int q;
        cin>>q;
        q--;
        cout<<A[q].FI<<endl;
    }
}