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 <iostream>
using namespace std;
int main(void){
   
  long long int n,t,q;
  cin>>n>>t>>q;
  long long int p[n],h[n],s[n],ss[n];
  for(int i=0;i<n;i++){
      cin>>p[i]>>h[i];
      ss[i]=0;
  }
  long long int hs=-1,hc=-1,sc,ssc=0;
  for(int i=0;i<n;i++){
      if(h[i]==1){
          hc=i;
          if(hs==-1){
              hs=i;
          }
      }else{
          if(hc==-1){
              if(ssc==1){
                  s[i]=sc;
                  ss[i]=1;
              }
              
          }else{
              sc=(p[hc]+p[i])/2;
              ssc=1;
              s[i]=sc;
              ss[i]=1;
              s[hc]=sc;
              ss[hc]=1;
              for(int k=hs;k<hc;k++){
                  s[k]=sc;
                  ss[k]=1;
              }
              hs=-1;
              hc=-1;
          }
      }
  }
  for(int i=0;i<q;i++){
      int x;
      cin>>x;
      x--;
      if(ss[x]==0){
          if(h[x]==1){
              cout<<p[x]+t<<endl;
          }else{
              cout<<p[x]-t<<endl;
          }
      }else{
         if(h[x]==1){
             if(s[x]-p[x]>t){
                 cout<<p[x]+t<<endl;
             }else{
                 cout<<s[x]<<endl;
             }
         }else{
             if(p[x]-s[x]>t){
                 cout<<p[x]-t<<endl;
             }else{
                 cout<<s[x]<<endl;
             }
         }
      }
  }
    
}

C++: #include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <cstring>
#include <cctype>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <map>
#include <list>
#include <stdio.h>
#include <string.h>
#include <cstdlib>
#include <math.h>
#include <bitset>
#include <iterator>
#include <iomanip>
#include <sstream>
#include <numeric>
#include <cassert>
#define INF 1000000000000000000
#define MOD 1000000007
#define sym cout<<"---------"<<endl;
#define ll long long
#define mk make_pair
#define en endl
#define RE return 0
#define int ll
#define P pair<int,int>
using namespace std;
int dx[5]={1,0,-1,0,0},dy[5]={0,1,0,-1,0};
int gcd(int a,int b){if(a%b==0){return b;}else return gcd(b,a%b);}
int lcm(int a,int b){if(a==0){return b;} return a/gcd(a,b)*b;}

#define MAX 100005
int n,t,q;
struct people{
	int where,dir;
};
people peo[MAX];
bool done[MAX];
int d[MAX];

signed main(){
	cin>>n>>t>>q;
	
	for(int i=1; i<=n; i++){
		int a,b; cin>>a>>b;
		peo[i].where=a; peo[i].dir=b;
		done[i]=false;
	}
	vector<int> col;
	for(int i=1; i<=n-1; i++){
		if(peo[i].dir==1){
			if(peo[i+1].dir==2&&2*t>peo[i+1].where-peo[i].where){
				int c=(peo[i+1].where+peo[i].where)/2;
				col.push_back(c);
				done[i+1]=done[i]=true;
				d[i+1]=d[i]=c;
			}
		}
	}
	sort(col.begin(), col.end());
	for(int i=0; i<q; i++){
		int a; cin>>a;
		if(done[a]){
			cout<<d[a]<<en;
			continue;
		}
		bool ok=false;
		P ans; ans.first=2*INF+1;
		for(int j=0; j<col.size(); j++){
			int w=col[j];
			if(w-peo[a].where>0&&peo[a].dir==1){
				if(w-peo[a].where<t){
					if(ans.first>w-peo[a].where){ 
						ans.second=w;
						ans.first=abs(w-peo[a].where);
					}
					ok=true;
				}
			}else if(w-peo[a].where<0&&peo[a].dir==2){
				if(peo[a].where-w<t){
					if(ans.first>peo[a].where-w){ 
						ans.second=w;
						ans.first=abs(peo[a].where-w);
					}
					ok=true;
				}
			}
		}
		if(!ok) cout<<(peo[a].dir==1 ? peo[a].where+t:peo[a].where-t)<<en;
		else cout<<ans.second<<en;
	}
}

C++: #include <bits/stdc++.h>
#define f first
#define s second
using namespace std;
typedef long long ll;
typedef pair<ll,ll> P;
P p[100010];
vector <ll>x;


int search(ll X){
  int L=0,M,R=x.size();
  while(L<R){
    M=(L+R)/2;
    if(X<=x[M]) R=M;
    else L=M+1;
  }
  return L;
}

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

  for(int i=0,idx=-1,dir;i<n;i++){
    ll a;
    cin>>a>>dir;
    dir--;
    if(dir==0) idx=i;
    p[i]=P(a,dir);
    if(i&&idx!=-1&&dir==1){
      ll dis=(p[i].f+p[idx].f)/2;
      if(abs(p[i].f-dis)<=T)x.push_back(dis);
      idx=-1;
    }
  }

  //  for(int i=0;i<x.size();i++)cout <<x[i]<<" ";cout <<endl;

  while(q--){
    int Q;
    cin>>Q;
    Q--;
    ll a=p[Q].f;
    int dir=p[Q].s;
    int idx=search(a)-(dir? 1:0);
    ll ans=(dir? -1:1)*T+a;
    if(!((dir==1&&idx==-1)||(dir==0&&idx==(int)x.size()))&&abs(x[idx]-a)<=T)ans=x[idx];
    cout <<ans<<endl;
  }
  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define REP(asd, fgh) for(ll asd = 0; asd < fgh; asd++)
 
struct P{
  ll pos;
  ll dir;
  bool stopped;
};
 
 
int main(void){
  ios::sync_with_stdio(false);
  cin.tie(0);
 
  ll N, T, Q; cin >> N >> T >> Q;
  vector<P> p(N);
 
  REP(i, N){
    ll a, d;
    cin >> a >> d;
    p[i].pos = a;
    if(d == 1){
      p[i].dir = 1;
    }else{
      p[i].dir = -1;
    }
    p[i].stopped = false;
  }
 
  for(ll i = 0; i < N-1; i++){
    if(p[i].dir == 1 && p[i+1].dir == -1){
      if((p[i+1].pos - p[i].pos) / 2 <= T){
        p[i+1].pos = p[i].pos = (p[i+1].pos + p[i].pos)/2;
      }else{
        p[i+1].pos -= T;
        p[i].pos += T;
      }
      p[i+1].stopped = p[i].stopped = true;
    }
  }
 
  for(ll i = 0; i < N; i++){
    if(p[i].stopped) continue;
 
    if(p[i].dir == 1){
      bool flg = false;
      for(ll j = i+1; j < N; j++){
        if(p[j].pos - p[i].pos > T){
          p[i].pos += T;
          flg = true;
          break;
        }else if(p[j].stopped == true){
          p[i].pos = p[j].pos;
          flg = true;
          break;
        }
      }
      if(!flg){
        p[i].pos += T;
      }
 
    } else {
      bool flg = false;
      for(ll j = i-1; j >= 0; j--){
        if(p[i].pos - p[j].pos > T){
          p[i].pos -= T;
          flg = true;
          break;
        }else if(p[j].stopped == true){
          p[i].pos = p[j].pos;
          flg = true;
          break;
        }
      }
      if(!flg){
        p[i].pos -= T;
      }
    }
  }
 
  REP(i, Q){
    int q; cin >> q; q--;
    cout << p[q].pos << endl;
  }
 
  return 0;
}