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++)
	{
		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 PB push_back
#define MP make_pair
#define FI first
#define SE second
using namespace std;
typedef pair<int,int> pii;
static const int inf = 1ll<<60;
static const int MAX_N = 100000;

int N,T,Q;
vector<pii> A;
int flg[MAX_N];
int table[MAX_N];
int next[MAX_N];

signed main(){
    memset(next,-1,sizeof(next));
    cin>>N>>T>>Q;
    for(int i=0;i<N;++i){
        int a,d;
        cin>>a>>d;
        if(d!=1)d=-1;
        A.PB(pii(a,d));
    }
    sort(A.begin(),A.end());
    int maxd=0;
    int t=0;
    for(int i=0;i<N;++i){
        if(flg[i]==1)continue;
        if(A[i].SE!=1||A[i+1].SE!=-1){
            if(A[i].SE==1)table[i]=t;
            else table[i]=t-1;
            continue;
        }
        cout<<i<<endl;
        int D=abs(A[i].FI-A[i+1].FI)/2;
        if(D<=T){
            maxd=max(D,maxd);
            A[i].FI+=A[i].SE*D;
            A[i+1].FI+=A[i+1].SE*D;
            flg[i]=flg[i+1]=1;
            next[t]=A[i].FI;
            t++;
        }
    }
    for(int i=0;i<N;++i){
        if(flg[i]==1)continue;
        if(A[i].SE==1)A[i].FI+=maxd;
        else A[i].FI-=maxd;
    }
    T-=maxd;
    for(int i=0;i<N;++i){
        if(flg[i]==1)continue;
        if(A[i].SE==1){
            int k=table[i];
            if(next[k]==-1)A[i].FI+=T;
            else{
                int d=abs(A[i].FI-next[k]);
                if(d<=T)A[i].FI=next[k];
                else A[i].FI+=T;
            }
        }
        else{
            int k=table[i];
            //cout<<next[k]<<endl;
            if(next[k]==-1)A[i].FI-=T;
            else{
                int d=abs(A[i].FI-next[k]);
                if(d<=T)A[i].FI=next[k];
                else A[i].FI-=T;
            }
        }
    }
    for(int i=0;i<Q;++i){
        int q;
        cin>>q;
        q--;
        cout<<A[q].FI<<endl;
    }
}

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

using ll = long long;

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

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[i].remain + c[i].a - c[j].a;
            }
            for (j = i + 2; j < n and c[j].d == c[i + 1].d; ++j) {
                c[j].remain = c[i + 1].remain + c[j].a - c[i + 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);
    }
    vector<int> x;
    for (int i = 0; i < q; i++) {
        int t;
        cin >> t;
        x.emplace_back(t - 1);
    }

    solve(citizens, t);

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

    return 0;
}