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 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+5];
int table[MAX_N+5];
int next[MAX_N+5];

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;
        }
        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];
            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 <iostream>
#include <algorithm>
#include<cstdio>
#include<string>
#include<vector>
using namespace std;
int main() {
	long long int n, t;
	int q;
	cin >>n >>t >>q;
	vector<long long int> w;
	
	int home[100005] = { 0 };
	
	
	for (long long int i = 0; i < n; i++) {
		long long int a;
		cin >> a >> home[i];
		w.push_back(a);
	}
	
	int key[100005] = {0};
	for (int i = 0; i < q; i++) {
		int a;
		cin >> a;
		key[a - 1] = 1;
	}
	int west = 0;
	
	
	long long int east2 = 0;
	vector<long long int> talkpoint;
	long long int tp;
	for (long long int i = 0; i < n; i++) {
		
			if (home[i]==2) {
				if (west==0) {
					
						long long int b = w[i] - t;
						talkpoint.push_back(b);
				
				}
				else {
					talkpoint.push_back(tp);
					
					
				}
			}
			if (home[i]==1) {
				
					if (home[i + 1] == 2) {
						tp = (w[i] + w[i + 1]) / 2;
						for (long long int j = 0; j <= east2; j++) {
							talkpoint.push_back(tp);
						}
						west = 1;
						east2 = 0;
					}
					else if (i == n - 1) {
						
						for (long long int j = east2; j >=0; j--) {
							long long int d = w[i-j] + t;
							talkpoint.push_back(d);
							east2 = 0;
						}
						
					}
					else {
						east2++;
						west = 1;
					}
				

			}

		
	}
	for (long long int i = 0; i < n; i++) {
		if (key[i] == 1) {
			
			long long int e;
			if (talkpoint[i] < w[i]) {
				e = w[i] - talkpoint[i];
			}
			else {
				e = talkpoint[i] - w[i];
			}
			if (e <= t) {
				cout << talkpoint[i] << endl;
			}
			else {
				if (home[i] == 1) {
					cout << w[i] + t << endl;
				}
				else {
					cout << w[i] - t << endl;
				}
			}
		}
	}
	return 0;
}

C++: #include <bits/stdc++.h>
#define int long long
#define r(i,n) for(int i=0;i<n;i++)
using namespace std;
int n,t,q,x[100001],y[100001],p,z;
vector<int>v;
main(){
	cin>>n>>t>>q;
	v.push_back(-1e19);
	r(i,n){
		scanf("%lld%lld",&x[i],&y[i]);
		if(i&&y[i]!=y[i-1])v.push_back((x[i]+x[i-1])/2);
	}
	v.push_back(1e19);
	r(i,q){
		scanf("%lld",&p);p--;
		if(y[p]==1){
			z=*lower_bound(v.begin(),v.end(),x[p]);
			if(t+x[p]<z)cout<<t+x[p]<<endl;
			else cout<<z<<endl;
		}
		else{
			z=*(lower_bound(v.begin(),v.end(),x[p])-1);
			if(x[p]-t>z)cout<<x[p]-t<<endl;
			else cout<<z<<endl;
		}
	}
}

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

long long A[100000] = {};
long long X[100000] = {};
long long D[10000] = {};
long long dp[2][1000] = {};
int main() {
	long long N, T, Q;

	cin >> N >> T >> Q;
	
	for (long long i = 0; i < N; i++) {
		cin >> A[i] >> D[i];
	}
	for (long long i = 0; i < Q; i++) {
		cin >> X[i];
	}

	for (long long i = 0; i <= T; i++) {
		for (long long j = 0; j < N; j++) {
			if (i == 0) {
				dp[0][j] = A[j];
			}
			else {
				for (long long k = 0; k < N; k++) {
					if (k != j) {
						if (dp[0][k] == dp[1][j]) {
							D[j] = -1;
						}
					}
				}
				for (long long k = 0; k < j - 1; k++) {
					if (dp[0][k] == dp[1][j]) {
						D[j] = -1;
					}
				}
				if (D[j] == 2) {
					dp[1][j]=dp[0][j]-1;
				}
				else if(D[j] == 1) {
					dp[1][j]=dp[0][j]+1;
				}

			}
		}
		if (i != 0) {
			for (long long k = 0; k < N; k++) {
				dp[0][k] = dp[1][k];
			}
		}

	}
	
	for (long long i = 0; i < Q; i++) {
		cout << dp[0][X[i]-1] << endl;
	}
}

C++: #include<iostream>
#include<string>
#include<algorithm>
#include<math.h>
#include<queue>
#include<set>
#include<map>

using namespace std;


long long N, T, Q;
long long a[100000], d[100000], q[1000];
vector<long long> stop;

int main() {
	cin >> N >> T >> Q;

	for (int i = 0; i < N; i++) {
		cin >> a[i] >> d[i];
	}
	for (int i = 0; i < Q; i++) {
		cin >> q[i];

		q[i]--;
	}

	for (int i = 0; i < N-1; i++) {
		if (d[i] < d[i + 1]) {
			stop.push_back((a[i]+a[i+1]) / 2);
		}
	}

	for (int i = 0; i < Q; i++) {

		if (d[ q[i]] == 1) {
			for (int j = 0; j < stop.size(); j++) {

				if (a[q[i]] < stop[j]) {
					cout << min(a[q[i]] + T, stop[j]) << endl;
					break;
				}
				else if (j == stop.size() - 1) {
					cout << a[q[i]] + T << endl;
				}
			}
		}

		if (d[q[i]] == 2) {
			for (int j = stop.size()-1; j >= 0; j--) {

				if (a[q[i]] > stop[j]) {
					cout << max(a[q[i]] - T, stop[j]) << endl;
					break;
				}
				else if (j == 0) {
					cout << a[q[i]] - T << endl;
				}
			}
		}


	}


	return 0;
}