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++: #define _USE_MATH_DEFINES
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<set>
#include<vector>


#define fr first
#define sc second
#define mp make_pair
#define pb push_back
#define rep1(i,x) for(int i=1;i<=x;i++)
#define rrep(i,x) for(int i=x-1;i>=0;i--)
#define rrep1(i,x) for(int i=x;i>0;i--)
#define sor(v) sort(v.begin(),v.end())
#define rev(s) reverse(s.begin(),s.end())
#define lb(vec,a) lower_bound(vec.begin(),vec.end(),a)
#define ub(vec,a) upper_bound(vec.begin(),vec.end(),a)
#define uniq(vec) vec.erase(unique(vec.begin(),vec.end()),vec.end())
#define mp1(a,b,c) P1(a,P(b,c))
 


using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
const long long int INF=1001001001001001001;

long long int A[100002];
int D[100002];
vector<long long int> M;
int main(){
	long long int N,T,Q;
	scanf("%lld %lld %lld",&N,&T,&Q);
	
	

	
	rep(i,N){
		scanf("%lld %d",&A[i],&D[i]);
		if(i!=0){
			if(D[i-1]==1&&D[i]==2){
				M.pb( (A[i-1]+A[i])/2 );
			}
		}
	}
	int X[100002];
	rep(i,Q){
		scanf("%d",&X[i]);
		X[i]--;
	}
	rep(i,Q){
		long long int ans=0;
		vector<long long int>::iterator ite;
		if(D[X[i]]==1){
			if(A[X[i]]>M[M.size()-1]){
				printf("%lld\n",A[X[i]]+T);
				continue;
			}
			ite=lower_bound(M.begin(),M.end(),A[X[i]]);
			long long int kyori=*ite-A[X[i]];
			if(kyori>T){
				printf("%lld\n",A[X[i]]+T);
				continue;
			}
		}else{
			if(A[X[i]]<M[0]){
				printf("%lld\n",A[X[i]]-T);
				continue;
			}
			ite=lower_bound(M.begin(),M.end(),A[X[i]])-1;
			long long int kyori=A[X[i]]-*ite;
			if(kyori>T){
				printf("%lld\n",A[X[i]]-T);
				continue;
			}
		}
		printf("%lld\n",*ite);
	}
	return 0;
}

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

typedef long long ll;
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define each(itr,c) for(__typeof(c.begin()) itr=c.begin(); itr!=c.end(); ++itr)
#define all(x) (x).begin(),(x).end()
#define mp make_pair
#define pb push_back
#define fi first
#define se second

typedef pair<ll,int> p;

const ll INF=3003003003003003003LL;

int main()
{
    int n,q;
    ll t;
    cin >>n >>t >>q;

    vector<p> a(n);
    rep(i,n) cin >>a[i].fi >>a[i].se;

    vector<ll> ans(n,INF);

    //?\???????????¶???????
    int st=0;
    while(st<n && a[st].se==2)
    {
        ans[st]=a[st].fi-t;
        ++st;
    }

    //??±??????????¶???????
    int ed=n-1;
    while(ed>=0 && a[ed].se==1)
    {
        ans[ed]=a[ed].fi+t;
        --ed;
    }

    while(st<ed)
    {
        int x=st;
        while(x+1<n && a[x+1].se==1) ++x;
        int y=x;
        while(y+1<n && a[y+1].se==2) ++y;
        //printf(" %d %d %d\n", st,x,y);

        //??????x??¨x+1????????????????????????????????????
        bool meet=(a[x+1].fi-t < a[x].fi+t);
        if(meet)
        {
            ll point=a[x].fi+(a[x+1].fi-a[x].fi)/2;
            for(int i=st; i<=x; ++i) ans[i]=min(point,a[i].fi+t);
            for(int i=x+1; i<=y; ++i) ans[i]=max(point,a[i].fi-t);
        }
        else
        {
            for(int i=x; i<=y; ++i)
            {
                if(a[i].se==1) ans[i]=a[i].fi+t;
                else ans[i]=a[i].fi-t;
            }
        }

        st=y+1;
    }

    //answer
    rep(i,q)
    {
        int x;
        cin >>x;
        cout << ans[x-1] << endl;
    }

    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(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=x;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<=x;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>
#pragma warning(disable : 4996)
using namespace std;
int n, q, c, e, d[111111]; long long t, x[111111], y[111111];
int main() {
	scanf("%d%lld%d", &n, &t, &q);
	for (int i = 0; i < n; i++) scanf("%lld%d", x + i, d + i);
	for (int i = 1; i < n; i++) if (d[i] - d[i - 1] == 1) y[c++] = (x[i - 1] + x[i]) / 2;
	for (int i = 0; i < q; i++) {
		scanf("%d", &e); e--;
		int ptr = lower_bound(y, y + c, x[e]) - y;
		long long ret = x[e] + t * (d[e] * -2 + 3);
		if (d[e] == 1) { if (ptr != c) ret = min(ret, y[ptr]); }
		else if (--ptr != -1) ret = max(ret, y[ptr]);
		printf("%lld\n", ret);
	}
	return 0;
}

C++: #include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
 
typedef long long LL;
typedef pair<LL, LL> P;
 
int main() {
    int a, c;
    LL b;
    cin >> a >> b >> c;
 
    P m[100005];
    P q[1010];
    vector<LL>st;
 
    for (int i = 0;i < a;i++)cin >> m[i].first >> m[i].second;
    
    for (int i = 0;i < c;i++) {
        int d;
        cin >> d;
        q[i] = m[d - 1];
    }
 
    for (int i = 0;i < a - 1;i++) {
        if (m[i].second == 1 && m[i + 1].second == 2)st.push_back((m[i].first + m[i+1].first) / 2);
    }
 
    for (int i = 0;i < c;i++) {
 
        if (q[i].second == 1) {
 
            long long e = q[i].first+b;;
 
            for (int j = 0;j < st.size();j++) {
 
                if (q[i].first < st[j]) {
                    if (q[i].first + b >= st[j])e = st[j];
                }
 
            }
            cout << e << endl;
        }
 
        else {
 
            long long e=q[i].first-b;
 
            int f = st.size();
            for (int j = f - 1;j > -1;j--) {
 
                if (q[i].first > st[j]) {
                    if (q[i].first - b <= st[j])e = st[j];
                }
                 
            }
            cout << e << endl;
        }
    }
   
    return 0;
}