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 <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <numeric>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <utility>
#include <sstream>
#include <complex>
#include <fstream>

using namespace std;

#define FOR(i,a,b) for(long long i=(a);i<(b);i++)
#define REP(i,N) for(long long i=0;i<(N);i++)
#define ALL(s) (s).begin(),(s).end()
#define fi first
#define se second

#define PI acos(-1.0)
#define INF 10e9+7
#define EPS 1e-10
#define MAX_N 100100
#define MAX_M 100100

typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<double, double> PD;
typedef pair<string, ll> PS;
typedef vector<ll> V;
typedef pair<P, char> PC;

ll n, t;
ll a[100100], d[100100];
ll q, x;

int main(){
	cin >> n >> t >> q;
	FOR(i, 1, n + 1){
		cin >> a[i] >> d[i];
	}
	REP(i, q){
		ll goal;
		ll ga = -1, pa = -1;
		cin >> x;
		if (d[x] == 1){
			for (ll j = x + 1; j < n; j++){
				if (d[x] != d[j]){
					ga = j;
					break;
				}
				else pa = j;
			}
		}
		else{
			for (ll j = x - 1; j >= 0; j--){
				if (d[x] != d[j]){
					ga = j;
					break;
				}
				else pa = j;
			}
		}
		if (ga == -1){
			if (d[x] == 1) cout << a[x] + t << endl;
			else cout << a[x] - t << endl;
		}
		else{
			ll goal;
			if (pa == -1){
				goal = (a[ga] + a[x]) / 2;
			}
			else{
				goal = (a[ga] + a[pa]) / 2;
			}
			if (d[x] == 1){
				if (goal - a[x] <= t)cout << goal << endl;
				else cout << a[x] + t << endl;
			}
			else {
				if (a[x] - goal <= t)cout << goal << endl;
				else cout << a[x] - t << endl;
			}
		}
	}
}

C++: #include<stdio.h>
#include<math.h>
#include<algorithm>
#include<cctype>
#include<string.h>
#include <stdlib.h>

#define PI 3.1415926
void swap(int* x, int* y) {
	int a = *x;
	int b = *y;
	*x = b;
	*y = a;
}
int factorial(int n);
int sum(int n, int*);
int ave(int n, int*);
int min(int n, int*), min(int n, int m);
int max(int n, int*), max(int n, int m);

int main() {
	int N, T, Q;
	int A[1000], D[1000], X[1000];
	scanf("%d %d %d", &N, &T, &Q);
	for (int i = 1; i <= N; i++) {
		scanf("%d %d", &A[i], &D[i]);
		if (D[i] == 2) D[i] = -1;
	}
	for (int i = 1; i <= Q; i++) {
		scanf("%d", &X[i]);
	}
	for (int i = 1; i <= T; i++) {
		for (int j = 1; j <= N; j++) {
			A[j] += D[j];
			if (A[j] == A[j - 1]) {
				D[j] = 0;
				D[j - 1] = 0;
			}
			if (A[j] == A[j + 1]) {
				D[j] = 0;
				D[j + 1] = 0;
			}
		}
	}
	for (int i = 1; i <= Q; i++) {
		printf("%d\n", A[X[i]]);
	}
	
	return 0;
}

int factorial(int n) {
	if (n > 0) {
		return n * factorial(n - 1);
	}
	else {
		return 1;
	}
}
int sum(int n, int *all) {
	int total = 0;
	for (int i = 0; i < n; ++i) {
		total += all[i];
	}
	return total;
}
int ave(int n, int *all) {
	int total = 0;
	for (int i = 0; i<n; ++i) {
		total += all[i];
	}
	return total / n;
}
int min(int n, int *all) {
	int min = all[0];
	for (int i = 1; i < n; ++i) {
		if (min > all[i]) {
			min = all[i];
		}
	}
	return min;
}
int max(int n, int *all) {
	int max = all[0];
	for (int i = 1; i<n; ++i) {
		if (max<all[i]) {
			max = all[i];
		}
	}
	return max;
}
int max(int n, int m) {
	if (n >= m) {
		return n;
	}
	else {
		return m;
	}
}
int min(int n, int m) {
	if (n >= m) {
		return m;
	}
	else {
		return n;
	}
}

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

typedef long long ll;

const int N_MAX = 100000;
int N;

ll T;
int Q;

vector<ll> meet;

ll A[N_MAX];
int D[N_MAX];
int E[N_MAX];

int swi;
ll againster;
int x;
int main()
{
	cin >> N >> T >> Q;
	swi = 2;
	for(int i = 0;i < N;i++)
	{
		cin >> A[i];
		cin >> D[i];
		if(D[i] == 1)
		{
			swi = 1;
			againster = A[i];
		}
		else if (swi == 1 && D[i] == 2)
		{
			meet.push_back((againster + A[i]) / 2);
			swi = 2;
		}
		E[i] = meet.size();
	}
	for(int i = 0;i < Q;i++)
	{
		cin >> x;
		x--;
		if(D[x] == 1)
		{
			if(E[x] == meet.size())
			{
				cout << A[x] + T;
			}
			else
			{
				cout << min(A[x] + T,meet[E[x]]);
			}
		}
		else
		{
			if(E[x] == 0)
			{
				cout << A[x] - T;
			}
			else
			{
				cout << max(A[x] - T,meet[E[x] - 1]);
			}
		}
		cout << endl;
	}
	return 0;
}

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

int main()
{
	int N, Q;
	ll T;
	cin >> N >> T >> Q;
	vector<ll> A(N), D(N);
	for (int i = 0; i < N; i++) {
		cin >> A[i] >> D[i];
	}
	vector<ll> pos;
	for (int i = 1; i < N; i++) {
		if (D[i - 1] == 1 && D[i] == 2) {
			pos.push_back((A[i - 1] + A[i]) / 2);
		}
	}
	while (Q--) {
		int X;
		cin >> X; X--;
		if (D[X] == 1) {
			int id = lower_bound(pos.begin(), pos.end(), A[X]) - pos.begin();
			if (id == (int)pos.size()) {
				printf("%lld\n", A[X] + T);
			}
			else {
				printf("%lld\n", min(A[X] + T, pos[id]));
			}
		}
		else {
			int id = upper_bound(pos.begin(), pos.end(), A[X]) - pos.begin() - 1;
			if (id == -1) {
				printf("%lld\n", A[X] - T);
			}
			else {
				printf("%lld\n", max(A[X] - T, pos[id]));
			}
		}
	}
	return 0;
}

C++: #include<stdio.h>
int main(void)
{
	int n,t,q,a[100001],flgd[100001],Q[1001],x,i;
	scanf("%d%d%d",&n,&t,&q);
	for(i=0;i<n;i++) scanf("%d%d",&a[i],&flgd[i]);
	for(i=0;i<q;i++) scanf("%d",&Q[i]);
	for(i=0;i<n;i++){
	//	printf("%d=%d %d\n",i,flgd[i],flgd[i+1]);
		if(flgd[i]==1&&flgd[i+1]==2){
			flgd[i+1]=0;
			a[i]+=t; a[i+1]-=t;
			if(a[i]>=a[i+1]){
				x=0;
				a[i]-=t; a[i+1]+=t;
				x=(a[i+1]-a[i])/2;
				a[i]+=x; a[i+1]-=x;
			}
		}
		else if(flgd[i]==0&&flgd[i+1]==2){
			flgd[i+1]=0; a[i+1]-=t;
			if(a[i]>=a[i+1]){
				a[i+1]=a[i];
			}
		}
		else{
			if(flgd[i]==1) a[i]+=t;
			if(flgd[i]==2) a[i]-=t;
			flgd[i]=0;
		}
	}
	for(i=0;i<n-1;i++){
		if(a[i]>a[i+1]){
			a[i]=a[i+1];
		}
	}
	for(i=0;i<q;i++){
		printf("%d\n",a[Q[i]-1]);
	}
	return 0;
}