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++: #define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <cstring>
#include <stack>
#include <queue>
using namespace std;
#define rep(i,n) for(int i = 0; i<n; i++)

int N, Q;
long long int T;
pair<long long int,int> P[100000];

int main(){
	scanf("%d %lld %d", &N, &T, &Q);
	rep(i, N){
		long long int A;
		int D;
		scanf("%lld %d", &A, &D);
		P[i].first = A;
		P[i].second = D;
	}
	rep(i, Q){
		int X;
		scanf("%d", &X);
		X = X - 1;
		int a,b;
		int flaga=0;
		int flagb = 0;
		if (P[X].second == 1){
			for (int j = X + 1; j < N; j++){
				if (P[j].second != P[X].second){
					b = j;
					flagb = 1;
					break;
				}
			}
		}
		else{
			for (int j = X - 1; j >= 0; j--){
				if (P[j].second != P[X].second){
					b = j;
					flagb = 1;
					break;
				}
			}
		}
		if (flagb == 0){
			printf("%lld\n", P[X].first + (P[X].second == 1 ? 1 : -1)*T);
			continue;
		}
		if (P[X].second == 1){
			for (int j = b - 1; j > X; j--){
				if (P[j].second == P[X].second){
					a = j;
					flaga = 1;
					break;
				}
			}
		}
		else{
			for (int j = b + 1; j < X; j++){
				if (P[j].second == P[X].second){
					a = j;
					flaga = 1;
					break;
				}
			}
		}
		if (flaga == 0&&flagb==1){
			//A????????????????????
			long long bxt = abs(P[X].first - P[b].first) / 2;
			if (T < bxt)printf("%lld\n", P[X].first + (P[X].second == 1 ? 1 : -1)*T);
			else printf("%lld\n", P[X].first + (P[X].second == 1 ? 1 : -1)*bxt);
			continue;
		}
		if(flaga==1&&flagb==1){
			long long abt = abs(P[a].first - P[b].first) / 2;
			long long abxd = abs(P[X].first - P[a].first) + abt;
			if (T < abxd)printf("%lld\n", P[X].first + (P[X].second == 1 ? 1 : -1)*T);
			else printf("%lld\n", P[X].first + (P[X].second == 1 ? 1 : -1)*abxd);
			continue;
		}
	}
	return 0;
}

C++: #include<bits/stdc++.h>
#define INF 1000100010001000100
using namespace std;
long long int p[100010],ans[100010],dire[100010],n,t,q,x[100010],mm,s1;


int main(){

    for(int i=0;i<100010;i++)ans[i]=INF;
    mm=INF;
    cin >> n >> t >> q;
    for(int i=1;i<=n;i++)cin >> x[i] >> dire[i];

    for(int i=1;i<=n;i++){
        if(dire[i]==1&&dire[i+1]==2){
            mm=(x[i]+x[i+1])/2;
            ans[i]=mm,ans[i+1]=mm;

            for(int j=i-1;j>0;j--){
                if(dire[j]==1)ans[j]=mm;
                else break;
            }
        }else if(dire[i]==2)ans[i]=mm;
    }

    for(int i=0;i<q;i++){
        cin >> mm;
        s1=x[mm]-ans[mm];
        if(s1<0)s1=-s1;
        //cout << "??????????????? " << x[mm] << "  ?????????????????? "  << x[mm]-t << "  ??¶????????£?????´??? " << ans[mm] << endl;
        if(t<s1){
            if(dire[mm]==1)cout << x[mm]+t << endl;
            else cout << x[mm]-t << endl;
        }else cout << ans[mm] << endl;
    }

return 0;
}

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

#define rep(i, n) for(int i = 0; i < (n); ++i)

using namespace std;

typedef long long ll;

int n;
ll t;
int q;
ll a[100000];
int d[100000];
int x[1000];

int main(){
	scanf("%d%lld%d", &n, &t, &q);
	rep(i, n){
		scanf("%lld%d", &a[i], &d[i]);
	}
	rep(i, q){
		scanf("%d", &x[i]);
	}

	int i = 0;
	while(i < n && d[i] == 2){
		a[i++] -= t;
	}
	while(i < n){
		int j = 0;
		while(i + j < n && d[i + j] == 1){
			++j;
		}
		int k = 0;
		while(i + j + k < n && d[i + j + k] == 2){
			++k;
		}
		if(k == 0){
			rep(z, j){
				a[i + z] += t;
			}
		}
		else{
			ll p = (a[i + j - 1] + a[i + j]) / 2;
			rep(z, j){
				a[i + z] = min(a[i + z] + t, p);
			}
			rep(z, k){
				a[i + j + z] = max(a[i + j + z] - t, p);
			}
		}
		i += j + k;
		printf("%d\n", i);
	}

	rep(i, q){
		printf("%lld\n", a[x[i] - 1]);
	}
	return 0;
}

C++: #include <stdio.h>
typedef long long ll;

int max(int a, int b) {
  return a > b ? a : b;
}

int min(int a, int b) {
  return a < b ? a : b;
}

int main(void) {
  ll n, t, q, i, j, k;
  scanf("%lld%lld%lld", &n, &t, &q);
  ll a[n], d[n], x[n], now, s = 0, be, bel, l = 2;
  for(i = 0; i < n; ++i) scanf("%lld%lld", &a[i], &d[i]);
  for(i = 0; i < n; ++i) {
    if(d[i] == 1) {
      s = 1;
      be = i;
      if(!i || (i &&  d[i - 1] == 2)) bel = i;
      l = 1;
    } else {
      if(be == i - 1) {
        now = (a[be] + a[i]) / 2;
        for(j = bel; j < i; ++j) x[j] = min(now, a[j] + t);
      } 
     x[i] = max(now, a[i] - t);
      l = 2;
    }
    if(!s) {
      x[i] = a[i] - t;
    }
  }
  if(l == 1) for(i = bel; i < n; ++i) x[i] = a[i] + t;
  /*for(i = 0; i < n; ++i) printf("%lld ", x[i]);
  printf("\n");*/
  for(i = 0; i < q; ++i) {
    int p;
    scanf("%d", &p);
    p--;
    printf("%lld\n", x[p]);
  }
  return 0;
}

C++: #include "bits/stdc++.h"
#define in std::cin
#define out std::cout
#define rep(i,N) for(LL i=0;i<N;++i)
typedef long long int LL;

struct group { LL begin, end, pos; };

LL N, T, Q, A[112345], D[112345], X[1123];
std::vector<group>list;

int main()
{
	in >> N >> T >> Q;
	rep(i, N) { in >> A[i] >> D[i]; }
	rep(i, Q) { in >> X[i]; }

	rep(i, N)
	{
		if (D[i] == 1)
		{
			LL p;
			bool eastFlag = true, meetFlag = true;
			for (LL j = i; j < N; ++j)
			{
				if (eastFlag)
				{
					if (D[j] == 2)
					{
						if (A[j] - A[j - 1] <= T * 2) { p = (A[j] + A[j - 1]) / 2; }
						else { meetFlag = false; }
						eastFlag = false;
					}
				}
				else
				{
					if (D[j] == 1)
					{
						if (meetFlag) { list.push_back(group{ i,j - 1,p }); }
						i = j - 1;
						break;
					}
					if (j == N - 1)
					{
						if (meetFlag) { list.push_back(group{ i,j,p }); }
						i = j;
						break;
					}
				}
			}
		}
	}
	int seek = 0;
	rep(i, N)
	{
		if (seek < (signed)list.size())
		{
			if (list[seek].begin <= i && i <= list[seek].end)
			{
				if (abs(A[i] - list[seek].pos) <= T) { A[i] = list[seek].pos; }
				else { A[i] += (D[i] == 1 ? T : -T); }
			}
			else if (list[seek].end < i) { ++seek; --i; }
			else { A[i] += (D[i] == 1 ? T : -T); }
		}
		else { A[i] += (D[i] == 1 ? T : -T); }
	}

	rep(i, Q) { out << A[X[i] - 1] << std::endl; }
	return 0;
}