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 rep(i,n)for(ll i=0;i<n;i++)
using namespace std;
typedef long long ll;

ll n, t, q, x[100000], a[100000];
int d[100000];
vector<ll>v;


int main() {
	scanf("%lld%lld%lld", &n, &t, &q);
	rep(i, n)scanf("%lld%d", &a[i], &d[i]);
	rep(i, q)scanf("%lld", &x[i]);
	rep(i, n - 1) {
		if (d[i] == 1 && d[i + 1] == 2 && a[i + 1] - a[i] <= t * 2) {
			d[i] = d[i + 1] = 0;
			a[i] = a[i + 1] = (a[i + 1] + a[i]) / 2;
			v.push_back(a[i]);
		}
	}
	rep(i, n) {
		if (!d[i])continue;
		ll j = lower_bound(v.begin(), v.end(), a[i]) - v.begin();
		if (j < v.size() && d[i] == 1 && v[j] - a[i] <= t) {
			a[i] = v[j];
			d[i] = 0;
		}
		j--;
		if (0 <= j&&d[i] == 2 && a[i] - v[j] <= t) {
			a[i] = v[j];
		}
	}
	rep(j, q) {
		int i = x[j] - 1;
		if (d[i] == 1)printf("%lld\n", a[i] + t);
		else if (d[i])printf("%lld\n", a[i] - t);
		else printf("%lld\n", a[i]);
	}
}

C++: #include <bits/stdc++.h>
using namespace std;
#define all(c) (c).begin(),(c).end()
#define rrep(i,n) for(int i=(int)(n)-1;i>=0;i--)
#define REP(i,m,n) for(int i=(int)(m);i<(int)(n);i++)
#define rep(i,n) REP(i,0,n)
#define iter(c) __typeof((c).begin())
#define tr(it,c) for(iter(c) it=(c).begin();it!=(c).end();it++)
#define mem(a) memset(a,0,sizeof(a))
#define pd(a) printf("%.10f\n",a)
#define pb(a) push_back(a)
#define in(a) insert(a)
#define pi M_PI
#define R cin>>
#define F first
#define S second
#define C class
#define ll long long
#define ln cout<<'\n'
template<C T>void pr(T a){cout<<a;ln;}
template<C T,C T2>void pr(T a,T2 b){cout<<a<<' '<<b;ln;}
template<C T,C T2,C T3>void pr(T a,T2 b,T3 c){cout<<a<<' '<<b<<' '<<c;ln;}
template<C T>void PR(T a,ll n){rep(i,n){if(i)cout<<' ';cout<<a[i];}ln;}
ll check(ll n,ll m,ll x,ll y){return x>=0&&x<n&&y>=0&&y<m;}
const ll MAX=1000000007,MAXL=1LL<<60,dx[4]={-1,0,1,0},dy[4]={0,1,0,-1};
typedef pair<ll,ll> P;

void Main() {
  ll n,m,k;
  cin >> n >> m >> k;
  vector<P> l,r;
  l.pb(P(MAXL,-1));
  l.pb(P(-MAXL,-2));
  rep(i,n) {
    ll x,y;
    cin >> x >> y;
    if(y==1) r.pb(P(x,i+1));
    else l.pb(P(x,i+1));
  }
  sort(all(l));
  sort(all(r));
  map<ll,ll> ma;
  ma[-2]=-MAXL;
  rrep(i,r.size()) {
    int x=upper_bound(all(l),r[i])-l.begin();
    ll d=(l[x].F+r[i].F)/2;
    if(ma.count(l[x].S)) d=max(d,ma[l[x].S]);
    ma[r[i].S]=min(d,r[i].F+m);
    ma[l[x].S]=max(d,l[x].F-m);
  }
  REP(i,1,l.size()-1) {
    if(!ma.count(l[i].S)) ma[l[i].S]=max(ma[l[i-1].S],l[i].F-m);
  }
  rep(i,k) {
    int x;
    R x;
    pr(ma[x]);
  }
}

int main() {
  ios::sync_with_stdio(0);cin.tie(0);
  Main();return 0;
}

C++: // aaaaaaaaaaaaa
#include<stdio.h>
#include<stdlib.h>
#include<algorithm>
using namespace std;
long long n,t,q,a[100001],d[100001],x[1001],s[100001],k,f[1001],flg,i,j;
int main(void)
{
	scanf("%lld %lld %lld",&n,&t,&q);
	for(i=1;i<=n;i++){
		scanf("%lld %lld",&a[i],&d[i]);
	}
	for(i=1;i<=q;i++){
		scanf("%lld",&x[i]);
	}
	k=0;
	for(i=1;i<n;i++){
		if(d[i]==1&&d[i+1]==2&&a[i]+t*2>=a[i+1]){
			s[k]=a[i]-(a[i]-a[i+1])/2;
			k++;
		}
	}
	for(j=1;j<=q;j++){
		flg=0;
		for(i=0;i<k;i++){
			if(d[x[j]]==1&&s[i]>=a[x[j]]&&s[i]-t<=a[x[j]]){
				f[j]=s[i];
				flg=1;
//				printf("%lld %lld\n",f[j],s[i]);
				break;
			}
		}
		for(i=k-1;i>=0;i--)	{
			if(d[x[j]]==2&&s[i]<=a[x[j]]&&s[i]+t>=a[x[j]]){
				f[j]=s[i];
				flg=1;
//				printf("%lld %lld\n",f[j],s[i]);
				break;
			}
		}
		if(flg==0){
			if(d[x[j]]==1) f[j]=a[x[j]]+t;
			else f[j]=a[x[j]]-t;
//			printf("%lld\n",f[j]);
		}
	}
	for(i=1;i<=q;i++){
		printf("%lld\n",f[i]);
	}
	return 0;
}

C++: #include <cstdio>
#include <iostream>
#include <algorithm>
#include <vector>
#define INF 3000000000000000000
#define int long long int
#define REP(i,n) for (int i=0;i<(n);i++)
#define RREP(i,n) for (int i=(n-1);i>=0;i--)
using namespace std;


int x[200000][3]={0};//0:x 1:direction
int p[2000];
int ans[2000];

int n,t,q;

void main(){
	scanf("%d%d%d",&n,&t,&q);
	REP(i,n)scanf("%d %d",&x[i][0],&x[i][1]);
	REP(i,q)scanf("%d",&p[i]);
	//search opposite people//calc
	bool flag=false;
	REP(i,n-1){
		if(x[i][1]==1&&x[i+1][1]==2){x[i+1][2]=(x[i+1][0]+x[i][0])/2;x[i][2]=x[i+1][2];}
		if(x[i][1]==2&&x[i+1][1]==2){x[i+1][2]=x[i][2];}
	}
	if(x[n-1][1]==1)x[n-1][2]=INF;
	RREP(i,n){
		if(x[i][1]==1&&x[i-1][1]==1){x[i-1][2]=x[i][2];}
	}
	if(x[0][1]==2)x[0][2]=INF;
	int c;
	//judge
	REP(i,q){
	c=x[p[i]-1][0]-t*(x[p[i]-1][1]*2-3);
	if((x[p[i]-1][1]==1&&c>x[p[i]-1][2])||(x[p[i]-1][1]==2&&c<x[p[i]-1][2]))printf("%d\n",x[p[i]-1][2]);
	else printf("%d\n",c);
	}
	//REP(i,n){REP(j,3)printf("%d ",x[i][j]);printf("\n ");}
	return ;
}

C++: #include<stdio.h>


int main() {
	int N, T, Q;
	int A[10000], D[10000], X[10000];
	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;
}