Problem:
Problem F
Billiards
You are challenging a game with prizes, which is similar to billiards.
    This game uses a kind of billiard table, several balls of the same size, and a prize coin.
    The top of the table has a flat rectangular cloth-covered area bounded by
    elastic bumpers, called
cushions.
The balls can move on this area.
    The cushions limit the moves
    so that the centers of the balls are restricted to stay
    within a rectangular area of width
a
and length
b,
called the
movable area.
Let the coordinates of the four corners of the movable area be (0, 0), (
a,
0), (
a, b
), and (0,
b
).
    At the start of a game,
    all the balls and the coin are placed on the table so that their centers are
    at distinct inner lattice points on the movable area, that is, not on its boundaries.
    As the balls and the coin are small enough,
    they do not touch each other unless a ball goes toward the center of another ball or the coin.
The challenger chooses one of the balls, and strikes it with a cue
    to the direction with an angle of 45 degrees to the edges
    of the boundary of the movable area.
    The direction is such that both
x-
and
y-
coordinates increase.
    The struck ball will move straight until it reaches a boundary
    of the movable area, hits another ball,
    or drops into one of the holes placed at four corners.
    When the ball reaches a boundary, it bounces on the cushion
    at the reflection angle equal to its incident angle and continues its
    straight move.
    When it hits another ball or drops into a corner hole, the game is over.
    If it hits the coin before hitting another ball or dropping into a hole,
    the challenger will win the coin.
Which ball should you strike to win the coin?  You want to know
    all the balls bringing you the coin.
Figure F-1 depicts the first and the second datasets of Sample Input.
Figure F-1: The first and the second datasets of Sample Input
For the first dataset,
    when the ball No. 2 is struck, it goes directly toward the coin.
    The ball No. 4 hits the coin after bouncing on the right-side
    cushion.
    As the ball No. 1 hits the ball No. 2,
    and the ball No. 3 drops in the upper right hole,
    striking them will not be awarded a coin.
For the second dataset, when the only ball No. 1 is struck,
    after bouncing on the cushions 5 times, it will pass through its original
    position, and then after bouncing twice more, it reaches the coin.
Input
The input consists of multiple datasets, each in the following format.
a
b
x
y
n
x
1
y
1
⋮
x
n
y
n
The first line has five integers.
a
and
b
are the width and the length of the movable area,
    respectively,
    satisfying 2 ≤
a
≤ 10
6
and
    2 ≤
b
≤ 10
6
.
x
and
y
give the coordinates (
x, y
) of the position of the coin,
    satisfying 1 ≤
x
<
a
and 1 ≤
y
<
b.
n
is the number of balls,
    satisfying 1 ≤
n
≤ 10
5
.
The balls are numbered from 1 to
n,
and the
i
-th
    line in the following
n
lines has the
    c
...(truncated)

Submitted solutions:

code_snippet: // I made some changes to the original code.

// #include<bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<set>
#include<tuple>
#include<vector>
#include<utility>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> pll;
#define rep(i,n) for(int i = 0;i < n;i++)
#define per(i,n) for(int i = n - 1;i >= 0;i--)
#define fore(e,v) for(auto &&e,v)
#define si(a) (int)(size(a))
#define all(a) begin(a),end(a)
#define lb(v,x) (lower_bound(all(v),x) - begin(v))
#define eb emplace_back

template<typename T , typename S> bool chmin(T &a, const S& b){
	return a > b ? a = b, 1:0;
}
template<typename T , typename S> bool chmax(T &a, const S& b){
	return a < b ? a = b, 1:0;
}

const int inf = 3e8;

// struct _{
// 	_(){
// 		cin.tie(0) -> sync_with_stdio(0),cout.tie(0);
// 	}
// }__;

int main(){
	// cout << "start" << endl;
	while(1){
		int a,b,x,y,n;
		cin>>a>>b>>x>>y>>n;
		if(a + b + x + y + n == 0)break;
		ll base = 1e6 + 10;
		vector<pll>apb_a[2 * base], amb_a[2 * base];
		rep(i,n){
			ll x,y;
			cin>>x>>y;

			apb_a[x + y].eb(x,i);
			amb_a[base + x - y].eb(x,i);
		}
		rep(i,2 * base){
			sort(all(apb_a[i]));
			sort(all(amb_a[i]));
		}
		ll dx[] = {-1,1,-1,1};
		ll dy[] = {-1,-1,1,1};
		set<ll> ans;
		rep(d0,4){
			set<tuple<ll,ll,ll> > vis;
			ll D = d0,X = x,Y = y;
			ll pres = -1;
			auto push = [&](int i){
				if(pres != -1 && i != pres)return false;
				pres = i;
				return true;
			};
			while(1){
				// cout << "continue" << endl;
				if((X == 0 || X == a) && (Y == 0 || Y == b))break;
				if(vis.count({X,Y,D}))break;
				vis.insert({X,Y,D});
				// cout<<"X,Y,D = "<<X<<" "<<Y<<" "<<D<<endl;
				if(D == 0){
					ll j = lb(amb_a[X - Y + base],make_pair(X,(ll)-1)) - 1;
					// cout<<"j = "<<j<<endl;
					if(j >= 0){
						if(!push(amb_a[X - Y + base][j].second))break;
						ans.insert(amb_a[X - Y + base][j].second);
						break;
					}
				}
				else if(D == 1){
					ll j = lb(apb_a[X + Y],make_pair(X,(ll)-1)) ;
					if(j < si(apb_a[X + Y])){
						if(!push(apb_a[X + Y][j].second))break;
						if(j + 1 < si(apb_a[X + Y]))break;
					}
				}
				else if(D == 2){
					ll j = lb(apb_a[X + Y],make_pair(X,(ll)-1)) - 1;
					if(j >= 0){
						if(!push(apb_a[X + Y][j].second))break;
						if(j - 1 >= 0)break;
					}
				}
				else if(D == 3){
					ll j = lb(amb_a[X - Y + base],make_pair(X,(ll)-1)) ;
					if(j < si(amb_a[X - Y + base])){
						if(!push(amb_a[X - Y + base][j].second))break;
						if(j + 1 < si(amb_a[X - Y + base]))break;
					}
				}

				int dd = inf;
				if(dx[D] == -1)chmin(dd,X);
				else chmin(dd,a - X);
				if(dy[D] == -1)chmin(dd,Y);
				else chmin(dd,b - Y);

				X += dx[D] * dd, Y += dy[D] * dd;
				if(X == 0 || X == a)D ^= 1;
				if(Y == 0 || Y == b)D ^= 2;
			}
		}
		if(si(ans) == 0)cout<<"No"<<endl;
		else{
			auto it = ans.begin();
			rep(i,si(ans)){
				cout<<(*it) + 1<<" ";
				it++;
			}
			cout<<endl;
		}
	}
}
accuracy: 1
time_ms: 1670
memory_kb: 155872
final_rank: 8

code_snippet: #include <bits/stdc++.h>
// #include <atcoder/all>

using namespace std;
using ll = long long;
using pll = pair<ll, ll>;
using bint = __int128_t;
#define rep(i, s, t) for (ll i = s; i < (ll)(t); i++)
#define rrep(i, s, t) for (ll i = (ll)(t) - 1; i >= (ll)(s); i--)
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
#define TT template <typename T>
TT using vec = vector<T>;
TT using minheap = priority_queue<T, vector<T>, greater<T>>;
TT using maxheap = priority_queue<T>;
template <class T> bool chmin(T& x, T y) {
	return x > y ? (x = y, true) : false;
}
template <class T> bool chmax(T& x, T y) {
	return x < y ? (x = y, true) : false;
}
struct io_setup {
	io_setup() {
		ios::sync_with_stdio(false);
		std::cin.tie(nullptr);
		cout << fixed << setprecision(15);
	}
} io_setup;

int A, B, X, Y, n;
constexpr int shf = 1e6 + 10;
vector<vector<array<int, 2>>> stad(shf * 2), stsb(shf * 2);
array<int, 2> XY;
array<array<char, shf>, 4> wud;
array<array<char, shf>, 4> hlr;
vector<int> dx = {1, -1, 1, -1};
vector<int> dy = {1, 1, -1, -1};
array<int, 2> stxy;

bool get_from(int a, int b, int dd) {
	set<array<int, 3>> st;
	int result = -1;
	while (1) {
		if (st.count({a, b, dd})) {
			result = 0;
			break;
		}
		st.insert({a, b, dd});
		if (dd == 1 || dd == 2) {
			if (!stad[a + b].empty()) {
				if (dd == 1) {
					auto itr = stad[a + b].rbegin();
					if (*itr == XY) {
						result = 1;
						break;
					} else if (*itr == stxy) {
						itr++;
						if (itr != stad[a + b].rend() && *itr == XY) {
							result = 1;
							break;
						}
					} else {
						result = 0;
						break;
					}
				} else {
					auto itr = stad[a + b].begin();
					if (*itr == XY) {
						result = 1;
						break;
					} else if (*itr == stxy) {
						itr++;
						if (itr != stad[a + b].end() && *itr == XY) {
							result = 1;
							break;
						}
					} else {
						result = 0;
						break;
					}
				}
			}
		} else {
			if (!stsb[a - b + shf].empty()) {
				if (dd == 0) {
					auto itr = stsb[a - b + shf].begin();
					if (*itr == XY) {
						result = 1;
						break;
					} else if (*itr == stxy) {
						itr++;
						if (itr != stsb[a - b + shf].end() && *itr == XY) {
							result = 1;
							break;
						}
					} else {
						result = 0;
						break;
					}
				} else {
					auto itr = stsb[a - b + shf].rbegin();
					if (*itr == XY) {
						result = 1;
						break;
					} else if (*itr == stxy) {
						itr++;
						if (itr != stsb[a - b + shf].rend() && *itr == XY) {
							result = 1;
							break;
						}
					} else {
						result = 0;
						break;
					}
				}
			}
		}
		{
			int tmp = 1e9;
			if (dx[dd] > 0) chmin(tmp, A - a);
			else chmin(tmp, a);
			if (dy[dd] > 0) chmin(tmp, B - b);
			else chmin(tmp, b);
			a += tmp * dx[dd];
			b += tmp * dy[dd];
		}
		if (a == 0 || a == A) {
			if (b == 0 || b == B) {
				result = 0;
				break;
			}
			dd ^= 1;
			if (hlr[dd][b] != -1) {
				result = hlr[dd][b];
				break;
			}
		} else {
			assert(b == 0 || b == B);
			if (a == 0 || a == A) {
				result = 0;
				break;
			}
			dd ^= 2;
			if (wud[dd][a] != -1) {
				result = wud[dd][a];
				break;
			}
		}
	}
	for (auto [aa, bb, ddd] : st) {
		if (aa == 0 || aa == A) {
			hlr[ddd][bb] = result;
		} else {
			assert(bb == 0 || bb == B);
			wud[ddd][aa] = result;
		}
	}
	return result;
}

int solve() {
	cin >> A >> B >> X >> Y >> n;
	if (A == 0) return 0;
	XY = {X, Y};
	vector<int> x(n), y(n);
	rep(i, 0, n) cin >> x[i] >> y[i];
	x.push_back(X);
	y.push_back(Y);
	rep(i, 0, 4) rep(j, 0, A + 1) wud[i][j] = -1;
	rep(i, 0, 4) rep(j, 0, B + 1) hlr[i][j] = -1;
	vector<int> ndad, ndsb;
	rep(i, 0, n + 1) {
		stad[x[i] + y[i]].push_back({x[i], y[i]});
		stsb[x[i] - y[i] + shf].push_back({x[i], y[i]});
		ndad.push_back(x[i] + y[i]);
		ndsb.push_back(x[i] - y[i] + shf);
	}
	sort(ndad.begin(), ndad.end());
	sort(ndsb.begin(), ndsb.end());
	ndad.erase(unique(ndad.begin(), ndad.end()), ndad.end());
	ndsb.erase(unique(ndsb.begin(), ndsb.end()), ndsb.end());
	for (int ind : ndad) sort(stad[ind].begin(), stad[ind].end());
	for (int ind : ndsb) sort(stsb[ind].begin(), stsb[ind].end());
	auto get_get_from = [&](int a, int b) -> bool {
		int dd = 0;
		auto itr = upper_bound(stsb[a - b + shf].begin(),
							   stsb[a - b + shf].end(), stxy);
		if (itr != stsb[a - b + shf].end()) {
			if (*itr == XY) return 1;
			return 0;
		}
		{
			int tmp = min(A - a, B - b);
			a += tmp;
			b += tmp;
		}
		if (a == 0 || a == A) {
			if (b == 0 || b == B) return 0;
			dd ^= 1;
			if (hlr[dd][b] != -1) return hlr[dd][b];
			return hlr[dd][b] = get_from(a, b, dd);
		} else {
			assert(b == 0 || b == B);
			if (a == 0 || a == A) return 0;
			dd ^= 2;
			if (wud[dd][a] != -1) return wud[dd][a];
			return wud[dd][a] = get_from(a, b, dd);
		}
	};
	vector<int> ans;
	rep(i, 0, n) {
		stxy = {x[i], y[i]};
		if (get_get_from(x[i], y[i])) ans.push_back(i);
	}
	if (ans.empty()) {
		cout << "No" << endl;
	} else {
		for (int val : ans) cout << val + 1 << " ";
		cout << endl;
	}
	for (int ind : ndad) stad[ind].clear();
	for (int ind : ndsb) stsb[ind].clear();
	return 1;
}

int main() {
	while (solve());
}
accuracy: 1
time_ms: 2340
memory_kb: 244020
final_rank: 14

code_snippet: #include <bits/stdc++.h>
#define fi first
#define se second
#define rep(i,s,n) for (int i = (s); i < (n); ++i)
#define rrep(i,n,g) for (int i = (n)-1; i >= (g); --i)
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define len(x) (int)(x).size()
#define dup(x,y) (((x)+(y)-1)/(y))
#define pb push_back
#define eb emplace_back
#define Field(T) vector<vector<T>>
using namespace std;
using ll = long long;
using ull = unsigned long long;
template<typename T> using pq = priority_queue<T,vector<T>,greater<T>>;
using P = pair<int,int>;
template<class T>bool chmax(T&a,T b){if(a<b){a=b;return 1;}return 0;}
template<class T>bool chmin(T&a,T b){if(b<a){a=b;return 1;}return 0;}

bool solve() {
  int a, b, p, q, n;
  cin >> a >> b >> p >> q >> n;
  if (n == 0) return 1;
  vector<int> x(n), y(n);
  rep(i,0,n) cin >> x[i] >> y[i];
  rep(i,0,n) {
    x.eb(2*a-x[i]), y.eb(y[i]);
    x.eb(x[i]), y.eb(2*b-y[i]);
    x.eb(2*a-x[i]), y.eb(2*b-y[i]);
  }
  x.eb(0), x.eb(0), x.eb(a), x.eb(a);
  y.eb(0), y.eb(b), y.eb(0), y.eb(b);
  map<int,vector<P>> mp;
  rep(i,0,4*n+4) {
    mp[y[i]-x[i]].eb(x[i], i);
  }
  for (auto &[t, v] : mp) {
    sort(all(v));
  }
  function<int(map<int,vector<P>> &,int,int)> get = [&](map<int,vector<P>> &mp, int p, int q) {
    if (mp.count(q-p)) {
      vector<P> &v = mp[q-p];
      if (p > v[0].fi) {
        rrep(i,len(v),0) {
          if (p > v[i].fi) {
            return v[i].se;
          }
        }
      }
    }
    int k = q-p;
    int f = -1;
    rep(i,0,2*(a+b)+2) {
      if (k > 0) k -= 2*a;
      else k += 2*b;
      if (mp.count(k)) {
        vector<P> &v = mp[k];
        int idx = v.back().se;
        if (idx >= 4*n) return -1;
        if (f != -1) {
          if (idx >= n) return -1;
          if (f == idx) return idx;
          else return -1;
        }
        if (idx < n) return idx;
        f = (idx-n)/3;
      }
    }
    return -1;
  };
  vector<P> ps = {{p, q}, {2*a-p, q}, {p, 2*b-q}, {2*a-p, 2*b-q}};
  set<int> st;
  rep(i,0,4) {
    int idx = get(mp, ps[i].fi, ps[i].se);
    st.emplace(idx);
  }
  vector<int> ans;
  for (int e : st) {
    if (e == -1 || e >= n) continue;
    ans.eb(e+1);
  }
  if (ans.empty()) {
    cout << "No" << endl;
    return 0;
  }
  for (int e : ans) {
    cout << e << " ";
  }
  cout << endl;
  return 0;
}

int main() {
  while(!solve());
  return 0;
}
accuracy: 1
time_ms: 1100
memory_kb: 48212
final_rank: 3

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, s, t) for (ll i = s; i < (ll)(t); i++)
#define rrep(i, s, t) for(ll i = (ll)(t) - 1; i >= (ll)(s); i--)
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)

#define TT template<typename T>
TT using vec = vector<T>;
template<class T1, class T2> bool chmin(T1 &x, T2 y) { return x > y ? (x = y, true) : false; }
template<class T1, class T2> bool chmax(T1 &x, T2 y) { return x < y ? (x = y, true) : false; }

struct io_setup {
    io_setup() {
        ios::sync_with_stdio(false);
        std::cin.tie(nullptr);
        cout << fixed << setprecision(15);
    }
} io_setup;

const int dx[]={1,1,-1,-1},dy[]={1,-1,-1,1};

int main() {
    while(1){
        int A,B,CX,CY,N;
        cin>>A>>B>>CX>>CY>>N;
        if(A==0)return 0;
        vector<int>X(N),Y(N);
        vector<set<pair<int,int>>>se_plus(A+B+1),se_minus(A+B+1);
        rep(i,0,N){
            cin>>X[i]>>Y[i];
            se_plus[X[i]+Y[i]].insert({X[i],i});
            se_minus[X[i]-Y[i]+B].insert({X[i],i});
        }
        vector<bool>flag(N);
        rep(t,0,4){
            auto f=[&](int x,int y,int d)-> tuple<int,int,int,vector<int>> {
                int tx,ty,td;
                if(d==0){
                    if(x+(B-y)<A)tx=x+(B-y),ty=B,td=1;
                    else if(x+(B-y)==A)tx=A,ty=B,td=-1;
                    else tx=A,ty=y+(A-x),td=3;
                }else if(d==1){
                    if(x+y<A)tx=x+y,ty=0,td=0;
                    else if(x+y==A)tx=A,ty=0,td=-1;
                    else tx=A,ty=y-(A-x),td=2;
                }else if(d==2){
                    if(x-y>0)tx=x-y,ty=0,td=3;
                    else if(x-y==0)tx=0,ty=0,td=-1;
                    else tx=0,ty=y-x,td=1;
                }else{
                    if(x-(B-y)>0)tx=x-(B-y),ty=B,td=2;
                    else if(x-(B-y)==0)tx=0,ty=B,td=-1;
                    else tx=0,ty=y+x,td=0;
                }
                vector<int>tv;
                if(d==0){
                    auto id=se_minus[x-y+B].lower_bound({x+1,0});
                    while(id!=se_minus[x-y+B].end()&&(*id).first<=tx){
                        tv.push_back((*id).second);
                        id++;
                    }
                }else if(d==1){
                    auto id=se_plus[x+y].lower_bound({x+1,0});
                    while(id!=se_plus[x+y].end()&&(*id).first<=tx){
                        tv.push_back((*id).second);
                        id++;
                    }
                }else if(d==2){
                    auto id=se_minus[x-y+B].lower_bound({x,-1});
                    if(id!=se_minus[x-y+B].begin()){
                        id--;
                        while((*id).first>=tx){
                            tv.push_back((*id).second);
                            if(id==se_minus[x-y+B].begin())break;
                            id--;
                        }
                    }
                }else{
                    auto id=se_plus[x+y].lower_bound({x,-1});
                    if(id!=se_plus[x+y].begin()){
                        id--;
                        while((*id).first>=tx){
                            tv.push_back((*id).second);
                            if(id==se_plus[x+y].begin())break;
                            id--;
                        }
                    }
                }
                return make_tuple(tx,ty,td,tv);
            };
            int nx=CX,ny=CY,nd=t;
            vector<int>nv;
            set<int>se;
            rep(cnt,0,4*(A+B)+5){
                auto[mx,my,md,mv]=f(nx,ny,nd);
                if(nd==2){
                    if(mv.size()){
                        int ax=-1,ai=-1;
                        for(auto i:mv){
                            if(chmax(ax,X[i])){
                                ai=i;
                            }
                        }
                        if(se.empty()||((int)se.size()==1&&*se.begin()==ai)){
                            flag[ai]=true;
                        }
                        break;
                    }
                }
                for(auto x:mv)se.insert(x);
                if(se.size()>1)break;
                swap(nx,mx);
                swap(ny,my);
                swap(nd,md);
                swap(nv,mv);
                if(nd==-1)break;
            }
        }
        vector<int>ans;
        rep(i,0,N){
            if(flag[i])ans.push_back(i+1);
        }
        if(ans.empty())cout<<"No"<<endl;
        else{
            for(auto a:ans)cout<<a<<" ";
            cout<<endl;
        }
    }
}
accuracy: 1
time_ms: 510
memory_kb: 219180
final_rank: 5

code_snippet: #ifdef LOCAL
#define _GLIBCXX_DEBUG
#pragma GCC optimize("O0")
#else
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#endif

#include <bits/stdc++.h>
// #include <bits/extc++.h>
using namespace std;

// #include <atcoder/modint>
// using mint = atcoder::modint998244353;
// istream& operator>>(istream& in, mint& x) {
//     long long a;
//     in >> a;
//     x = a;
//     return in;
// }
// ostream& operator<<(ostream& out, const mint& x) {
//     return out << x.val();
// }

using ll = long long;
using u32 = unsigned int;
using u64 = unsigned long long;
using i128 = __int128;
using u128 = unsigned __int128;
using f128 = __float128;

template <class T>
constexpr T infty = 0;
template <>
constexpr int infty<int> = 1'000'000'000;
template <>
constexpr ll infty<ll> = ll(infty<int>) * infty<int> * 2;
template <>
constexpr u32 infty<u32> = infty<int>;
template <>
constexpr u64 infty<u64> = infty<ll>;
template <>
constexpr i128 infty<i128> = i128(infty<ll>) * infty<ll>;
template <>
constexpr double infty<double> = infty<ll>;
template <>
constexpr long double infty<long double> = infty<ll>;

using pi = pair<int, int>;
using pl = pair<ll, ll>;
using vi = vector<int>;
using vl = vector<ll>;
template <class T>
using vc = vector<T>;
template <class T>
using vvc = vector<vc<T>>;
using vvi = vvc<int>;
using vvl = vvc<ll>;
template <class T>
using vvvc = vector<vvc<T>>;
template <class T>
using vvvvc = vector<vvvc<T>>;
template <class T>
using vvvvvc = vector<vvvvc<T>>;
template <class T>
using pqg = std::priority_queue<T, vector<T>, greater<T>>;
template <class T, class U>
using umap = unordered_map<T, U>;

// template <typename K>
// using tree = __gnu_pbds::tree<K, __gnu_pbds::null_type, std::less<>,
//                               __gnu_pbds::rb_tree_tag,
//                               __gnu_pbds::tree_order_statistics_node_update>;

#define vv(type, name, h, ...) \
    vector<vector<type>> name(h, vector<type>(__VA_ARGS__))
#define vvv(type, name, h, w, ...)     \
    vector<vector<vector<type>>> name( \
        h, vector<vector<type>>(w, vector<type>(__VA_ARGS__)))
#define vvvv(type, name, a, b, c, ...)         \
    vector<vector<vector<vector<type>>>> name( \
        a, vector<vector<vector<type>>>(       \
               b, vector<vector<type>>(c, vector<type>(__VA_ARGS__))))

// FOR(a) :=  for (ll _ = 0; _ < (ll)a; ++_)
// FOR(i, a) := for (ll i = 0; i < (ll)a; ++i)
// FOR(i, a, b) := for (ll i = a; i < (ll)b; ++i)
// FOR(i, a, b, c) := for (ll i = a; i < (ll)b; i += (c))
// FOR_R(a) := for (ll i = (a) - 1; i >= 0; --i)
// FOR_R(i, a) := for (ll i = (a) - 1; i >= 0; --i)
// FOR_R(i, a, b) := for (ll i = (b) - 1; i >= (ll)a; --i)
#define FOR1(a) for (ll _ = 0; _ < (ll)a; ++_)
#define FOR2(i, a) for (ll i = 0; i < (ll)a; ++i)
#define FOR3(i, a, b) for (ll i = a; i < (ll)b; ++i)
#define FOR4(i, a, b, c) for (ll i = a; i < (ll)b; i += (c))
#define FOR1_R(a) for (ll i = (a) - 1; i >= 0; --i)
#define FOR2_R(i, a) for (ll i = (a) - 1; i >= 0; --i)
#define FOR3_R(i, a, b) for (ll i = (b) - 1; i >= (ll)a; --i)
#define overload4(a, b, c, d, e, ...) e
#define overload3(a, b, c, d, ...) d
#define FOR(...) overload4(__VA_ARGS__, FOR4, FOR3, FOR2, FOR1)(__VA_ARGS__)
#define FOR_R(...) overload3(__VA_ARGS__, FOR3_R, FOR2_R, FOR1_R)(__VA_ARGS__)

#define FOR_subset(t, s) \
    for (int t = (s); t >= 0; t = (t == 0 ? -1 : (t - 1) & (s)))
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()

int popcnt(int x) { return __builtin_popcount(x); }
int popcnt(u32 x) { return __builtin_popcount(x); }
int popcnt(ll x) { return __builtin_popcountll(x); }
int popcnt(u64 x) { return __builtin_popcountll(x); }
int popcnt_mod_2(int x) { return __builtin_parity(x); }
int popcnt_mod_2(u32 x) { return __builtin_parity(x); }
int popcnt_mod_2(ll x) { return __builtin_parityll(x); }
int popcnt_mod_2(u64 x) { return __builtin_parityll(x); }
// (0, 1, 2, 3, 4) -> (-1, 0, 1, 1, 2)
int topbit(int x) { return (x == 0 ? -1 : 31 - __builtin_clz(x)); }
int topbit(u32 x) { return (x == 0 ? -1 : 31 - __builtin_clz(x)); }
int topbit(ll x) { return (x == 0 ? -1 : 63 - __builtin_clzll(x)); }
int topbit(u64 x) { return (x == 0 ? -1 : 63 - __builtin_clzll(x)); }
// (0, 1, 2, 3, 4) -> (-1, 0, 1, 0, 2)
int lowbit(int x) { return (x == 0 ? -1 : __builtin_ctz(x)); }
int lowbit(u32 x) { return (x == 0 ? -1 : __builtin_ctz(x)); }
int lowbit(ll x) { return (x == 0 ? -1 : __builtin_ctzll(x)); }
int lowbit(u64 x) { return (x == 0 ? -1 : __builtin_ctzll(x)); }

template <typename T>
T floor(T a, T b) {
    return a / b - (a % b && (a ^ b) < 0);
}
template <typename T>
T ceil(T x, T y) {
    return floor(x + y - 1, y);
}
template <typename T>
T bmod(T x, T y) {
    return x - y * floor(x, y);
}
template <typename T>
pair<T, T> divmod(T x, T y) {
    T q = floor(x, y);
    return {q, x - q * y};
}

template <typename T, typename U>
T POW(U x_, int n) {
    T x = x_;
    T ret = 1;
    while (n > 0) {
        if (n & 1) ret *= x;
        x *= x;
        n >>= 1;
    }
    return ret;
}

template <typename T, typename U>
T SUM(const vector<U> &A) {
    T sm = 0;
    for (auto &&a : A) sm += a;
    return sm;
}

#define LB(c, x) distance((c).begin(), lower_bound(all(c), (x)))
#define UB(c, x) distance((c).begin(), upper_bound(all(c), (x)))
#define UNIQUE(x) \
    sort(all(x)), x.erase(unique(all(x)), x.end()), x.shrink_to_fit()

template <class T, class S>
inline bool chmax(T &a, const S &b) {
    return (a < b ? a = b, 1 : 0);
}
template <class T, class S>
inline bool chmin(T &a, const S &b) {
    return (a > b ? a = b, 1 : 0);
}

// ? は -1
vc<int> s_to_vi(const string &S, char first_char) {
    vc<int> A(S.size());
    FOR(i, S.size()) { A[i] = (S[i] != '?' ? S[i] - first_char : -1); }
    return A;
}

template <typename T, typename U>
vector<T> cumsum(vector<U> &A, int off = 1) {
    int N = A.size();
    vector<T> B(N + 1);
    FOR(i, N) { B[i + 1] = B[i] + A[i]; }
    if (off == 0) B.erase(B.begin());
    return B;
}

template <typename T>
vector<int> argsort(const vector<T> &A) {
    vector<int> ids(A.size());
    iota(all(ids), 0);
    sort(all(ids),
         [&](int i, int j) { return (A[i] == A[j] ? i < j : A[i] < A[j]); });
    return ids;
}

// A[I[0]], A[I[1]], ...
template <typename T>
vc<T> rearrange(const vc<T> &A, const vc<int> &I) {
    vc<T> B(I.size());
    FOR(i, I.size()) B[i] = A[I[i]];
    return B;
}

template <class... T>
constexpr auto min(T... a) {
    return min(initializer_list<common_type_t<T...>>{a...});
}
template <class... T>
constexpr auto max(T... a) {
    return max(initializer_list<common_type_t<T...>>{a...});
}

void print() { cout << '\n'; }
template <class T>
void print(const T &a) {
    cout << a << '\n';
}
template <class T, class... Ts>
void print(const T &a, const Ts &...b) {
    cout << a;
    (cout << ... << (cout << ' ', b));
    cout << '\n';
}
template <class T>
void print(vector<T> &a) {
    for (int i = 0; i < (int)a.size(); ++i) {
        cout << a[i] << " \n"[i == (int)a.size() - 1];
    }
}
template <class T>
void print(vector<T> &&a) {
    for (int i = 0; i < (int)a.size(); ++i) {
        cout << a[i] << " \n"[i == (int)a.size() - 1];
    }
}
template <class T>
void print(vector<vector<T>> &a) {
    for (int i = 0; i < (int)a.size(); ++i) {
        for (int j = 0; j < (int)a[i].size(); ++j) {
            cout << a[i][j] << " \n"[j == (int)a[i].size() - 1];
        }
    }
}
template <class T>
void print(vector<vector<T>> &&a) {
    for (int i = 0; i < (int)a.size(); ++i) {
        for (int j = 0; j < (int)a[i].size(); ++j) {
            cout << a[i][j] << " \n"[j == (int)a[i].size() - 1];
        }
    }
}
void YESNO(bool b) { cout << (b ? "YES" : "NO") << endl; }
void YesNo(bool b) { cout << (b ? "Yes" : "No") << endl; }

// #ifdef LOCAL
// https://zenn.dev/sassan/articles/19db660e4da0a4
// #include "/export/home/ICPC/ICPC05/workspace/cpp-dump/dump.hpp"
// #define dump(...) cpp_dump(__VA_ARGS__)
// // CPP_DUMP_DEFINE_EXPORT_OBJECT(mint, val());
// #else
#define dump(...)
#define CPP_DUMP_SET_OPTION(...)
#define CPP_DUMP_DEFINE_EXPORT_OBJECT(...)
#define CPP_DUMP_DEFINE_EXPORT_ENUM(...)
#define CPP_DUMP_DEFINE_DANGEROUS_EXPORT_OBJECT(...)
// #endif

#include <atcoder/math>

//----------------------------------------------------------------
void solve() {
    ll A, B, X, Y, N;
    cin >> A >> B >> X >> Y >> N;
    if (A == 0 && B == 0 && X == 0 && Y == 0 && N == 0) {
        exit(0);
    }
    vl xs(N), ys(N);
    FOR(i, N) { cin >> xs[i] >> ys[i]; }
    vc<pl> ans(4, {infty<ll>, -1});
    FOR(l, 4) {
        FOR(i, N) {
            ll x = xs[i];
            ll y = ys[i];
            ll xx, yy;
            switch (l) {
                case 0: {
                    xx = x;
                    yy = y;
                    break;
                }
                case 1: {
                    xx = A + A - x;
                    yy = y;
                    break;
                }
                case 2: {
                    xx = A + A - x;
                    yy = B + B - y;
                    break;
                }
                case 3: {
                    xx = x;
                    yy = B + B - y;
                    break;
                }
            }
            ll corner = infty<ll>;
            {
                vl r = {A, B};
                vl m = {A - xx, B - yy};
                pl c = atcoder::crt(m, r);
                if (c != pl(0, 0)) corner = c.first;
            }
            FOR(k, 4) {
                vl r = {A + A, B + B};
                vl m;
                if (k == 0) {
                    m = {X - xx, Y - yy};
                } else if (k == 1) {
                    m = {A + A - X - xx, Y - yy};
                } else if (k == 2) {
                    m = {A + A - X - xx, B + B - Y - yy};
                } else {
                    m = {X - xx, B + B - Y - yy};
                }
                pl c = atcoder::crt(m, r);
                if (c == pl(0, 0)) continue;
                ll coin = c.first;
                if (corner < coin) continue;
                if (coin < ans[k].first) {
                    if (l == 0) {
                        ans[k] = {coin, i};
                    } else {
                        if (ans[k].second != i) {
                            ans[k] = {coin, -1};
                        }
                    }
                }
            }
        }
    }
    vl res;
    for (auto p : ans) {
        if (p.second != -1) {
            res.emplace_back(p.second + 1);
        }
    }
    if (res.size() == 0) {
        print("No");
    } else {
        UNIQUE(res);
        sort(all(res));
        FOR(i, res.size()) {
            cout << res[i] << " ";
        }
        cout << endl;
    }
}

int main() {
    while (true) {
        solve();
    }
    return 0;
}
accuracy: 1
time_ms: 560
memory_kb: 4948
final_rank: 1

code_snippet: #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> pll;
#define vl vector<ll>
#define vll vector<ll>
#define rep(i,n) for(int i = 0;i < n;i++)
#define per(i,n) for(int i = n - 1;i >= 0;i--)
#define fore(e,v) for(auto &&e,v)
#define si(a) (int)(size(a))
#define all(a) begin(a),end(a)
#define lb(v,x) (lower_bound(all(v),x) - begin(v))
#define eb emplace_back

template<typename T , typename S> bool chmin(T &a, const S& b){
	return a > b ? a = b, 1:0;
}
template<typename T , typename S> bool chmax(T &a, const S& b){
	return a < b ? a = b, 1:0;
}

const ll inf = 3e18;

struct _{
	_(){
		cin.tie(0) -> sync_with_stdio(0),cout.tie(0);
	}
}__;

int main(){
	while(1){
		ll a,b,x,y,n;
		cin>>a>>b>>x>>y>>n;
		if(a + b + x + y + n == 0)break;
		ll base = 1e6 + 10;
		vector<pll>apb_a[2 * base], amb_a[2 * base];
		rep(i,n){
			ll x,y;
			cin>>x>>y;

			apb_a[x + y].eb(x,i);
			amb_a[base + x - y].eb(x,i);
		}
		rep(i,2 * base){
			sort(all(apb_a[i]));
			sort(all(amb_a[i]));
		}
		ll dx[] = {-1,1,-1,1};
		ll dy[] = {-1,-1,1,1};
		set<ll> ans;
		rep(d0,4){
			set<tuple<ll,ll,ll> > vis;
			ll D = d0,X = x,Y = y;
			ll pres = -1;
			auto push = [&](ll i){
				if(pres != -1 && i != pres)return false;
				pres = i;
				return true;
			};
			while(1){
				if((X == 0 || X == a) && (Y == 0 || Y == b))break;
				if(vis.count({X,Y,D}))break;
				vis.insert({X,Y,D});
				// cout<<"X,Y,D = "<<X<<" "<<Y<<" "<<D<<endl;
				if(D == 0){
					ll j = lb(amb_a[X - Y + base],pair(X,(ll)-1)) - 1;
					// cout<<"j = "<<j<<endl;
					if(j >= 0){
						if(!push(amb_a[X - Y + base][j].second))break;
						ans.insert(amb_a[X - Y + base][j].second);
						break;
					}
				}
				else if(D == 1){
					ll j = lb(apb_a[X + Y],pair(X,(ll)-1)) ;
					if(j < si(apb_a[X + Y])){
						if(!push(apb_a[X + Y][j].second))break;
						if(j + 1 < si(apb_a[X + Y]))break;
					}
				}
				else if(D == 2){
					ll j = lb(apb_a[X + Y],pair(X,(ll)-1)) - 1;
					if(j >= 0){
						if(!push(apb_a[X + Y][j].second))break;
						if(j - 1 >= 0)break;
					}
				}
				else if(D == 3){
					ll j = lb(amb_a[X - Y + base],pair(X,(ll)-1)) ;
					if(j < si(amb_a[X - Y + base])){
						if(!push(amb_a[X - Y + base][j].second))break;
						if(j + 1 < si(amb_a[X - Y + base]))break;
					}
				}

				ll dd = inf;
				if(dx[D] == -1)chmin(dd,X);
				else chmin(dd,a - X);
				if(dy[D] == -1)chmin(dd,Y);
				else chmin(dd,b - Y);

				X += dx[D] * dd, Y += dy[D] * dd;
				if(X == 0 || X == a)D ^= 1;
				if(Y == 0 || Y == b)D ^= 2;
			}
		}
		if(si(ans) == 0)cout<<"No"<<endl;
		else{
			auto it = ans.begin();
			rep(i,si(ans)){
				cout<<(*it) + 1<<" ";
				it++;
			}
			cout<<endl;
		}
	}
}
accuracy: 1
time_ms: 1790
memory_kb: 156124
final_rank: 9

code_snippet: #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> pll;
#define vl vector<ll>
#define vll vector<ll>
#define rep(i,n) for(int i = 0;i < n;i++)
#define per(i,n) for(int i = n - 1;i >= 0;i--)
#define fore(e,v) for(auto &&e,v)
#define si(a) (int)(size(a))
#define all(a) begin(a),end(a)
#define lb(v,x) (lower_bound(all(v),x) - begin(v))
#define eb emplace_back

template<typename T , typename S> bool chmin(T &a, const S& b){
	return a > b ? a = b, 1:0;
}
template<typename T , typename S> bool chmax(T &a, const S& b){
	return a < b ? a = b, 1:0;
}

const ll inf = 3e18;

// struct _{
// 	_(){
// 		cin.tie(0) -> sync_with_stdio(0),cout.tie(0);
// 	}
// }__;

int main(){
	ll a,b,x,y,n;
	while(cin>>a>>b>>x>>y>>n){
		if(a + b + x + y + n == 0)break;
		ll base = 1e6 + 10;
		vector<pll>apb_a[2 * base], amb_a[2 * base];
		rep(i,n){
			ll x,y;
			cin>>x>>y;

			apb_a[x + y].eb(x,i);
			amb_a[base + x - y].eb(x,i);
		}
		rep(i,2 * base){
			sort(all(apb_a[i]));
			sort(all(amb_a[i]));
		}
		ll dx[] = {-1,1,-1,1};
		ll dy[] = {-1,-1,1,1};
		set<ll> ans;
		rep(d0,4){
			set<tuple<ll,ll,ll> > vis;
			ll D = d0,X = x,Y = y;
			ll pres = -1;
			auto push = [&](ll i){
				if(pres != -1 && i != pres)return false;
				pres = i;
				return true;
			};
			while(1){
				if((X == 0 || X == a) && (Y == 0 || Y == b))break;
				if(vis.count({X,Y,D}))break;
				vis.insert({X,Y,D});
				// cout<<"X,Y,D = "<<X<<" "<<Y<<" "<<D<<endl;
				if(D == 0){
					ll j = lb(amb_a[X - Y + base],pair(X,(ll)-1)) - 1;
					// cout<<"j = "<<j<<endl;
					if(j >= 0){
						if(!push(amb_a[X - Y + base][j].second))break;
						ans.insert(amb_a[X - Y + base][j].second);
						break;
					}
				}
				else if(D == 1){
					ll j = lb(apb_a[X + Y],pair(X,(ll)-1)) ;
					if(j < si(apb_a[X + Y])){
						if(!push(apb_a[X + Y][j].second))break;
						if(j + 1 < si(apb_a[X + Y]))break;
					}
				}
				else if(D == 2){
					ll j = lb(apb_a[X + Y],pair(X,(ll)-1)) - 1;
					if(j >= 0){
						if(!push(apb_a[X + Y][j].second))break;
						if(j - 1 >= 0)break;
					}
				}
				else if(D == 3){
					ll j = lb(amb_a[X - Y + base],pair(X,(ll)-1)) ;
					if(j < si(amb_a[X - Y + base])){
						if(!push(amb_a[X - Y + base][j].second))break;
						if(j + 1 < si(amb_a[X - Y + base]))break;
					}
				}

				ll dd = inf;
				if(dx[D] == -1)chmin(dd,X);
				else chmin(dd,a - X);
				if(dy[D] == -1)chmin(dd,Y);
				else chmin(dd,b - Y);

				X += dx[D] * dd, Y += dy[D] * dd;
				if(X == 0 || X == a)D ^= 1;
				if(Y == 0 || Y == b)D ^= 2;
			}
		}
		if(si(ans) == 0)cout<<"No"<<endl;
		else{
			ll m = si(ans);
			auto it = ans.begin();
			rep(i,m){
				cout<<(*it) + 1<<" ";
				// if(i == si(ans) - 1)cout<<endl;
				// else cout<<" ";
				it++;
			}
			cout<<endl;
		}
	}
	return 0;
}
accuracy: 1
time_ms: 1830
memory_kb: 155828
final_rank: 10

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0; i<(int)n; i++)
#define repi(i,m,n) for(int i=(int)m; i<=(int)n; i++)
#define rrep(i,n) for(int i=(int)n-1; i>=0; i--)
#define rrepi(i,m,n) for(int i=(int)n; i>=(int)m; i--)
#define all(x) x.begin(),x.end()
template <typename T> inline bool chmin(T& a, const T& b) { return a > b ? a = b, true : false; };
template <typename T> inline bool chmax(T& a, const T& b) { return a < b ? a = b, true : false; };
template <typename T> void uniq(std::vector<T> &v){
    std::sort(v.begin(),v.end());
    v.erase(unique(v.begin(),v.end()),v.end());
}
using ll = long long;

bool solve(){
    int a,b,x,y,n; cin >> a >> b >> x >> y >> n;
    if(a + b + x + y + n == 0) return false;
    vector<set<pair<int,int>>> s(2*a + 2*b + 1);
    
    vector<int> c(n+5),d(n+5);
    rep(i,n) cin >> c[i] >> d[i];
    c[n] = x; d[n] = y;
    c[n+1] = 0; d[n+1] = 0;
    c[n+2] = a; d[n+2] = 0;
    c[n+3] = 0; d[n+3] = b;
    c[n+4] = a; d[n+4] = b;

    rep(i,n+5){
        s[2*a - c[i] + d[i]].emplace(c[i] + d[i], i);
        s[2*a - (2*a - c[i]) + d[i]].emplace((2*a - c[i]) + d[i], i);
        s[2*a - c[i] + (2*b - d[i])].emplace(c[i] + (2*b - d[i]), i);
        s[2*a - (2*a - c[i]) + (2*b - d[i])].emplace((2*a - c[i]) + (2*b - d[i]), i);
    }

    vector<int> ans;
    rep(i,n){
        x = c[i]; y = d[i];
        s[2*a - (2*a - x) + y].erase(make_pair((2*a - x) + y, i));
        s[2*a - x + (2*b - y)].erase(make_pair(x + (2*b - y), i));
        s[2*a - (2*a - x) + (2*b - y)].erase(make_pair((2*a - x) + (2*b - y), i));

        while(1){
            int idx = 2*a - x + y, dist = x + y;
            auto p = s[idx].lower_bound(make_pair(dist, n));
            if(p == s[idx].end()){
                if(2*a - x < 2*b - y){
                    y = (y + 2*a - x)%(2*b);
                    x = 0;
                }
                else{
                    x = (x + 2*b - y)%(2*a);
                    y = 0;
                }
            }                
            else{
                if((*p).second == n){
                    ans.emplace_back(i+1);
                }
                break;
            }
        }

        x = c[i];y = d[i];

        s[2*a - (2*a - x) + y].emplace((2*a - x) + y, i);
        s[2*a - x + (2*b - y)].emplace(x + (2*b - y), i);
        s[2*a - (2*a - x) + (2*b - y)].emplace((2*a - x) + (2*b - y), i);
    }
            
    if(ans.size() == 0) cout << "No" << endl;
    else{
        uniq(ans);
        for(auto e : ans) cout << e << " ";
        cout << endl;
    }
    
    return true;
}
int main(){
    while(solve());
    
    return 0;
}
accuracy: 1
time_ms: 800
memory_kb: 210032
final_rank: 6

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0; i<(int)n; i++)
#define repi(i,m,n) for(int i=(int)m; i<=(int)n; i++)
#define rrep(i,n) for(int i=(int)n-1; i>=0; i--)
#define rrepi(i,m,n) for(int i=(int)n; i>=(int)m; i--)
#define all(x) x.begin(),x.end()
template <typename T> inline bool chmin(T& a, const T& b) { return a > b ? a = b, true : false; };
template <typename T> inline bool chmax(T& a, const T& b) { return a < b ? a = b, true : false; };
template <typename T> void uniq(std::vector<T> &v){
    std::sort(v.begin(),v.end());
    v.erase(unique(v.begin(),v.end()),v.end());
}
using ll = long long;

bool solve(){
    int a,b,x,y,n; cin >> a >> b >> x >> y >> n;
    if(a + b + x + y + n == 0) return false;
    int t = 2*a + 2*b - 1;
    vector<set<pair<int,int>>> s(t);
    
    vector<int> c(n+5),d(n+5);
    rep(i,n) cin >> c[i] >> d[i];
    c[n] = x; d[n] = y;
    c[n+1] = 0; d[n+1] = 0;
    c[n+2] = a; d[n+2] = 0;
    c[n+3] = 0; d[n+3] = b;
    c[n+4] = a; d[n+4] = b;

    rep(i,n+5){
        s[(-c[i] + d[i] + t)%t].emplace(c[i] + d[i], i);
        s[(t-(2*a - c[i]) + d[i])%t].emplace((2*a - c[i]) + d[i], i);
        s[(t - c[i] + (2*b - d[i]))%t].emplace(c[i] + (2*b - d[i]), i);
        s[(t - (2*a - c[i]) + (2*b - d[i]))%t].emplace((2*a - c[i]) + (2*b - d[i]), i);
    }

    vector<int> ans;
    rep(i,n){
        x = c[i]; y = d[i];
        s[(t - (2*a - x) + y)%t].erase(make_pair((2*a - x) + y, i));
        s[(t - x + (2*b - y))%t].erase(make_pair(x + (2*b - y), i));
        s[(t - (2*a - x) + (2*b - y))%t].erase(make_pair((2*a - x) + (2*b - y), i));

        while(1){
            int idx = (t - x + y)%t, dist = x + y;
            auto p = s[idx].upper_bound(make_pair(dist, n));
            if(p == s[idx].end()){
                if(2*a - x < 2*b - y){
                    y = (y + 2*a - x)%(2*b);
                    x = 0;
                }
                else{
                    x = (x + 2*b - y)%(2*a);
                    y = 0;
                }
            }                
            else{
                if((*p).second == n){
                    ans.emplace_back(i+1);
                }
                break;
            }
        }

        x = c[i];y = d[i];

        s[(t - (2*a - x) + y)%t].emplace((2*a - x) + y, i);
        s[(t - x + (2*b - y))%t].emplace(x + (2*b - y), i);
        s[(t - (2*a - x) + (2*b - y))%t].emplace((2*a - x) + (2*b - y), i);
    }
            
    if(ans.size() == 0) cout << "No" << endl;
    else{
        uniq(ans);
        for(auto e : ans) cout << e << " ";
        cout << endl;
    }
    
    return true;
}
int main(){
    while(solve());
    
    return 0;
}
accuracy: 1
time_ms: 810
memory_kb: 210216
final_rank: 7

code_snippet: #include<bits/stdc++.h>
using namespace std;

void solve(int a,int b,int cx,int cy,int n){
    vector<int> X(n),Y(n);
    int t = 2*(a+b)-1;
    vector<set<int>> s(t);
    map<pair<int,int>,int> mp;
    for (int i = 0; i < n; i++){
        int x,y,nx,ny;cin >> x >> y;

        X[i] = x;Y[i] = y;

        s[(x-y+t)%t].insert(x);
        mp[make_pair(x,y)] = i+1;

        nx = 2*a - x;
        s[(nx-y+t)%t].insert(nx);
        mp[make_pair(nx,y)] = i+1;

        ny = 2*b - y;
        s[(x-ny+t)%t].insert(x);
        mp[make_pair(x,ny)] = i+1;

        s[(nx-ny+t)%t].insert(nx);
        mp[make_pair(nx,ny)] = i+1;
    }

    s[(a-b+t)%t].insert(a);
    mp[make_pair(a,b)] = -1;
    s[(2*a-2*b+t)%t].insert(2*a);
    mp[make_pair(2*a,2*b)] = -1;
    s[(a-2*b+t)%t].insert(a);
    mp[make_pair(a,2*b)] = -1;
    s[(2*a-b+t)%t].insert(2*a);
    mp[make_pair(2*a,b)] = -1;

    s[(cx-cy+t)%t].insert(cx);
    mp[make_pair(cx,cy)] = 0;

    int nx = 2*a-cx,ny = 2*b-cy;
    s[(nx-cy+t)%t].insert(nx);
    mp[make_pair(nx,cy)] = 0;

    s[(cx-ny+t)%t].insert(cx);
    mp[make_pair(cx,ny)] = 0;

    s[(nx-ny+t)%t].insert(nx);
    mp[make_pair(nx,ny)] = 0;

    vector<int> ans;

    for (int i = 0; i < n; i++){
        int now = i+1;
        int x=X[i],y=Y[i];
        int idx = (x-y+t)%t;

        while(1){
            auto it = s[idx].upper_bound(x);
            if (it == s[idx].end()){
                if (2*a-x > 2*b-y){
                    x += 2*b-y;
                    y = 0;
                }
                else {
                    y += 2*a-x;
                    x = 0;
                }
                idx = (x-y+t)%t;
                continue;
            }
            else {
                nx = *it;
                ny = (nx - idx + t)%t;

                int tmp = mp[make_pair(nx,ny)];
                if (tmp == now){
                    if (nx < a && ny < b)break;
                    else {
                        x = nx;
                        y = ny;
                        continue;
                    }
                }

                if (tmp == 0){
                    ans.push_back(now);
                    break;
                }
                break;
            }
        }
    }
    sort(ans.begin(),ans.end());
    if (!ans.size())cout << "No";
    for (int i = 0; i < ans.size(); i++)cout << ans[i] << ' ';
    cout << endl;
    return;
}

int main(){
    while(1){
        int a,b,x,y,n;
        cin >> a >> b >> x >> y >> n;
        if (a==0)return 0;
        solve(a,b,x,y,n);
    }
    return 0;
}
accuracy: 1
time_ms: 1450
memory_kb: 235124
final_rank: 11

code_snippet: #pragma GCC optimize("O3")
#include<bits/stdc++.h>
//#include<boost/multiprecision/cpp_int.hpp>
using namespace std;
#define ll long long
#define rep(i,n) for (long long i=0;i<(ll)n;i++)
#define loop(i,m,n) for(long long i=m;i<=(ll)n;i++)
//#define bbi boost::multiprecision::cpp_int
#define vl vector<long long>
#define vvl vector<vector<long long>>
#define vdbg(a) rep(ii,a.size()){cout<<a[ii]<<" ";}cout<<endl;
#define vvdbg(a) rep(ii,a.size()){rep(jj,a[ii].size()){cout<<a[ii][jj]<<" ";}cout<<endl;}
#define setdbg(a) for(const auto & ii:a){cout<<ii<<" ";}cout<<endl;
#define inf 4000000000000000000LL
#define mod 998244353LL
//#define mod 1000000007LL


//グリッド問題等用
vl fdx={1,1,-1,-1};
vl fdy={1,-1,1,-1};

//乱数、ファイル入出力
random_device rnd;// 非決定的な乱数生成器
mt19937 mt(rnd());// メルセンヌ・ツイスタの32ビット版、引数は初期シード

ll solve(){
	ll a,b,x,y,n;
	cin>>a>>b>>x>>y>>n;
	if(a==0)return 1;
	//rupはy切片、x座標に対してyがどれだけ大きいか。
	unordered_map<ll,set<pair<ll,ll>>> rup;
	//lupは、x+yが0,0よりどれだけ大きいか。
	unordered_map<ll,set<pair<ll,ll>>> lup;

	set<ll> ans;

	rep(i,n){
		ll c,d;
		cin>>c>>d;
		rup[d-c].insert({c,i+1});
		lup[c+d].insert({c,i+1});
	}

	rep(i,4){
		ll dx=fdx[i];
		ll dy=fdy[i];
		ll coinx=x,coiny=y;
		set<ll> tmp;
		rep(j,a+b+1){
			
			if(dx==dy){
				if(rup[coiny-coinx].size()!=0){
					if(dx==-1){
						auto it=rup[coiny-coinx].rbegin();
						while(it!=rup[coiny-coinx].rend()&&it->first>coinx)it++;
						if(it!=rup[coiny-coinx].rend()){
							tmp.insert(it->second);
							if(tmp.size()==1){
								ans.insert(it->second);
								break;
							}
						}
					}else{
						auto it=rup[coiny-coinx].begin();
						while(it!=rup[coiny-coinx].end()&&it->first<coinx)it++;
						if(it!=rup[coiny-coinx].end()){
							tmp.insert(it->second);
						}
					}
				}
			}else{
				if(lup[coiny+coinx].size()!=0){
					if(dx==-1){
						auto it=lup[coiny+coinx].rbegin();
						while(it!=lup[coiny+coinx].rend()&&it->first>coinx)it++;
						if(it!=lup[coiny+coinx].rend()){
							tmp.insert(it->second);
						}
					}else{
						auto it=lup[coiny+coinx].begin();
						while(it!=lup[coiny+coinx].end()&&it->first<coinx)it++;
						if(it!=lup[coiny+coinx].end()){
							tmp.insert(it->second);
						}
					}
				}
			}
			//壁にぶつかるまでの移動距離を求める。
			ll idoux,idouy;
			if(dx==-1)idoux=coinx;
			else idoux=a-coinx;
			if(dy==-1)idouy=coiny;
			else idouy=b-coiny;
	
			coinx+=dx*min(idoux,idouy);
			coiny+=dy*min(idoux,idouy);
	
			if(idoux==idouy)break;
			else if(idoux<idouy)dx*=-1;
			else dy*=-1;

			if(tmp.size()==2)break;
		}
	}
	if(ans.size()==0){
		cout<<"No"<<endl;
	}else{
		setdbg(ans);
	}
	return 0;
}

//メイン
int main(){
	while(!solve());
	return 0;
}
accuracy: 1
time_ms: 780
memory_kb: 122388
final_rank: 4

code_snippet: #include <bits/stdc++.h>
using namespace std;

using i64 = long long;

array<i64, 3> exgcd(i64 a, i64 b) {
	if (b == 0) { return {a, 1, 0}; }
	auto [g, x, y] = exgcd(b, a % b);
	return {g, y, x - a / b * y};
}

bool crt(i64 &m1, i64 &r1, i64 m2, i64 r2) {
    if (m2 > m1) swap(m1, m2), swap(r1, r2);
    auto [g, a, b] = exgcd(m1, m2);
    if ((r2 - r1) % g != 0) return false;
    m2 /= g; i64 D = (r2 - r1) / g % m2 * a % m2;
    r1 += (D < 0 ? D + m2 : D) * m1; m1 *= m2;
    assert(r1 >= 0 && r1 < m1);
    return true;
}

mt19937 rng(58);

int main() {
    cin.tie(nullptr)->sync_with_stdio(false);

    i64 a, b, X, Y, n;

    auto solve = [&]() {
        vector<pair<i64, i64>> balls(n);
        // set<pair<i64, i64>> vis{{X, Y}};
        for (auto &[x, y] : balls) {
            cin >> x >> y;
            // x = rng() % (a - 1) + 1;
            // y = rng() % (b - 1) + 1;
            // while (vis.count({x, y})) {
            //     x = rng() % (a - 1) + 1;
            //     y = rng() % (b - 1) + 1;
            // }
            // vis.emplace(x, y);
        }

        vector<int> ans;

        for (int px : {1, -1}) {
            for (int py : {1, -1}) {
                i64 sx = px == 1 ? X : a + a - X;
                i64 sy = py == 1 ? Y : b + b - Y;
                i64 rest = -1;
                int resi = -1;
                for (int i = 0; i < n; i++) {
                    i64 m1 = 2 * a;
                    i64 m2 = 2 * b;   
                    i64 r1 = sx - balls[i].first;
                    r1 = (r1 % m1 + m1) % m1;
                    i64 r2 = sy - balls[i].second;
                    r2 = (r2 % m2 + m2) % m2;
                    if (!crt(m1, r1, m2, r2)) {
                        continue;
                    }
                    i64 t = r1;
                    if (rest == -1 || t < rest) {
                        rest = t;
                        resi = i;
                    }
                }
                if (resi != -1) {
                    vector<pair<i64, i64>> holes{{0, 0}, {a, 0}, {0, b}, {a, b}};
                    for (int i = 0; i < n; i++) {
                        if (i == resi) {
                            continue;
                        }
                        auto [x, y] = balls[i];
                        holes.emplace_back(x, y);
                        holes.emplace_back(a + a - x, y);
                        holes.emplace_back(a + a - x, b + b - y);
                        holes.emplace_back(x, b + b - y);
                    }
                    for (auto [x, y] : holes) {
                        i64 m1 = 2 * a;
                        i64 m2 = 2 * b;   
                        i64 r1 = sx - x;
                        r1 = (r1 % m1 + m1) % m1;
                        i64 r2 = sy - y;
                        r2 = (r2 % m2 + m2) % m2;
                        if (!crt(m1, r1, m2, r2)) {
                            continue;
                        }
                        i64 t = r1;
                        if (t < rest) {
                            resi = -1;
                            break;
                        }
                    }
                }
                if (resi != -1) {
                    ans.push_back(resi);
                }
            }
        }

        if (ans.empty()) {
            cout << "No\n";
        } else {
            sort(ans.begin(), ans.end());
            ans.erase(unique(ans.begin(), ans.end()), ans.end());
            for (int i = 0; i < ans.size(); i++) {
                cout << ans[i] + 1 << ' ';
            }
            cout << '\n';
        }
    };

    // while (true) {
    //     a = rng() % 20 + 5;
    //     b = rng() % 20 + 5;
    //     X = rng() % (a - 1) + 1;
    //     Y = rng() % (b - 1) + 1;
    //     n = rng() % 10 + 1;
    //     solve();
    // }
    
    while (cin >> a >> b >> X >> Y >> n && a != 0) {
        solve();
    }
    
    return 0;
}
accuracy: 1
time_ms: 550
memory_kb: 20760
final_rank: 2

code_snippet: #include <bits/stdc++.h>
using namespace std;

#define ll long long
#define elif else if

#define repname(a, b, c, d, e, ...) e
#define rep(...)                    repname(__VA_ARGS__, rep3, rep2, rep1, rep0)(__VA_ARGS__)
#define rep0(x)                     for (int rep_counter = 0; rep_counter < (x); ++rep_counter)
#define rep1(i, x)                  for (int i = 0; i < (x); ++i)
#define rep2(i, l, r)               for (int i = (l); i < (r); ++i)
#define rep3(i, l, r, c)            for (int i = (l); i < (r); i += (c))

#define all(A) A.begin(),A.end()

void solve(ll A,ll B,ll SX,ll SY,ll N){
  map<ll,vector<ll>> M1,M2;
  
  map<pair<ll,ll>,ll> BALL;
  rep(i,N){
    ll x,y;
    cin>>x>>y;
    M1[x+y].push_back(x);
    M2[x-y].push_back(x);
    BALL[{x,y}]=i;
  }
  for(auto &m:M1)sort(all(m.second));
  for(auto &m:M2)sort(all(m.second));
  set<ll> AN;

  vector<ll> dx={1,-1,-1,1},dy={1,1,-1,-1};

  rep(dir,4){
    ll d=dir;
    ll x=SX;
    ll y=SY;
    map<pair<pair<ll,ll>,ll>,ll> seen;
    bool OK=1;
    ll C=-1;
    while(OK){
      ll dt=-1;
      if(seen[{{x,y},d}]>1){
        OK=0;
        break;
      }
      seen[{{x,y},d}]++;
      if(d==0){
        rep(_,2){
          auto p=upper_bound(all(M2[x-y]),x);
          if(p!=M2[x-y].end()){
            ll bx=*p;
            ll by=bx-x+y;
            ll bid=BALL[{bx,by}];
            if(C!=-1&&C!=bid){
              OK=0;
              break;
            }
            C=bid;
            x=bx;
            y=by;
          }
        }
        dt=min(A-x,B-y);
      }
      else if(d==1){
        rep(_,2){
          auto p=lower_bound(all(M1[x+y]),x);
          if(p!=M1[x+y].begin()){
            ll bx=*(prev(p));
            ll by=x+y-bx;
            ll bid=BALL[{bx,by}];
            if(C!=-1&&C!=bid){
              OK=0;
              break;
            }
            C=bid;
            x=bx;
            y=by;
          }
        }
        dt=min(x,B-y);
      }
      else if(d==2){
        rep(_,2){
          auto p=lower_bound(all(M2[x-y]),x);
          if(p!=M2[x-y].begin()){
            ll bx=*(prev(p));
            ll by=bx-x+y;
            ll bid=BALL[{bx,by}];
            if(C!=-1&&C!=bid){
              OK=0;
              break;
            }
            else{
              
              if(OK){
                AN.insert(bid);
                OK=0;
              }
              break;
            }
            
          }
        }
        dt=min(x,y);
      }
      else{
        rep(_,2){
          auto p=upper_bound(all(M1[x+y]),x);
          if(p!=M1[x+y].end()){
            ll bx=*p;
            ll by=x+y-bx;
            ll bid=BALL[{bx,by}];
            if(C!=-1&&C!=bid){
              OK=0;
              break;
            }
            C=bid;
            x=bx;
            y=by;
          }
        }
        dt=min(A-x,y);
      }
      ll nx=-1,ny=-1;
      x=x+dx[d]*dt;
      y=y+dy[d]*dt;
      if(max(min(x,A-x),min(y,B-y))==0){
        OK=0;
        break;
      }
      if(x==0||x==A){
        if(d==0)d=1;
        else if(d==1)d=0;
        else if(d==2)d=3;
        else if(d==3)d=2;
      }
      else{
        if(d==0)d=3;
        else if(d==3)d=0;
        else if(d==2)d=1;
        else if(d==1)d=2;
      }
    }
    
  }
  if(AN.size()==0){
    cout<<"No"<<"\n";
  }
  else{
    vector<ll> ANV;
    for(auto s: AN)ANV.push_back(s);
    ll an=ANV.size();
    rep(i,an)cout<<ANV[i]+1<<(an==i-1?"":" ");
    cout<<endl;
  }
}

int main(){
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  ll A,B,SX,SY,N;
  while(cin>>A>>B>>SX>>SY>>N){
    if(A+B+SX+SY+N==0)return 0;
    solve(A,B,SX,SY,N);
  }
}
accuracy: 1
time_ms: 2720
memory_kb: 162184
final_rank: 13

code_snippet: #include <bits/stdc++.h>
using namespace std;

#define ll long long
#define elif else if

#define repname(a, b, c, d, e, ...) e
#define rep(...)                    repname(__VA_ARGS__, rep3, rep2, rep1, rep0)(__VA_ARGS__)
#define rep0(x)                     for (int rep_counter = 0; rep_counter < (x); ++rep_counter)
#define rep1(i, x)                  for (int i = 0; i < (x); ++i)
#define rep2(i, l, r)               for (int i = (l); i < (r); ++i)
#define rep3(i, l, r, c)            for (int i = (l); i < (r); i += (c))

#define all(A) A.begin(),A.end()

void solve(ll A,ll B,ll SX,ll SY,ll N){
  map<ll,vector<ll>> M1,M2;
  
  map<pair<ll,ll>,ll> BALL;
  rep(i,N){
    ll x,y;
    cin>>x>>y;
    M1[x+y].push_back(x);
    M2[x-y].push_back(x);
    BALL[{x,y}]=i;
  }
  for(auto &m:M1)sort(all(m.second));
  for(auto &m:M2)sort(all(m.second));
  set<ll> AN;

  vector<ll> dx={1,-1,-1,1},dy={1,1,-1,-1};

  rep(dir,4){
    ll d=dir;
    ll x=SX;
    ll y=SY;
    map<pair<pair<ll,ll>,ll>,ll> seen;
    bool OK=1;
    ll C=-1;
    while(OK){
      ll dt=-1;
      if(seen[{{x,y},d}]>1){
        OK=0;
        break;
      }
      seen[{{x,y},d}]++;
      if(d==0){
        rep(_,2){
          auto p=upper_bound(all(M2[x-y]),x);
          if(p!=M2[x-y].end()){
            ll bx=*p;
            ll by=bx-x+y;
            ll bid=BALL[{bx,by}];
            if(C!=-1&&C!=bid){
              OK=0;
              break;
            }
            C=bid;
            x=bx;
            y=by;
          }
        }
        dt=min(A-x,B-y);
      }
      else if(d==1){
        rep(_,2){
          auto p=lower_bound(all(M1[x+y]),x);
          if(p!=M1[x+y].begin()){
            ll bx=*(prev(p));
            ll by=x+y-bx;
            ll bid=BALL[{bx,by}];
            if(C!=-1&&C!=bid){
              OK=0;
              break;
            }
            C=bid;
            x=bx;
            y=by;
          }
        }
        dt=min(x,B-y);
      }
      else if(d==2){
        rep(_,2){
          auto p=lower_bound(all(M2[x-y]),x);
          if(p!=M2[x-y].begin()){
            ll bx=*(prev(p));
            ll by=bx-x+y;
            ll bid=BALL[{bx,by}];
            if(C!=-1&&C!=bid){
              OK=0;
              break;
            }
            else{
              
              if(OK){
                AN.insert(bid);
                OK=0;
              }
              break;
            }
            
          }
        }
        dt=min(x,y);
      }
      else{
        rep(_,2){
          auto p=upper_bound(all(M1[x+y]),x);
          if(p!=M1[x+y].end()){
            ll bx=*p;
            ll by=x+y-bx;
            ll bid=BALL[{bx,by}];
            if(C!=-1&&C!=bid){
              OK=0;
              break;
            }
            C=bid;
            x=bx;
            y=by;
          }
        }
        dt=min(A-x,y);
      }
      ll nx=-1,ny=-1;
      x=x+dx[d]*dt;
      y=y+dy[d]*dt;
      if(max(min(x,A-x),min(y,B-y))==0){
        OK=0;
        break;
      }
      if(x==0||x==A){
        if(d==0)d=1;
        else if(d==1)d=0;
        else if(d==2)d=3;
        else if(d==3)d=2;
      }
      else{
        if(d==0)d=3;
        else if(d==3)d=0;
        else if(d==2)d=1;
        else if(d==1)d=2;
      }
    }
    
  }
  if(AN.size()==0){
    cout<<"No"<<endl;
  }
  else{
    vector<ll> ANV;
    for(auto s: AN)ANV.push_back(s);
    ll an=ANV.size();
    rep(i,an)cout<<ANV[i]+1<<(an==i-1?"":" ");
    cout<<endl;
  }
}

int main(){
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  ll A,B,SX,SY,N;
  while(cin>>A>>B>>SX>>SY>>N){
    if(A+B+SX+SY+N==0)return 0;
    solve(A,B,SX,SY,N);
  }
}
accuracy: 1
time_ms: 2670
memory_kb: 162236
final_rank: 12