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>
#include <vector>
using namespace std;

class People
{
public:
	People(){}
	People(long long int n, long long int pos, bool stopflag) : n(n), pos(pos), stopflag(stopflag){}
	long long int n, pos;
	bool stopflag;
};
bool operator<(const People& p1, const People& p2)
{
	return p1.pos < p2.pos;
}

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

	vector<People> left;
	vector<People> right;
	for (int i = 1; i <= n; i++)
	{
		long long int a, d;
		cin >> a >> d;
		if (d == 1) right.push_back(People(i, a, false));
		if (d == 2) left.push_back(People(i, a, false));
	}
	for (int i = right.size() - 1; i >= 0; i--)
	{
		auto itr = lower_bound(begin(left), end(left), right[i]);
		if (itr == end(left)) continue;
		if (itr->stopflag)
		{
			if (right[i].pos + t >= itr->pos)
			{
				right[i].stopflag = true;
				right[i].pos = itr->pos;
			}
		}
		else
		{
			if (right[i].pos + t >= itr->pos - t)
			{
				right[i].stopflag = true;
				itr->stopflag = true;
				long long int p = (right[i].pos + itr->pos) / 2;
				right[i].pos = p;
				itr->pos = p;
			}
		}
	}
	for (int i = 0; i < left.size(); i++)
	{
		if (left[i].stopflag) continue;
		auto itr = lower_bound(begin(right), end(right), left[i]);
		if (itr == begin(right)) continue;
		itr--;
		if (itr->stopflag)
		{
			if (left[i].pos - t <= itr->pos)
			{
				left[i].stopflag = true;
				left[i].pos = itr->pos;
			}
		}
		else
		{
			if (left[i].pos - t <= itr->pos + t)
			{
				left[i].stopflag = true;
				itr->stopflag = true;
				long long int p = (left[i].pos + itr->pos) / 2;
				left[i].pos = p;
				itr->pos = p;
			}
		}
	}
	for (int i = 0; i < left.size(); i++)
	{
		if (!left[i].stopflag) left[i].pos -= t;
	}
	for (int i = 0; i < right.size(); i++)
	{
		if (!right[i].stopflag) right[i].pos += t;
	}

	for (int i = 0; i < q; i++)
	{
		int a;
		cin >> a;
		for (int j = 0; j < left.size(); j++)
		{
			if (left[j].n == a)
			{
				cout << left[j].pos << endl;
				break;
			}
		}
		for (int j = 0; j < right.size(); j++)
		{
			if (right[j].n == a)
			{
				cout << right[j].pos << endl;
				break;
			}
		}
	}

	return 0;
}

C++: #include <bits/stdc++.h>
#define range(i, a, b) for(int i = (a); i < (b); i++)
#define rep(i, a) range(i, 0, a)
using namespace std;

int main() {
    long long n, t, q;
    cin >> n >> t >> q;
    vector <vector <long long>> ad(4);
    vector <long long> a(n), d(n);
    rep (i, n) {
        cin >> a[i] >> d[i], d[i]--;
        ad[d[i]    ].push_back( a[i]);
        ad[d[i] + 2].push_back(-a[i]);
    }
    sort(ad[2].begin(), ad[2].end());
    sort(ad[3].begin(), ad[3].end());

    rep (i, q) {
        int in;
        cin >> in, in--;
        auto itrA = d[in] == 0 ? lower_bound(ad[1].begin(),  ad[1].end(),  a[in])
                               : lower_bound(ad[2].begin(),  ad[2].end(), -a[in]);

        auto itrB = d[in] == 0 ? upper_bound(ad[2].begin(),  ad[2].end(), a[in])
                               : upper_bound(ad[3].begin(),  ad[3].end(), -a[in]);


        if (itrA == ad[d[in] + 1].end()) {
            cout << a[in] + t << endl;
        }
        else if (d[in]) {
            if (itrB == ad[d[in]+2].end()) {
                cout << max(a[in] - t, (a[in] - *itrA) / 2) << endl;
            }
            else {
                if (-*itrA > -*itrB) cout << max((a[in] + -*itrA) / 2, a[in] - t) << endl;
                else cout << max((-*itrB - *itrA) / 2, a[in] - t) << endl;
            }
        }
        else {
            if (itrB == ad[d[in]+2].end()) {
                cout << a[in] + min(t, (*itrA - a[in]) / 2) << endl;
            }
            else {
                if (*itrA < -*itrB) cout << min(a[in] + t, (*itrA + a[in]) / 2) << endl;
                else cout << min((*itrA + max(a[in], -*itrB)) / 2, a[in] + t) << endl;
            }
        }
    }
    return 0;
}

C++: #include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

typedef long long LL;
typedef pair<int, int> P;

int main() {
	int a, c;
	LL b;
	cin >> a >> b >> c;

	P m[100005];
	P q[1010];
	vector<int>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 = 3e13;

			for (int j = 0;j < st.size();j++) {

				if (q[i].first < st[j]) {
					if (q[i].first + b >= st[j])e = st[j];
					else e = q[i].first + b;
				}

			}
			if (e == 3e13)e = q[i].first + b;
			cout << e << endl;
		}

		else {

			long long e=3e13;

			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];
					else e = q[i].first - b;
				}
				
			}
			if (e == 3e13)e = q[i].first - b;
			cout << e << endl;
		}
	}
	getchar();
	getchar();
	return 0;
}

C++: #include <cstdio>
#include <iostream>
#include <algorithm>
#include <vector>
#define INF 3000000000000000000
#define REP(i,n) for (int i=0;i<(n);i++)
#define RREP(i,n) for (int i=(n-1);i>=0;i--)
using namespace std;


 long long int x[200000][3]={0};//0:x 1:direction
 long long int p[2000];
 long long int ans[2000];

 long long int n,t,q;

int main(){
	scanf("%d%d%d",&n,&t,&q);
	REP(i,n)scanf("%d %d",&x[i][0],&x[i][1]);
	REP(i,q)scanf("%d",&p[i]);
	//search opposite people//calc
	bool flag=false;
	REP(i,n-1){
		if(x[i][1]==1&&x[i+1][1]==2){x[i+1][2]=(x[i+1][0]+x[i][0])/2;x[i][2]=x[i+1][2];}
		if(x[i][1]==2&&x[i+1][1]==2){x[i+1][2]=x[i][2];}
	}
	if(x[n-1][1]==1)x[n-1][2]=INF;
	RREP(i,n){
		if(x[i][1]==1&&x[i-1][1]==1){x[i-1][2]=x[i][2];}
	}
	if(x[0][1]==2)x[0][2]=INF;
	 long long int c;
	//judge
	REP(i,q){
	c=x[p[i]-1][0]-t*(x[p[i]-1][1]*2-3);
	if((x[p[i]-1][1]==1&&c>x[p[i]-1][2])||(x[p[i]-1][1]==2&&c<x[p[i]-1][2]))printf("%d\n",x[p[i]-1][2]);
	else printf("%d\n",c);
	}
	//REP(i,n){REP(j,3)printf("%d ",x[i][j]);printf("\n ");}
	return 0;
}