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>
#include<algorithm>
using namespace std;
class people{
public:
    long long int place;
    int dotti;
};
int main(){
    int n;
    long long int T;
    int Q;
    cin>>n>>T>>Q;
    people *house;
    house=new people[n+2];
    for(int i=1;i<=n;++i){
        cin>>house[i].place>>house[i].dotti;
    }
    long long int dummy;
    house[0].dotti=2;
    house[n+1].dotti=1;
    for(int i=1;i+1<=n;++i){
        if(house[i].dotti==1&&house[i+1].dotti==2){
            if(house[i+1].place-house[i].place<=T*2){
                dummy=(house[i+1].place+house[i].place)/2;
                house[i].place=dummy;
                house[i+1].place=dummy;
                house[i].dotti=3;
                house[i+1].dotti=3;
                for(int j=i-1;j>=1;--j){
                    if(house[j].dotti==1){
                        house[j].place=min(house[j+1].place,house[j].place+T);
                        house[j].dotti=3;
                    }else{
                        break;
                    }
                }
                for(int j=i+2;j<=n;++j){
                    if(house[j].dotti==2){
                        house[j].place=max(house[j-1].place,house[j].place-T);
                        house[j].dotti=3;
                    }
                }
            }else{
                house[i].place+=T;
                house[i+1].place-=T;
                house[i].dotti=3;
                house[i+1].dotti=3;
            }
        }
    }
    for(int i=1;i<=n;++i){
        if(house[i].dotti==2){
            if(house[i-1].dotti==3){
                house[i].place=max(house[i].place-T,house[i-1].place);
                house[i].dotti=3;
            }else{
                house[i].place-=T;
            }
        }else if(house[i].dotti==1){
            if(house[i+1].dotti==3){
                house[i].place=min(house[i].place+T,house[i+1].place);
                house[i].dotti=3;
            }else{
                house[i].place=min(house[i].place+T,house[i+1].place);
            }
        }
    }
    int doll;
    for(int i=0;i<Q;++i){
        cin>>doll;
        cout<<house[doll].place<<endl;
    }
    return 0;
}

C++: #include<bits/stdc++.h>
#define rep(i,n)for(ll i=0;i<n;i++)
using namespace std;
typedef long long ll;

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

int main() {
	scanf("%lld%lld%lld", &n, &t, &q);
	rep(i, n)scanf("%lld%d", &a[i], &d[i]);
	rep(i, q)scanf("%lld", &x[i]);
	rep(i, n - 1) {
		if (d[i] == 1 && d[i + 1] == 2 && a[i + 1] - a[i] <= t * 2) {
			d[i] = d[i + 1] = 0;
			a[i] = a[i + 1] = a[i] + (a[i + 1] - a[i]) / 2;
		}
	}
	rep(i, n) {
		if (i < n - 1 && d[i] == 1 && !d[i + 1] && a[i + 1] - a[i] <= t) {
			a[i] = a[i + 1];
			d[i] = 0;
		}
		if (0 < i&&d[i] == 2 && !d[i - 1] && a[i] - a[i - 1] <= t) {
			a[i] = a[i - 1];
			d[i] = 0;
		}
	}
	rep(j, q) {
		int i = x[j] - 1;
		if (d[i] == 1)printf("%lld\n", a[i] + t);
		else if (d[i])printf("%lld\n", a[i] - t);
		else printf("%lld\n", a[i]);
	}
}

C++: #include<bits/stdc++.h>
using namespace std;
int n,q,k,b[100010];
long long a[100010],t;
vector<long long> v;
int main(){
	cin>>n>>t>>q;
	for(k=0;k<n;k++){
		cin>>a[k]>>b[k];
		if(k)if(b[k-1]==1&&b[k]==2)v.push_back((a[k-1]+a[k])/2);
	}
	while(q--){
		cin>>n;n--;
		k=lower_bound(v.begin(),v.end(),a[n])-v.begin();
		if(b[n]==1){
			if(k==v.size())cout<<a[n]+t;
			else cout<<min(a[n]+t,v[k]);
		}
		else {
			if(!k)cout<<a[n]-t;
			else cout<<max(a[n]-t,v[k-1]);
		}
		cout<<endl;
	}
	return 0;
}

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

using ll = long long;

struct Citizen
{
    ll a;
    ll d;
    ll remain;
    bool isimportant;
    Citizen(ll a, ll d) : a(a), d(d), remain((ll)1e18), isimportant(false){}
};

int n;

void solve(vector<Citizen>& c, int t)
{
    for (int i = 0; i < n - 1; i++) {
        if (c[i].d == 1 and c[i + 1].d == -1){
            c[i].remain = c[i + 1].remain = (c[i + 1].a - c[i].a) / 2;
            int j;
            for (j = i - 1; j >= 0 and c[j].d == c[i].d; --j) {
                c[j].remain = c[j + 1].remain + c[j + 1].a - c[j].a;
            }
            for (j = i + 1; j < n and c[j].d == c[i].d; ++j) {
                c[j].remain = c[j - 1].remain + c[j].a - c[j - 1].a;
            }
            i = j - 1;
        }
    }
}

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

    vector<Citizen> citizens;
    for (int i = 0; i < n; i++) {
        int a, d;
        cin >> a >> d;
        citizens.emplace_back(a, d == 1 ? 1 : -1);
    }
    for (int i = 0; i < q; i++) {
        int x;
        cin >> x;
        citizens[x - 1].isimportant = true;
    }

    solve(citizens, t);
    for (auto&& i : citizens) {
#ifdef DEBUG
        ll current = i.a + i.d * min(i.remain, t);
        printf("%lld %lld %lld %lld\n", current, i.a, i.d, i.remain);
#else
        if (i.isimportant){
            ll current = i.a + i.d * min(i.remain, t);
            cout << current << endl;
        }
#endif
    }

    return 0;
}

C++: #include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <cmath>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#include <stdlib.h>
#include <stdio.h>
#include <bitset>
#include <cstring>
using namespace std;
#define FOR(I,A,B) for(int I = (A); I < (B); ++I)
#define CLR(mat) memset(mat, 0, sizeof(mat))
typedef long long ll;

int main()
{
  ll N, T, Q; cin >> N >> T >> Q;
  ll A[N], D[N]; FOR(i,0,N) cin >> A[i] >> D[i];
  ll X[Q]; FOR(i,0,Q) cin >> X[i];
  vector<ll> stop;
  stop.push_back(-(1LL<<62));
  FOR(i,0,N-1) {
    if(D[i] == 1 && D[i+1] == 2) {
      stop.push_back((A[i]+A[i+1])/2);
    }
  }
  stop.push_back(1LL<<62);
  sort(stop.begin(), stop.end());
  FOR(i,0,Q) {
    ll a = A[X[i] - 1];
    int j = lower_bound(stop.begin(), stop.end(), a) - stop.begin();
    if(D[X[i]-1] == 1) {
      cout << min(stop[j], a + T) << endl;
    } else {
      j--;
      cout << max(stop[j], a - T) << endl;
    }
  }
  return 0;
}