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>
using namespace std;
#define M 100000


int n,t,q;
int a[M],d[M];
int x;
int m[M]={0};

int meet(int i){
    for (int j=i+1; j<n; j++) {
        if(d[j]==2){
            m[i]=(a[j]+a[j-1])/2;
            break;
        }
    }
    return m[i];
}

void output(){
    for (int i=0; i<q; i++) {
        cin>>x;
        x--;
        if(d[x]==1){
            if(a[x]+t<m[x])cout<<a[x]+t<<endl;
            else cout<<m[x]<<endl;
        }
        else{
            if(a[x]-t>m[x])cout<<a[x]-t<<endl;
            else cout<<m[x]<<endl;
        }
    }
}

int main(){
    cin>>n>>t>>q;
    for (int i=0; i<n; i++)cin>>a[i]>>d[i];
    int tt=a[0];
    for (int i=0; i<n-1; i++) {
        if(d[i]==1)tt=meet(i);
        if(d[i]==2)m[i]=tt;
    }
    if(d[n-1]==1)m[n-1]=a[n-1]+t;
    else m[n-1]=tt;
    output();
    return 0;
}

C++: //============================================================================
// Name        : JOI.cpp
// Author      :
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
// 1. dp?????????
ll dp[100001];
// 2. n?????????
ll N,T,Q;
ll A[1001];
int D[10001];
int X[10001];
// 7. calc?????????
ll calc(ll k){
	// 8. dp???????????°??????
	if(dp[k]!=A[k])return dp[k];
	// 9. dp????¨????

	if(D[k]==1){
		if(D[k+1]==1){
			dp[k]=min(A[k]+T,calc(k+1));
		}else{
			if(abs((A[k]-A[k+1]))>T){
				dp[k]=min(A[k]+T,calc(k+1));
			}else{
				dp[k]=(A[k]+A[k+1])/2;
			}
		}
	}
	if(D[k]==2){
		if(D[k-1]==2){
			dp[k]=max(A[k]-T,calc(k-1));
		}else{
			if(abs((A[k]-A[k-1]))>T){
				dp[k]=max(A[k]-T,calc(k-1));
			}else{
				dp[k]=(A[k]+A[k-1])/2;
			}
		}
	}

	return dp[k];
}
int main() {
	cin>>N>>T>>Q;

	//3.?????????

	//4.??\???
	for(int i=1;i<N+1;i++){
		cin>>A[i]>>D[i];
		dp[i]=A[i];
	}
	for(int i=1;i<Q+1;i++){
		cin>>X[i];
	}

	//5.???????????¨???

	if(D[1]==2){
		dp[1]-=T;
	}

	if(D[N]==1){
		dp[N]+=T;
	}


	//6.??????

	for(int i=1;i<Q+1;i++){
		cout<<calc(X[i])<<endl;
	}

	return 0;
}

C++: 
#include <cstdio>
#include <vector>

using namespace std;

using ll = long long;

const ll inf = 4e18;

ll n, t, q;
ll a[123456];
bool d[123456];
ll x[1000];
vector<ll> talkp;
ll place[123456];

inline ll mean(ll a, ll b) { return a/2 + b/2; }

int main()
{
	scanf("%lld %lld %lld", &n, &t, &q);
	a[0] = -inf; d[0] = true;
	for (int i = 1; i <= n; ++i)
	{
		int tmpd;
		scanf("%lld %d", a + i, &tmpd);
		d[i] = (tmpd == 1);
	}
	a[n + 1] = inf; d[n + 1] = false;
	for (int j = 0; j < q; ++j)
	{
		scanf("%lld", x + j);
	}
	for (int i = 0; i <= n; ++i)
	{
		if (d[i] && !d[i + 1])
		{
			talkp.push_back(mean(a[i], a[i + 1]));
		}
	}
	auto fritr = talkp.begin();
	auto bkitr = talkp.begin();

	for (int i = 0; i <= n; ++i)
	{
		place[i] = a[i];
		while ((*fritr) < a[i])
		{
			bkitr = fritr;
			++fritr;
		}
		if (d[i])
		{
			ll dist = (*fritr) - a[i];
			if (dist < t)place[i] += dist;
			else place[i] += t;
		}
		else
		{
			ll dist = a[i] - (*bkitr);
			if (dist < t)place[i] -= dist;
			else place[i] -= t;
		}
	}
	for (int j = 0; j < q; ++j)
	{
		printf("%lld\n", place[x[j]]);
	}
	return 0;
}

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

ll N,T,Q;
set<ll> S;

ll a[100005];
ll d[100005];

int main(){
  cin>>N>>T>>Q;
  for(int i=0;i<N;i++){
    cin>>a[i]>>d[i];
    if(i>0 && d[i-1]==1 && d[i]==2 ){
      S.insert( a[i]/2LL + a[i-1]/2LL);
    }
  }

  ll INF = 1000000000000000001LL;
  S.insert(INF);
  S.insert(-INF);

  for(int i=0;i<Q;i++){
    ll id;
    scanf("%lld",&id);
    id--;
    if( d[id] == 1 ){
      ll target = *S.lower_bound(a[id]);
      if( abs(target) == INF)cout<<a[id]+T<<endl;
      else cout<<min(a[id]+T,target)<<endl;
    }else{
      set<ll> :: iterator it;
      it=S.lower_bound(a[id]);
      it--;
      ll target = *it;
      if( abs(target) == INF)cout<<a[id]-T<<endl;
      else cout<<max(a[id]-T,target)<<endl;
    }
  }
  return 0;
}