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

int main(){
    
    int N,T,Q;
    cin >> N >> T >> Q;
    
    int WantAnswer[Q];
    vector<long long> Houses(N),Direction(N);
    
    for(int i = 0;i < N;i++){
        cin >> Houses.at(i) >> Direction.at(i);
    }
    
    for(int i = 0;i < Q;i++){
        cin >> WantAnswer[i];
        WantAnswer[i]--;
    }
    
    for(int t = 0;t < T;t++){
        for(int n = 0;n < N;n++){
            if(count(Houses.begin(),Houses.end(),Houses[n]) < 2){
                if(Direction[n] == 1){
                    Houses[n]++;
                }else{
                    Houses[n]--;
                }
            }
        }
    }
    
    for(int i = 0;i < Q;i++){
        cout << Houses.at(WantAnswer[i]) << endl;
    }
    
    return 0;
}

C++: #include<iostream>
#include<string>
#include<cmath>
#include<queue>
#include<map>
#include<set>
#include<list>
#include<iomanip>
#include<vector>
#include<functional>
#include<algorithm>
#include<cstdio>
#include<unordered_map>
using namespace std;
typedef long long ll;
ll a[100000], b[100000], inf = 99999999999999;
int c[100000];
int main() {
	ll n, t, q;
	cin >> n >> t >> q;
	for (ll i = 0; i < n; i++) {
		cin >> a[i] >> c[i];
		b[i] = inf;
	}
	for (ll i = 0; i < n - 1; i++) {
		if (c[i]== 1 && c[i + 1] == 2) {
			ll t = (a[i] + a[i + 1]) / 2, j;
			j = i;
			while (j>=0&&c[j] == 1) {
				b[j] = t;
				j--;
			}
			j = i + 1;
			while (j<n&&c[j] == 2) {
				b[j] = t;
				j++;
			}
		}
	}
	for (ll i = 0; i < q; i++) {
		ll d;
		cin >> d;
		d -= 1;
		if (abs(b[d] - a[d]) <= t) {
			cout << b[d] << endl;
		}
		else {
			if (c[d] == 1) cout << a[d] + t << endl;
			else cout << a[d] - t << endl;
		}
	}
}

C++: #include <bits/stdc++.h>
#define ll long long
#define rep(i,n) for(int i=0;i<n;i++)
#define INF 100000005
#define MAX 5001
#define fi first
#define se second
using namespace std;
//__gcd(a,b), __builtin_popcount(a);

int a[1002][1002];
int dp[1002][1002];
int main(){
	int h, w, n;
	while(1){
	scanf("%d%d%d", &h, &w, &n);
	if(!h)break;
	fill((int*)a, (int*)(a+1002), 0);
	fill((int*)dp, (int*)(dp+1002), 0);
	for(int i = 1;i <= h;i++)
	for(int j = 1;j <= w;j++)scanf("%d", &a[i][j]);
	n--;dp[1][1] = n;
	for(int i = 1;i <= h;i++){
		for(int j = 1;j <= w;j++){
			dp[i][j] += dp[i][j-1]/2;
			if(a[i][j-1] == 1)dp[i][j] += dp[i][j-1]%2;
			dp[i][j] += dp[i-1][j]/2;
			if(a[i-1][j] == 0)dp[i][j] += dp[i-1][j]%2;
		}
	}
	
	int y = 1, x = 1, t, f;
	while(y <= h && x <= w){
		f = a[y][x];
		if(dp[y][x]%2)f = !f;
		if(f == 0)y++;
		else x++;
	}
	printf("%d %d\n", y, x);
	}
	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
typedef struct{
  int first,second;
  long long place;
}PA;
vector<PA> vec;

void east(int n,long long place){
  if(vec[n].second == 1){
    vec[n].place = place;
    east(n-1,place);
  }
}

void west(int n,long long place){
  if(vec[n].second == 2){
    vec[n].place = place;
    west(n+1,place);
  }
}

int main(){
  int n,q,b,c;
  long long t,a;
      
  scanf("%d %lld %d",&n,&t,&q);
  
  for(int i=0;i<n;i++){
    scanf("%lld %d",&a,&b);
    vec.push_back( (PA){a,b,LLONG_MAX});
  }
  
  for(int i=0;i<n-1;i++){
    if( vec[i].second == 1 && vec[i+1].second == 2){
      vec[i].place = (vec[i].first+vec[i+1].first)/2;
      east(i-1,vec[i].place);
      west(i+1,vec[i].place);
    }
  }

  for(int i=0;i<q;i++){
    scanf("%d",&c);
    c--;
    
    if(vec[c].place == LLONG_MAX || abs((int)(vec[c].first-vec[c].place)) > t){
      if(vec[c].second == 1) cout << vec[c].first+t << endl;
      if(vec[c].second == 2) cout << vec[c].first-t << endl;
    } else cout << vec[c].place << endl;
  }
  return (0);
}

C++: //
//  main.cpp

#include <iostream>
using namespace std;
using ll = long long;

int main(){	
	int N,Q;
	ll T;
	cin >> N >> T >> Q;
	
	ll people[1001];
	ll A[100001];
	int D[100001];
	for(int i=0;i<N;i++){
		cin >> A[i] >> D[i];
	}
	for(int i=0;i<Q;i++){
		cin >> people[i];
		people[i]--;
	}
	
	ll east = 0;
	ll west = 0;
	ll t = 0;
	for(int i=0;i<Q;i++){
		if(D[people[i]] == 1){
			east = A[people[i]];
			for(ll j=people[i]+1;j<N;j++){
				if(D[j] == 1){
					east = A[j];
				}else{
					t = (east + A[j])/2;
					break;
				}
			}
			if(T < abs(t - A[people[i]])){
				cout << A[people[i]] + T << endl;
			}else{
				cout << t << endl;
			}
		}else{
			west = A[people[i]];
			for(ll j=people[i]-1;j>=0;j--){
				if(D[j] == 1){
					t = (west + A[j])/2;
					break;
				}else{
					west = A[j];
				}
			}
			if(T < abs(A[people[i]] - t)){
				cout << A[people[i]] - T << endl;
			}else{
				cout << t << endl;
			}
		}
	}

	return 0;
}