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<bits/stdc++.h>

#define int long long int
#define rep(a,b,c) for(int a=b;a<c;a++)
#define repm(a,b,c) for(int a=(b-1);a>=c;a--)
#define pb push_back
#define str string
#define sf(a) scanfs("%d",&a)
#define pb push_back
#define mp make_pair
#define Fi first
#define Se second
#define ALL(v) (v).begin(), (v).end()

using namespace std;

const int INF = 1e18 + 9;
const int Mod = 1e9 + 7;
inline int replac(str s){double ans=0;rep(i,0,s.length()){ans+=(s[i]-'0')*pow(10,s.length()-i-1);}return (int)ans;}
inline string numstr(int m){str s="";while(m>0){char c;int a=m/10;if(a>0)a=m%(a*10);else a=m;c=(char)('0'+a);s+=c;m/=10;}str st="";for(int i=s.size()-1;i>=0;i--){st+=s[i];}return st;}
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef vector<pii> vii;
int x[100005],y[100005];
int memo[100005];
signed main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n,t,m,a[2],len=2;
    cin >> n >> t >> m;
    rep(i,0,n){
        cin >> x[i] >> y[i];
    }
    rep(i,0,100005){memo[i]=0;}
    rep(j,0,n){
        if(y[j]==2){
            memo[j]=x[j]-t;
        }
        if(y[j]==1){
            memo[j]=x[j]+t;
            for(;j<n;j++){
                memo[j]=x[j]+t;
                if(y[j]==2){
                int wa;
                int butu=(x[j-1]+x[j])/2;
                //cout << butu <<" " << j << "ss";
                //1?????????
                for(int p=j-1;p>=0;p--){
                    if(y[p]==2)break;
                    if((x[p]+t)>=butu){
                        memo[p]=butu;
                    }else{
                        break;
                    }
                }
                int mae = j-1;
                for(;j<n;j++){
                if(y[j]==1){j--;break;}
                if((x[j]-t)<=butu){
                  //  cout << j << endl;
                    memo[j]=butu;
                }else{
                    memo[j]=x[j]-t;
                }
                }
                }
            }
        }
    }
    rep(i,0,m){
        int u;cin >> u;
        cout << memo[u-1] << endl;
    }
    return 0;
}

C++: #define _CRT_SECURE_NO_WARNINGS
#include "bits/stdc++.h"
using namespace std;

#define int int64_t

#define CHOOSE(a) CHOOSE2 a
#define CHOOSE2(a0,a1,a2,a3,x,...) x
#define REP1(i, s, cond, cal) for (signed i = signed(s); i cond; i cal)
#define REP2(i, s, n) REP1(i, s, < signed(n), ++)
#define REP3(i, n) REP2(i, 0, n)
#define rep(...) CHOOSE((__VA_ARGS__,REP1,REP2,REP3))(__VA_ARGS__)
#define rrep(i, s) rep(i, s, >= 0, --)

#define all(c) begin(c), end(c)
#define maxup(ans, x) (ans = (ans < x ? x : ans))
#define minup(ans, x) (ans = (ans > x ? x : ans))

#define breakif(cond) if(cond) break; else

using VV = vector<vector<int>>;
using V = vector<int>;
using P = pair<int, int>;
using IP = pair<int, P>;

template<typename T>
inline void input(vector<T>& v) { for (auto& x : v) cin >> x; }

const int INF = 1ll << 60;

signed main() {
	int n, t, q;
	cin >> n >> t >> q;
	vector<int> stops;
	vector<P> as(n);

	P pre;
	cin >> pre.first >> pre.second;
	as[0] = pre;
	rep(i, n - 1) {
		int a, d;
		cin >> a >> d;
		as[i + 1] = P(a, d);
		if (pre.second != d) {
			int pa = pre.first;
			stops.push_back((a + pa) / 2);
		}
		pre = P(a, d);
	}

	rep(i, q) {
		int x;
		cin >> x;
		x--;
		int a = as[x].first, d = as[x].second;
		int time;
		if (d == 1) {
			if (stops.size() && stops.back() < a) {
				time = INF;
			} else {
				time = *lower_bound(all(stops), a) - a;
			}
		} else {
			if (stops.size() && stops[0] > a) {
				time = INF;
			} else {
				time = a - *(--upper_bound(all(stops), a));
			}
		}

		if (time <= t) {
			cout << a + time * (d == 1 ? 1 : -1) << endl;
		} else {
			cout << a + t * (d == 1 ? 1 : -1) << endl;
		}
	}

	//system("pause");
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#define INF 100000000000
using namespace std;
int main() {
	long long n, t, q;
	cin >> n >> t >> q;
	vector<long long>a(n);
	vector<int>d(n);
	vector<long long>s(n);
	for (int i = 0; i < n; i++) {
		cin >> a[i] >> d[i];
	}
	for (int i = 0; i < n; i++) s[i] = INF;
	long long ha = 0;
	for (int i = 0; i < n-1; i++) {
		if (d[i] == 1 && d[i + 1] == 2) {
			ha = (a[i] + a[i + 1]) / 2;
			for (int j = i; j > -1; j--) {
				if (d[j] == 2) break;
				if (s[j] == INF) s[j] = ha;
			}
			for (int j = i + 1; j < n; j++) {
				if (d[j] == 1) break;
				if (s[j] == INF) s[j] = ha;
			}
		}
	}
	for (int i = 0; i < q; i++) {
		int x;
		cin >> x;
		x--;
		if (s[x] == INF) {
			if (d[x] == 1) cout << a[x] + t << endl;
			else cout << a[x] - t << endl;
		}
		else {
			if (abs(a[x] - s[x]) < t) {
				cout << s[x] << endl;
			}
			else {
				if (d[x] == 1) cout << a[x] + t << endl;
				else cout << a[x] - t << endl;
			}
		}
	}
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define Rep(i,N) for(int i = 0; i < N; i++)
#define INF 1 << 28
#define S second
#define F first
#define pb push_back
typedef pair<int,int> Pi;
#define int long long

inline void chmax(int &a, int b) { a = max(a,b); }
inline void chmin(int &a, int b) { a = min(a,b); }

int N, T, M;
int A[100005], B[100005];
int point[100005];

void calc(int q)
{
  stack<int>st;
  int g, p;
  if(!q) g = 1, p = 0;
  else g = -1, p = N - 1;
  for(int i = p; i < N && i >= 0; i += g) {
    if(q == B[i]) {
      st.push(i);
    } else {
      int t;
      if(!st.empty())t = st.top();
      while(!st.empty()) {
	int k = st.top(); st.pop();
	point[k] = A[i] + (A[t] - A[i]) / 2;
	if(!q)chmin(point[k],A[k] + T);
	else chmax(point[k],A[k] - T);
      }
    }
  }
  while(!st.empty()) {
    int k = st.top(); st.pop();
    point[k] = A[k] + T * g;
  }
} 

signed main()
{
  
  cin >> N >> T >> M;
  Rep(i,N) {
    cin >> A[i] >> B[i];--B[i];
  }
  Rep(i,2)calc(i);
  
  int Q;
  while(M--) {
    cin >> Q;--Q;
    cout << point[Q] << endl;
  }
  return 0;
}