Problem:
Origami, or the art of folding paper
Master Grus is a famous
origami
(paper folding) artist, who is
enthusiastic about exploring the possibility of origami art.
For future creation, he is now planning fundamental
experiments to establish the general theory of origami.
One rectangular piece of paper is used in each of his experiments.
He folds it horizontally and/or vertically several times and then
punches through holes in the folded paper.
The following figure illustrates the folding process of a simple experiment,
which corresponds to the third dataset of the Sample Input below.
Folding the 10 × 8 rectangular piece of paper shown at top left
three times results in the 6 × 6 square shape shown at
bottom left.
In the figure, dashed lines indicate the locations to fold the paper
and round arrows indicate the directions of folding.
Grid lines are shown as solid lines to tell the sizes of rectangular
shapes and the exact locations of folding.
Color densities represent the numbers of overlapping layers.
Punching through holes at A and B in the folded paper makes
nine holes in the paper, eight at A and another at B.
Your mission in this problem is to write a computer program to count
the number of holes in the paper, given the information on a rectangular
piece of paper and folding and punching instructions.
Input
The input consists of at most 1000 datasets, each in the following format.
n
m
t
p
d
1
c
1
...
d
t
c
t
x
1
y
1
...
x
p
y
p
n
and
m
are the width and the height, respectively, of
a rectangular piece of paper.
They are positive integers at most 32.
t
and
p
are the numbers of folding and punching
instructions, respectively.
They are positive integers at most 20.
The pair of
d
i
and
c
i
gives the
i
-th folding instruction as follows:
d
i
is either 1 or 2.
c
i
is a positive integer.
If
d
i
is 1, the left-hand side of the vertical folding line that passes at
c
i
right to the left boundary is folded onto the right-hand side.
If
d
i
is 2, the lower side of the horizontal folding line that passes at
c
i
above the lower boundary is folded onto the upper side.
After performing the first
i
−1 folding instructions, if
d
i
is 1, the width of the shape is greater than
c
i
.
Otherwise the height is greater than
c
i
.

(
x
i
+ 1/2,
y
i
+ 1/2) gives the coordinates of
the point where the
i
-th punching instruction is performed.
The origin (0, 0) is at the bottom left of the finally obtained shape.
x
i
and
y
i
are both non-negative integers and they are less than the width and the height, respectively, of the shape.
You can assume that no two punching instructions punch holes at the same location.
The end of the input is indicated by a line containing four zeros.
Output
For each dataset, output
p
lines, the
i
-th of which contains
the number of holes punched in the paper by the
i
-th punching instruction.
Sample Input
2 1 1 1
1 1
0 0
1 3 2 1
2 1
2 1
0 0
10 8 3 2
2 2
1 3
1 1
0 1
3 4
3 3 3 2
1 2
2 1
1 1
0 1
0 0
0 0 0 0
Output for the Sample Input
2
3
8
1
3
6

Submitted solutions:

code_snippet: #include <iostream>
#include <cstring>
#define ll long long
#define N 100000

using namespace std;

int g[600][600], dx, dy;

int main(void) {
#ifdef _DEBUG
	freopen("1.in", "r", stdin);
#else 
	ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#endif
	int n, m, t, p;
	while (cin >> n >> m >> t >> p, n) {
		dx = dy = 0;
		memset(g, 0, sizeof(g));
		for (int i = 0; i != n; ++i) for (int j = 0; j != m; ++j) g[i][j] = 1;
		while (t--) { 
			int d, c; cin >> d >> c;
			if (d == 1) {
				for (int i = 0; i != c; ++i)
					for (int j = 0; j != m - dy; ++j)
						g[dx + c + i][dy + j] += g[dx + c - i - 1][dy + j];
				dx += c;
				if (n < dx + 2 * c) n = dx + 2 * c;
			}
			else if (d == 2) {
				for (int j = 0; j != c; ++j)
					for (int i = 0; i != n - dx; ++i)
						g[dx + i][dy + c + j] += g[dx + i][dy + c - j - 1];
				dy += c;
				if (m < dy + 2 * c) m = dy + 2 * c;
			}
		}
		while (p--) {
			int x, y; cin >> x >> y;
			cout << g[dx + x][dy + y] << endl;
		}
	}
	return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 4784
final_rank: 5

code_snippet: #include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<b;i++)
#define rrep(i,a,b) for(int i=a;i>=b;i--)
#define fore(i,a) for(auto &i:a)
#define all(x) (x).begin(),(x).end()
using namespace std; void _main(); int main() { cin.tie(0); ios::sync_with_stdio(false); _main(); }
typedef long long ll; const int inf = INT_MAX / 2; const ll infl = 1LL << 60;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a = b; return 1; } return 0; }
//---------------------------------------------------------------------------------------------------
/*---------------------------------------------------------------------------------------------------
　　　　　　　　　　　 ∧＿∧  
　　　　　 ∧＿∧ 　（´<_｀ ）　 Welcome to My Coding Space!
　　　　 （ ´_ゝ`）　/　 ⌒i     
　　　　／　　　＼　 　  |　|     
　　　 /　　 /￣￣￣￣/　　|  
　 ＿_(__ﾆつ/　    ＿/ .| .|＿＿＿＿  
　 　　　＼/＿＿＿＿/　（u　⊃  
---------------------------------------------------------------------------------------------------*/




int W, H, T, P;
int A[2][101][101];
//---------------------------------------------------------------------------------------------------
void _main() {
    while (cin >> W >> H >> T >> P) {
        if (W == 0) return;
        
        rep(t, 0, 2) rep(y, 0, 101) rep(x, 0, 101) A[t][y][x] = 0;
        rep(y, 0, H) rep(x, 0, W) A[0][y][x] = 1;

        rep(t, 0, T) {
            int d, c; cin >> d >> c;
            rep(y, 0, 101) rep(x, 0, 101) A[(t + 1) % 2][y][x] = 0;

            if (d == 1) {
                rep(y, 0, H) rep(x, 0, W) A[(t + 1) % 2][y][x] = A[t % 2][y][x + c];
                rep(x, 0, c) rep(y, 0, H) A[(t + 1) % 2][y][x] += A[t % 2][y][c - 1 - x];
            } else {
                rep(y, 0, H) rep(x, 0, W) A[(t + 1) % 2][y][x] = A[t % 2][y + c][x];
                rep(x, 0, W) rep(y, 0, c) A[(t + 1) % 2][y][x] += A[t % 2][c - 1 - y][x];
            }
        }

        rep(p, 0, P) {
            int x, y; cin >> x >> y;
            printf("%d\n", A[T % 2][y][x]);
        }
    }
}
accuracy: 1
time_ms: 10
memory_kb: 3576
final_rank: 3

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using a2 = array<ll, 2>;
using a3 = array<ll, 3>;

template <typename A> void chmin(A &l, const A &r) {
    if(r < l)
        l = r;
}
template <typename A> void chmax(A &l, const A &r) {
    if(l < r)
        l = r;
}

ll mod = 998244353;

void init() {}

ll w, h, t, p;
void input() { cin >> w >> h >> t >> p; }
void solve() {
    vector<vector<ll>> v(1000, vector<ll>(1000, 0));
    ll th = 0, tw = 0, bh = h, bw = w;

    for(int i = 0; i < h; i++)
        for(int j = 0; j < w; j++)
            v[i][j] = 1;

    for(int i = 0; i < t; i++) {
        ll a, b;
        cin >> a >> b;
        if(a == 1) {
            for(int j = th; j < bh; j++) {
                for(int k = tw; k < tw + b; k++) {
                    ll diff = 2 * (tw + b - k) - 1;
                    v[j][k + diff] += v[j][k];
                    chmax(bw, (ll)(k+diff+1));
                }
            }
            tw += b;
        } else {
            for(int j = th; j < th + b; j++) {
                for(int k = tw; k < bw; k++) {
                    ll diff = 2 * (th + b - j) - 1;
                    v[j+diff][k] += v[j][k];
                    chmax(bh, (ll)(j+diff+1));
                }
            }
            th += b;
        }
    }

    for(int i = 0; i < p; i++) {
        ll a, b;
        cin >> a >> b;
        cout << v[th + b][tw + a] << endl;
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);

    // init();
    while(1) {
        input();
        if(w == 0)
            break;
        solve();
    }

    return 0;
}
accuracy: 1
time_ms: 1570
memory_kb: 11052
final_rank: 16

code_snippet: #include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;
using namespace atcoder;
#define int long long

void solve(int n, int m, int t, int p) {
    int k = 30;
    vector<vector<int>> paper(m*k, vector<int>(n*k, 0));
    for (int i=0; i<m; ++i) {
        for (int j=0; j<n; ++j) {
            paper[m*k-i-1][j] = 1;
        }
    }

    int left = 0, low = m*k-1;
    for (;t--;) {
        int d, c;
        cin >> d >> c;
        if (d == 1) {
            //縦軸で折り返す
            for (int i=0; i<=low; ++i) {
                for (int j=0; j<c; ++j) {
                    //left left+1 left+2 | left+6-2-1
                    //left | left+2-0-1
                    //paper[i][j]をpaper[i][left+2c-j-1]に加算
                    paper[i][left+2*c-j-1] += paper[i][left+j];
                    paper[i][left+j] = 0;
                }
            }
            left += c;
        } else {
            //横軸で折り返す
            for (int i=0; i<c; ++i) {
                for (int j=left; j<n*k; ++j) {
                    //low low-1 low-2 | low-6+2+1 low-6+1+1 low-6+0+1
                    paper[low-2*c+i+1][j] += paper[low-i][j];
                    paper[low-i][j] = 0;
                }
            }
            low -= c;
        }
        // for (int _=0; _<10; ++_) cout << "=";
        // cout << "here~~" << "\n";
        // cout << "left low " << left << ' ' << low << endl;
        // for (int i=0; i<m; ++i) {
        //     for (int j=0; j<n; ++j) {
        //         cout << paper[i][j] << ' ';
        //     }
        //     cout << endl;
        // }
        // for (auto _x: paper) {
        //     for (auto __x: _x) {
        //         cout << __x << ' ';
        //     }
        //     cout << endl;
        // }
    }

    for (;p--;) {
        int x, y;
        cin >> x >> y;
        cout << paper[low-y][left+x] << endl;
    }
}

signed main() {
    while(true) {
        int n, m, t, p;
        cin >> n >> m >> t >> p;
        if (n+m+t+p == 0) break;
        solve(n, m, t, p);
    }
}

/*
memo


2 1 1 1
1 1
0 0

1 3 2 1
2 1
2 1
0 0
10 8 3 2
2 2
1 3
1 1
0 1
3 4


3 3 3 2
1 2
2 1
1 1
0 1
0 0
0 0 0 0


*/
accuracy: 1
time_ms: 20
memory_kb: 10668
final_rank: 10

code_snippet: #include <bits/stdc++.h>
using namespace std;
typedef int ll;
typedef unsigned long long ull;
typedef long double ld;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define rrep(i,start,end) for (ll i = start;i >= (ll)(end);i--)
#define repn(i,end) for(ll i = 0; i <= (ll)(end); i++)
#define reps(i,start,end) for(ll i = start; i < (ll)(end); i++)
#define repsn(i,start,end) for(ll i = start; i <= (ll)(end); i++)
#define each(p,a) for(auto &p:a)
typedef vector<ll> vll;
typedef vector<pair<ll ,ll>> vpll;
typedef vector<vector<ll>> vvll;
typedef set<ll> sll;
typedef map<ll , ll> mpll;
typedef pair<ll ,ll> pll;
typedef tuple<ll , ll , ll> tpl3;
#define LL(...) ll __VA_ARGS__; input(__VA_ARGS__)
#define LD(...) ld __VA_ARGS__; input(__VA_ARGS__)
#define Str(...) string __VA_ARGS__; input(__VA_ARGS__)
#define Ch(...) char __VA_ARGS__; input(__VA_ARGS__)
#define all(a)  (a).begin(),(a).end()
#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() );
#define sz(x) (ll)x.size()
// << std::fixed << std::setprecision(10)
const ll INF = 1LL << 30;
const ld EPS = 1e-9;
 
ll lceil(ll a,ll b){if(a%b==0){return a/b;}if(a>=0){return (a/b)+1;}else{return -((-a)/b);}}
ll lfloor(ll a,ll b){if(a%b==0){return a/b;}if(a>=0){return (a/b);}else{return -((-a)/b)-1;}}
inline ll positive_mod(ll a,ll m){return (a % m + m)%m;}
inline ll popcnt(ull a){ return __builtin_popcountll(a);}
//0indexed
inline ll topbit(ll a){assert(a != 0);return 63 - __builtin_clzll(a);}
inline ll smlbit(ll a){assert(a != 0);return __builtin_ctzll(a);}
template<class T> bool chmin(T& a, T b){if(a > b){a = b;return true;}return false;}
template<class T> bool chmax(T& a, T b){if(a < b){a = b;return true;}return false;}
template<typename T> std::istream &operator>>(std::istream&is,std::vector<T>&v){for(T &in:v){is>>in;}return is;}
template<typename T> std::ostream &operator<<(std::ostream&os,const std::vector<T>&v){for(auto it=std::begin(v);it!=std::end(v);){os<<*it<<((++it)!=std::end(v)?" ":"");}return os;}
template<typename T1, typename T2>std::ostream &operator<< (std::ostream &os, std::pair<T1,T2> p){os << "{" << p.first << "," << p.second << "}";return os;}
template<class... T>void input(T&... a){(cin >> ... >> a);}
void print(){cout << endl;}
template<class T, class... Ts>void print(const T& a, const Ts&... b){cout << a;((cout << ' ' << b), ...);cout << endl;}
template<class T> void pspace(const T& a){ cout << a << ' ';}
void perr(){cerr << endl;}
template<class T, class... Ts>void perr(const T& a, const Ts&... b){cerr << a;((cerr << ' ' << b), ...);cerr << endl;}
void yes(bool i = true){ return print(i?"yes":"no"); }
void Yes(bool i = true){ return print(i?"Yes":"No"); }
void YES(bool i = true){ return print(i?"YES":"NO"); }
template <class T> vector<T> &operator++(vector<T> &v) {for(auto &e : v) e++;return v;}
template <class T> vector<T> operator++(vector<T> &v, signed) {auto res = v;for(auto &e : v) e++;return res;}
template <class T> vector<T> &operator--(vector<T> &v) {for(auto &e : v) e--;return v;}
template <class T> vector<T> operator--(vector<T> &v, signed) {auto res = v;for(auto &e : v) e--;return res;}
//grid探索用
vector<ll> _ta = {0,0,1,-1,1,1,-1,-1};
vector<ll> _yo = {1,-1,0,0,1,-1,1,-1};
bool isin(ll now_i,ll now_j,ll h,ll w){return (0<=now_i && now_i < h && 0 <= now_j && now_j < w);}
  
ll lpow(ll x,ll n){ll ans = 1;while(n >0){if(n & 1)ans *= x;x *= x;n >>= 1;}return ans;}
ll Modlpow(ll x,ll n,ll m){ll ans = 1;ll a = x%m;while(n >0){if(n & 1){ans *= a;ans%= m;}a *= a;a %= m;n >>= 1;}return ans;} 
const ll MOD9 = 998244353LL;
const ll MOD10 = 1000000007LL;


 // for AOJ or ICPC or etc..
template<class Tp>
bool zero (const Tp &x) {
    return x == 0;
}

template<class Tp, class... Args>
bool zero (const Tp &x, const Args& ...args) {
    return zero(x) and zero(args...);
}

void solve(ll n,ll m,ll t,ll p){
  swap(n,m);
  vvll paper(700,vll(700));
  ll tp = 0,dn = n;
  ll l = 0,r = m; 
  reps(i,tp,dn)reps(j,l,r){
    paper[i][j] = 1;
  }
  while(t--){
    LL(d,c);
    if(d == 1){
      for(ll i = tp;i < dn;i++){
        rep(j,c){
          paper[i][l+c+j] += paper[i][l+c-1-j];
          paper[i][l+c-1-j] = 0;
        }
      }
      l += c;
      chmax(r,l+c);
    }else{
      rep(i,c){
        for(ll j =l;j<r;j++){
          paper[tp+c+i][j] += paper[tp+c-1-i][j];
          paper[tp+c-1-i][j] = 0;
        }
      }
      tp += c;
      chmax(dn,tp+c);
    }
  }
  // ll ans = 0;
  while(p--){
    LL(x,y);
    cout << paper[tp+y][l+x] << endl;
  }
}

int main(){
  ios::sync_with_stdio(false);cin.tie(nullptr);
  while(true){
    LL(n,m,t,p);
    // perr(n,m,t,p);
    if(zero(n,m,t,p))break;
    solve(n,m,t,p);
  }
}
accuracy: 1
time_ms: 240
memory_kb: 5376
final_rank: 12

code_snippet: #include <bits/stdc++.h>
using namespace std;
int main () {
    while(1){
        int n,m,t,p;
        cin>>m>>n>>t>>p;
        if(n == 0 && m == 0 && t == 0 && p == 0){
            break;
        }
        vector<vector<int>> g(1000,vector<int>(1000,0));
        for(int i = 0 ; i < n;i++){
            for(int j = 0 ; j < m;j++){
                g[i][j] = 1;
            }
        }
        int d[t],c[t];
        for(int i = 0 ; i < t;i++){
            cin>>d[i]>>c[i];
        }
        int x[p],y[p];
        for(int i = 0 ; i < p;i++){
            cin>>x[i]>>y[i];
        }
        int L = 0,R = m,U = n ,D = 0;
        for(int i =  0 ; i < t;i++){
            if(d[i] == 1){
                for(int j = 0; j < c[i];j++){
                    for(int k = D ; k <U;k++){
                        g[k][L + c[i] + j] += g[k][L + c[i] - j - 1];
                    }
                }
                L += c[i];
                if(R-L < c[i]){
                    R = L + 2 * c[i];
                    // R += (c[i] - R + 1);
                }
            }else{
                for(int j = 0 ; j < c[i];j++){
                    for(int k = L ; k < R; k++){
                        g[D + c[i] + j][k] += g[D + c[i] - j - 1][k];
                    }
                }
                D += c[i];
                if(U-D < c[i]) {
                    U = D + 2 * c[i];
                    // U += (c[i]-U+1);
                }
            }
        }
        vector<vector<bool>> che(1000,vector<bool>(1000,false));
        for(int i = 0 ; i < p;i++){
            cout<<g[D + y[i]][L + x[i]]<<endl;
            che[D+y[i]][L+x[i]] = true;
        }
    }
    return(0);
}
accuracy: 1
time_ms: 140
memory_kb: 7480
final_rank: 9

code_snippet: #include <bits/stdc++.h>
//using
using intl = long long;
using ll = long long;
using itnl = long long;//typo用
using uintl = unsigned long long;
using itn = int;//typo用
using ld = long double;
using namespace std;

//関数マクロ
#define rep(i, n) for(intl i = 0; i < (intl)(n); i++)
#define rrep(i, n) for(intl i = (intl)(n) - 1; i >= 0; i--)
#define repi(i, a, b) for(intl i = (intl)(a); i < (intl)(b); i++)
#define rrepi(i, a, b) for(intl i = (intl)(a) - 1; i >= (intl)(b); i--)
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define m0(x) memset(x,0,sizeof(x))
#define m1(x) memset(x,1,sizeof(x))
#define fill(x,y) memset(x,y,sizeof(x))
#define alength(a) (sizeof(a) / sizeof(a[0]))
#define debug(x) cout << #x << ":" << x << endl

//定数マクロ
#define pb push_back
#define mp make_pair
#define pii pair<intl,intl>

//定数
const intl INF = 1e18;
const intl MOD = 1e9+7; //1e9より大きい最小の素数, 1e9より小さい最大の素数は998244353
const ld EPS = 1.0e-14;
const ld PI = acos(-1);

//テンプレート関数
template<class T> inline bool chmin(T& a, T b) { if (a > b) {a = b; return true;} return false; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) {a = b; return true;} return false; }
intl modpow(intl a, intl n, intl mod) {intl res = 1;while (n > 0) {if (n & 1) res = res * a % mod;a = a * a % mod;n >>= 1;}return res;}
intl modpow(intl a, intl n) {intl res = 1; while (n > 0) {if (n & 1) res = res * a % MOD;a = a * a % MOD;n >>= 1;}return res;}
//intl gcd(intl a, intl b){ if(!b)return a; return gcd(b, a % b); } //c++17からある
//intl lcm(intl a,intl b){ return a / gcd(a, b) * b; } //c++17からある

//自作テンプレート関数
void fp(bool f){cout << (f ? "Yes" : "No") << endl;}//Yes,Noの出力を楽にするよう
void fp(bool f, string s, string t){cout << (f ? s : t) << endl;}//上の関数のYes,Noを任意の文字列でできるようにしたもの
intl fact(intl k){ intl a = 1; for(int i = 2; i <= k; i++){ a *= i; } return a; }//k!を求める関数,O(N)
intl digit10(intl a){ intl b = 0; do{ a /= 10; b++; }while(a); return b; }//aを10進数で表したときの桁数を求める関数,O(logN)
intl mceil(intl a, intl b){ return (a + b - 1) / b; }//aをbで割って切り上げる関数,O(1)


//--------------入力受け取る場所----------------------------------------------------
intl n,m,t,p;
vector<intl> d,c,x,y;


void input_var(){
    cin >> n >> m >> t >> p;
    if (n == 0 && m == 0 && t == 0 && p == 0) exit(0);
    rep(i, t) {
        cin >> d[i] >> c[i];
    }
    rep(i, p) {
        cin >> x[i] >> y[i];
    }
}
//-------------------------------------------------------------------------------

void solve(){
    intl left = 0;
    intl right = n;
    intl top = 0;
    intl bottom = m;
    vector<vector<intl>> kasanari(100 * m + 1, vector<intl>(100 * n + 1, 0));
    rep(i,m)rep(j,n)kasanari[i][j] = 1;
    /*rep(j,m) {
        rep(k,n)cout << kasanari[j][k] << " ";
        cout << endl;
    }*/
    rep(i, t) {
        //cout << d[i] << c[i] << " " << top << " " << left << endl;
        if (d[i] == 1) {
            for (intl j = 0; j < c[i]; j++) {
                for (intl k = top; k < bottom; k++) {
                    kasanari[k][left + 2 * c[i] - j - 1] += kasanari[k][left + j];
                    kasanari[k][left + j] = 0;
                }
            }
            right = max(left + 2 * c[i], right);
            left += c[i];
        }
        else {
            for (intl j = 0; j < c[i]; j++) {
                for (intl k = left; k < right; k++) {
                    kasanari[top + 2 * c[i] - j - 1][k] += kasanari[top + j][k];
                    kasanari[top + j][k] = 0;
                }
            }
            bottom += max(top + 2 * c[i], bottom);
            top += c[i];
        }
        /*rep(j,2*m+1) {
            rep(k,2*n+1)cout << kasanari[j][k] << " ";
            cout << endl;
        }*/
    }
    rep(i, p) {
        //cout << bottom << " " << x[i] << " " << left <<  " " << y[i] << endl; 
        cout << kasanari[y[i]+top][x[i]+left] << endl;
    }
}

signed main(){
    cout << fixed << setprecision(10);

    d.resize(21);
    c.resize(21);
    x.resize(21);
    y.resize(21);
    while (true) {
        input_var();
        solve();
    }
    return 0;
}
accuracy: 1
time_ms: 420
memory_kb: 83340
final_rank: 19

code_snippet: #include<bits/stdc++.h>
using namespace std;
int main(){
    int n,m,t,p;
    int d,c,x,y;
    int cum,cun;
    int ans;
    while(1){
        cin>>n>>m>>t>>p;
        ans=0;
        if(n+m+t+p==0){
            break;
        }
        vector<vector<int>> paper(1500,vector<int>(1500,0)); //横、高さの順
        for(int i=0;i<n;i++){
            for(int j=0;j<m;j++){
                paper.at(j).at(i)=1;
            }
        }
        cun=0;
        cum=0;
        for(int i=0;i<t;i++){
            cin>>d>>c;
            if(d==1){
                for(int j=cum;j<m;j++){
                    for(int k=cun+c,l=1;k<cun+2*c;k++,l+=2){
                        paper.at(j).at(k)+=paper.at(j).at(k-l);
                    }
                }
                cun+=c;
                n+=max(0,cun-n+c);
            }
            else if(d==2){
                for(int j=cum+c,l=1;j<cum+2*c;j++,l+=2){
                    for(int k=cun;k<n;k++){
                        paper.at(j).at(k)+=paper.at(j-l).at(k);
                    }
                }
                cum+=c;
                m+=max(0,cum-m+c);
            }
        }
        ans=0;
        for(int i=0;i<p;i++){
            cin>>x>>y;
            cout<<paper.at(cum+y).at(cun+x)<<endl;
        }
    }
}
accuracy: 1
time_ms: 1760
memory_kb: 11968
final_rank: 17

code_snippet: //#include <atcoder/all>
#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = a; i < b; i++)
#define rrep(i, a, b) for (int i = a - 1; i >= b; i--)
#define ll long long
#define ull unsigned long long
#define graph vector<vector<int>>
using namespace std;
//using namespace atcoder;

int mod=998244353;
vector<int> dx={1,0,-1,0},dy={0,1,0,-1};

int main(){
    while(1){
        int n,m,t,p; cin>>n>>m>>t>>p;
        if(n==0) break;

        vector<vector<ll>> grid(650,vector<ll>(650));
        rep(i,0,n){
            rep(j,0,m){
                grid[i][j]=1;
            }
        }

        int l=0,u=0;

        rep(i,0,t){
            int d,c; cin>>d>>c;
            if(d==1){
                l += c;
                rep(ii,u,u+m){
                    rep(j,0,c){
                        grid[l+j][ii] += grid[l-j-1][ii];
                        grid[l-j-1][ii]=0;
                    }
                }
            }

            else{
                u += c;
                rep(ii,l,l+n){
                    rep(j,0,c){
                        grid[ii][u+j] += grid[ii][u-j-1];
                        grid[ii][u-j-1]=0;
                    }
                }
            }
        }

        rep(i,0,p){
            int x,y; cin>>x>>y;
            cout << grid[x+l][y+u] << endl;
        }
    }
    
}
accuracy: 1
time_ms: 630
memory_kb: 6556
final_rank: 15

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vll>;
using vvvll = vector<vvll>;
#define all(A) A.begin(),A.end()
#define ALL(A) A.begin(),A.end()
#define rep(i, n) for (ll i = 0; i < (ll) (n); i++)
template<class T>
bool chmax(T& p, T q, bool C = 1) {
    if (C == 0 && p == q) {
        return 1;
    }
    if (p < q) {
        p = q;
        return 1;
    }
    else {
        return 0;
    }
}

template<class T>
bool chmin(T& p, T q, bool C = 1) {
    if (C == 0 && p == q) {
        return 1;
    }
    if (p > q) {
        p = q;
        return 1;
    }
    else {
        return 0;
    }
}



void solve(ll N, ll M, ll T, ll P) {
    swap(N, M);
    vector<vll> D(N * 100, vll(M * 100, 0));
    rep(i, N)rep(j, M)D[i][j] = 1;
    rep(t, T) {
        ll d, c;
        cin >> d >> c;

        if (d == 1) {
            ll L = 1e18;
            rep(j, M * 100)rep(i, N * 100) {
                if (D[i][j] > 0) {
                    L = min(L, j);
                    break;
                }
            }
            for (ll j = L; j <= L + c - 1; j++) {
                rep(i, N * 100) {
                    ll ds = L + c - j;
                    D[i][L + c - 1 + ds] += D[i][j];
                    D[i][j] = 0;
                }
            }
        }
        else {
            ll W = 1e18;
            rep(i, N * 100)rep(j, M * 100) {
                //if (W >= 0)break;
                if (D[i][j] > 0) {
                    W = min(W, i);
                    break;
                }
            }
            for (ll i = W; i <= W + c - 1; i++) {
                rep(j, M * 100) {
                    ll ds = W + c - i;
                    D[W + c - 1 + ds][j] += D[i][j];
                    D[i][j] = 0;
                }
            }
        }
    }
    ll L = 1e18, W = 1e18;
    rep(i, N * 100)rep(j, M * 100) {
        if (D[i][j] > 0) {
            W = min(W, i);
            L = min(L, j);
        }
    }
    ll an = 0;
    rep(p, P) {
        ll X, Y;
        cin >> X >> Y;
        cout << D[Y + W][X + L] << endl;
    }
}


int main() {

    cin.tie(nullptr);
    ios::sync_with_stdio(false);

    ll N, M, T, P;
    while (cin >> N >> M >> T >> P, N != 0)solve(N, M, T, P);

}
accuracy: 1
time_ms: 3270
memory_kb: 83300
final_rank: 20

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

void solve(int n, int m, int t, int p){
    vector<int> d(t), c(t);
    for(int i = 0; i < t; i++){
        cin >> d[i] >> c[i];
    }
    vector<int> x(p), y(p);
    for(int i = 0; i < p; i++){
        cin >> x[i] >> y[i];
    }
    
    // 動的にメモリを確保
    vector<vector<int>> paper(n * n, vector<int>(m * m, 0));
    for(int i = 0; i < n; i++){
        for(int j = 0; j < m; j++){
            paper[i][j] = 1;
        }
    }
    
    int delx = 0, dely = 0;
    int widx = n, widy = m;

    for(int i = 0; i < t; i++){
        if(d[i] == 1){
            for(int j = dely; j < dely + widy; j++){
                for(int k = delx; k < delx + c[i]; k++){
                    paper[2 * (delx + c[i]) - 1 - k][j] += paper[k][j];
                }
            }
            delx += c[i];
            if(c[i] * 2 < widx) widx -= c[i];
            else widx = c[i];
        }
        else if(d[i] == 2){
            for(int j = delx; j < delx + widx; j++){
                for(int k = dely; k < dely + c[i]; k++){
                    paper[j][2 * (dely + c[i]) - 1 - k] += paper[j][k];
                }
            }
            dely += c[i];
            if(c[i] * 2 < widy) widy -= c[i];
            else widy = c[i];
        }
    }

    for(int i = 0; i < p; i++){
        // 範囲外アクセスを防ぐためのチェック
        if (delx + x[i] < n * n && dely + y[i] < m * m) {
            cout << paper[delx + x[i]][dely + y[i]] << endl;
        } 
        //else {
        //    cout << "Out of bounds" << endl;
        //}
    }
}

int main() {
    while(true){
        int n, m, t, p;
        cin >> n >> m >> t >> p;
        if(n == 0) break;
        solve(n, m, t, p);
    }
    //return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 7344
final_rank: 6

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

void solve(int n, int m, int t, int p){
    vector<int> d(t), c(t);
    for(int i = 0; i < t; i++){
        cin >> d[i] >> c[i];
    }
    vector<int> x(p), y(p);
    for(int i = 0; i < p; i++){
        cin >> x[i] >> y[i];
    }
    
    // 動的にメモリを確保
    vector<vector<int>> paper(n * n, vector<int>(m * m, 0));
    for(int i = 0; i < n; i++){
        for(int j = 0; j < m; j++){
            paper[i][j] = 1;
        }
    }
    
    int delx = 0, dely = 0;
    int widx = n, widy = m;

    for(int i = 0; i < t; i++){
        if(d[i] == 1){
            for(int j = dely; j < dely + widy; j++){
                for(int k = delx; k < delx + c[i]; k++){
                    paper[2 * (delx + c[i]) - 1 - k][j] += paper[k][j];
                }
            }
            delx += c[i];
            if(c[i] * 2 < widx) widx -= c[i];
            else widx = c[i];
        }
        else if(d[i] == 2){
            for(int j = delx; j < delx + widx; j++){
                for(int k = dely; k < dely + c[i]; k++){
                    paper[j][2 * (dely + c[i]) - 1 - k] += paper[j][k];
                }
            }
            dely += c[i];
            if(c[i] * 2 < widy) widy -= c[i];
            else widy = c[i];
        }
    }

    for(int i = 0; i < p; i++){
        // 範囲外アクセスを防ぐためのチェック
        if (delx + x[i] < n * n && dely + y[i] < m * m) {
            cout << paper[delx + x[i]][dely + y[i]] << endl;
        } else {
            cout << "Out of bounds" << endl;
        }
    }
}

int main() {
    while(true){
        int n, m, t, p;
        cin >> n >> m >> t >> p;
        if(n == 0) break;
        solve(n, m, t, p);
    }
    return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 7128
final_rank: 7

code_snippet: #include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
  #include "settings/debug.cpp"
  #define _GLIBCXX_DEBUG
#else
  #define Debug(...) void(0)
#endif
#define rep(i, n) for (int i = 0; i < (n); ++i)
using ll = long long;
using ull = unsigned long long;

int main() {
  while (true) {
    ll n, m, t, p;
    cin >> m >> n >> t >> p;
    if (n == 0 && m == 0 && t == 0 && p == 0) {
      break;
    }
    ll left = 0, bottom = 30 * n - 1;
    vector<vector<ll>> cnt(30 * n, vector<ll>(30 * m, 0));
    rep(i, n) rep(j, m) cnt[30 * n - 1 - i][j] = 1;
    rep(i, t) {
      ll d, c;
      cin >> d >> c;
      if (d == 1) {
        rep(i, c) {
          ll now_x = bottom, now_y = left + i;
          while (true) {
            if (cnt[now_x][now_y] == 0) {
              break;
            }
            cnt[now_x][left + 2 * c - 1 - i] += cnt[now_x][now_y];
            now_x--;
            // cerr << now_x << " " << now_y << endl;
          }
        }
        left += c;
      }
      else {
        rep(i, c) {
          ll now_x = bottom - i, now_y = left;
          while (true) {
            if (cnt[now_x][now_y] == 0) {
              break;
            }
            cnt[bottom - (2 * c - 1 - i)][now_y] += cnt[now_x][now_y];
            now_y++;
            // cerr << "now: " << now_x << " " << now_y << endl;
          }
        }
        bottom -= c;
      }
      // rep(i, n) {
      //   rep(j, m) {
      //     cerr << cnt[bottom - i][left + j] << " ";
      //   }
      //   cerr << endl;
      // }
      // cerr << endl;
    }
    rep(i, p) {
      ll x, y;
      cin >> y >> x;
      cout << cnt[bottom - x][left + y] << endl;
    }
  }
  return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 10476
final_rank: 11

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
#define rep(i, a, b) for(ll i = a; i < b; ++i)
#define rrep(i, a, b) for(ll i = a; i >= b; --i)
constexpr ll inf = 4e18;
struct SetupIO {
    SetupIO() {
        ios::sync_with_stdio(0);
        cin.tie(0);
        cout << fixed << setprecision(30);
    }
} setup_io;

ll D = 100;
vector<vector<ll>> rot(vector<vector<ll>> tab) {
    vector<vector<ll>> rot_tab(D * 3);
    rep(i,0,D * 3){
        rot_tab[i].resize(D * 3, 0);
    }
    rep(i,0,D * 3){
        rep(j,0,D * 3){
            rot_tab[i][j] = tab[j][i];
        }
    }
    return rot_tab;
}
bool valid(ll i, ll j){
    return (0 <= i) && (i < D * 3) && (0 <= j) && (j < D * 3);
}
vector<vector<ll>> oru(vector<vector<ll>> tab, ll c) {
    vector<vector<ll>> new_tab(D * 3);
    rep(i,0,D * 3){
        new_tab[i].resize(D * 3, 0);
    }

    rep(i,0,D * 3){
        rep(j,0,D * 3){
            if(j < D + c) {
                continue;
            }
            ll j2 = (D+c) + (D+c-1) - j;
            if (!valid(i, j2)) {
                continue;
            }
            new_tab[i][j] = tab[i][j] + tab[i][(D+c) + (D+c-1) - j];
        }
    }
    return new_tab;
}
vector<vector<ll>> hidariue(vector<vector<ll>> tab) {
    // 左上が(D, D)に来るように
    vector<vector<ll>> new_tab(D * 3);
    rep(i,0,D * 3){
        new_tab[i].resize(D * 3, 0);
    }

    ll min_i = inf;
    ll min_j = inf;
    rep(i,0,D * 3){
        rep(j,0,D * 3){
            if(tab[i][j] > 0) {
                min_i = min(min_i, i);
                min_j = min(min_j, j);
            }
        }
    }

    rep(i,0,D * 3){
        rep(j,0,D * 3){
            if (valid(i + (min_i - D), j + (min_j - D))) {
                new_tab[i][j] = tab[i + (min_i - D)][j + (min_j - D)];
            }
        }
    }

    return new_tab;
}
// void output(vector<vector<ll>> tab) {
//     cerr << "\n";
//     rep(i,0,D * 3) {
//         rep(j,0,D * 3) {
//             cerr << tab[i][j];
//         }
//         cerr << "\n";
//     }
//     cerr << "\n";
// }

int main(void) {
    while(true){
        ll n,m,t,p;cin>>n>>m>>t>>p;
        if (n == 0) break;
        vector<vector<ll>> tab(D * 3);
        rep(i,0,D * 3){
            tab[i].resize(D * 3, 0);
        }
        rep(i,D,D+m){
            rep(j,D,D+n){
                tab[i][j] = 1;
            }
        }
        rep(i,0,t){
            ll d,c;cin>>d>>c;
            if (d == 2) {
                tab = rot(tab);
            }
            tab = oru(tab, c);
            if (d == 2) {
                tab = rot(tab);
            }
            tab = hidariue(tab);
        }
        rep(i,0,p){
            ll x, y;cin>>x>>y;
            cout << tab[D + y][D + x] << "\n";
        }
    }
}
accuracy: 1
time_ms: 2340
memory_kb: 5280
final_rank: 18

code_snippet: #include <iostream>
#include <vector>
#include <set>
#include <map>
#include <deque>
#include <algorithm>
#include <assert.h>

using std::cin, std::cout, std::vector, std::string, std::pair;

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

template <typename T>
void print_vector(const vector<T> &vec, const string &splitter = " ") {
    for (int i = 0; i < vec.size(); ++i) {
        cout << vec[i];
        if (i == vec.size() - 1) cout << "\n";
        else cout << splitter;
    }
}


// s = (2b - n^2 + 1)/2n

int main() {
    int w, h, t, p;

    while (cin >> w >> h >> t >> p, w) {
        vector pos(h, vector<pair<int, int>>(w));
        std::multimap<pair<int, int>, pair<int, int>> map;
        for (int i = 0; i < h; ++i) {
            for (int j = 0; j < w; ++j) {
                pos[i][j] = {i, j};
                map.emplace(pair {i, j}, pair {i, j});
            }
        }

        int left_x = 0, right_x = w, bottom_y = 0, top_y = h;
        rep(q, t) {
            int d, c; cin >> d >> c;

            if (d == 1) {
                for (int y = bottom_y; y < top_y; ++y) {
                    for (int xd = 0; xd < c; ++xd) {
                        int x = left_x + c - xd - 1;
                        auto rng = map.equal_range({y, x});
                        int nxt_x = left_x + c + xd;
                        for (auto iter = rng.first; iter != rng.second; ++iter) {
                            auto [i, j] = iter->second;
                            pos[i][j] = { y, nxt_x };
                            map.emplace(pair { y, nxt_x }, pair { i, j });
                        }
                        map.erase({ y, x });
                    }
                }

                left_x += c;
                if (left_x + c >= right_x) {
                    right_x = left_x + c;
                }
            } else {
                for (int yd = 0; yd < c; ++yd) {
                    int y = bottom_y + c - yd - 1;
                    int nxt_y = bottom_y + c + yd;
                    for (int x = left_x; x < right_x; ++x) {
                        auto rng = map.equal_range({ y, x });
                        for (auto iter = rng.first; iter != rng.second; ++iter) {
                            auto [i, j] = iter->second;
                            pos[i][j] = { nxt_y, x };
                            map.emplace(pair { nxt_y, x }, pair { i, j });
                        }
                        map.erase({ y, x });
                    }
                }

                bottom_y += c;
                if (bottom_y + c >= top_y) {
                    top_y = bottom_y + c;
                }
            }
        }

//        int ans = 0;
        rep(q, p) {
            int x, y; cin >> x >> y;
            int tmp = map.count({ y + bottom_y, x + left_x });
//            ans += tmp;
            cout << tmp << std::endl;
//            std::cerr << tmp << "\n";
        }
//        cout << ans << std::endl;
//        std::cerr << std::endl;
    }

}
accuracy: 1
time_ms: 10
memory_kb: 3308
final_rank: 1

code_snippet: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define rep(i, n) for(ll i = 0; i < (n); i++)
#define reps(i, l, r) for(ll i = (l); i < (r); i++)
#define all(a) (a).begin(), (a).end()
#define endl "\n";
const ll INF = 2e18;
const ll mod1 = 1000000007;
const ll mod2 = 998244353;
ll dx[4] = {-1, 0, 1, 0};
ll dy[4] = {0, -1, 0, 1};

void solve() {
  ll N, M, T, P; cin >> N >> M >> T >> P;
  while (N != 0 || M != 0 || T != 0 || P != 0) {
    vector<vector<ll>>A(N*N, vector<ll>(M*M, 0));
    rep(i, N) rep(j, M) A[i][j] = 1;
    ll u = 0, l = 0, up = N, lp = M;
    while (T--) {
      ll d, c; cin >> d >> c;
      if (d == 1) {
        ll nu = u + 2 * c - 1;
        up = max(up, nu + 1);
        while (1) {
          reps(i, l, lp) {A[nu][i] += A[u][i];}
          if (u + 1 == nu) break;
          u++; nu--;
        }
        u = nu;
      }
      else {
        ll nl = l + 2 * c - 1;
        lp = max(lp, nl + 1);
        while (1) {
          reps(i, u, up) {A[i][nl] += A[i][l];}
          if (l + 1 == nl) break;
          l++; nl--;
        }
        l = nl;
      }
    }
    while (P--) {
      ll x, y; cin >> x >> y;
      x += u; y += l;
      cout << A[x][y] << endl;
    }
    cin >> N >> M >> T >> P;
  }
}
signed main() {
    ll T = 1; //cin >> T;
    while (T--) solve();
}
accuracy: 1
time_ms: 30
memory_kb: 11172
final_rank: 13

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

int A[200][200];

int main(){
  int n,m,t,p;
  while(1){
    cin>>n>>m>>t>>p;
    if(n==0&&m==0&&t==0&&p==0)break;

    for(int i=0;i<200;i++)
      for(int j=0;j<200;j++)
        A[i][j]=0;
    
    for(int i=0;i<m;i++)
      for(int j=0;j<n;j++)
        A[i][j]=1;
    
    for(int u=0;u<t;u++){
      int d,c;
      cin>>d>>c;
      if(d==1){
        
        for(int i=0;i<m;i++){
          for(int j=0;j<c;j++){
            int dist=c-j;
            A[i][c+dist-1]+=A[i][j];
            A[i][j]=0;
          }
        }

        for(int i=0;i<100;i++){
          for(int j=0;j<100;j++){
            A[i][j]=A[i][j+c];
          }
        }
        
      }else{
        
        for(int i=0;i<c;i++){
          for(int j=0;j<n;j++){
            int dist=c-i;
            A[c+dist-1][j]+=A[i][j];
            A[i][j]=0;
          }
        }

        for(int i=0;i<100;i++){
          for(int j=0;j<100;j++){
            A[i][j]=A[i+c][j];
          }
        }
        
      }
      
    }
    
    for(int i=0;i<p;i++){
      int x,y;
      cin>>x>>y;
      cout<<A[y][x]<<endl;
    }
    
  }
  return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 3264
final_rank: 2

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

int solve() {
    int m, n, t, p;
    std::cin >> m >> n >> t >> p;
    if (n == 0) return 1;
    const int N = 1000;
    std::vector count(N, std::vector<int>(N));
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            count[i][j] = 1;
        }
    }
    int left = 0;
    int low = 0;
    while (t--) {
        int d, c;
        std::cin >> d >> c;
        if (d == 1) {
            int right = left + 2 * c;
            for (int i = low; i < N; i++) {
                for (int j = 0; j < c; j++) {
                    count[i][right - 1 - j] += count[i][left + j];
                }
            }
            left += c;
        } else {
            int up = low + 2 * c;
            for (int i = 0; i < c; i++) {
                for (int j = left; j < N; j++) {
                    count[up - 1 - i][j] += count[low + i][j];
                }
            }
            low += c;
        }
        // for (int i = 0; i < 2 * n; i++) {
        //     for (int j = 0; j < 2 * m; j++) {
        //         std::cerr << count[i][j] << ' ';
        //     }
        //     std::cerr << std::endl;
        // }
        // std::cerr << "-----------" << std::endl;
    }

    while (p--) {
        int x, y;
        std::cin >> x >> y;
        std::cout << count[low + y][left + x] << '\n';
    }
    return 0;
}

int main() {
    while (!solve());
}
accuracy: 1
time_ms: 90
memory_kb: 7076
final_rank: 8

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

int N, M, T, P;
long long A[1050][1050];
int D[30], C[30];
int X[30], Y[30];
vector<long long> Ans;

int main() {
    while (true) {
        cin >> M >> N >> T >> P;
        if (N == 0 && M == 0 && T == 0 && P == 0) {
            break;
        }
        int lef = 0, dow = 0;
        for (int i = 0; i < 1050; i++) {
            for (int j = 0; j < 1050; j++) {
                A[i][j] = 0;
            }
        }
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < M; j++) {
                A[i][j] = 1;
            }
        }
        for (int i = 0; i < T; i++) {
            cin >> D[i] >> C[i];
        }
        for (int i = 0; i < P; i++) {
            cin >> Y[i] >> X[i];
        }
        for (int i = 0; i < T; i++) {
            if (D[i] == 1) {
                for (int j = 0; j < C[i]; j++) {
                    for (int k = dow; k < 1050; k++) {
                        int ll = C[i] + lef - 1 - j;
                        int rr = j + C[i] + lef;
                        A[k][rr] += A[k][ll];
                    }
                }
                lef += C[i];
            } else {
                for (int j = 0; j < C[i]; j++) {
                    for (int k = lef; k < 1050; k++) {
                        int dd = dow + C[i] - 1 - j;
                        int uu = j + C[i] + dow;
                        A[uu][k] += A[dd][k];
                    }
                }
                dow += C[i];
            }
        }
        for (int i = 0; i < P; i++) {
            Ans.push_back(A[X[i] + dow][Y[i] + lef]);
        }
    }
    for (auto x : Ans) {
        cout << x << endl;
    }
    return 0;
}
accuracy: 1
time_ms: 220
memory_kb: 11988
final_rank: 14

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i,n) for(long long i = 0;i < (long long)(n);i++)

map<pair<ll,ll>,ll> f(map<pair<ll,ll>,ll> mp){
  //0,0の調整
  ll mi_n = 1LL << 60;
  ll mi_m = 1LL << 60;
  for(auto &q:mp){
    if(q.second == 0)continue;
    auto [x,y] = q.first;
    if(x < mi_n){
      mi_n = x;
    }
    if(y < mi_m){
      mi_m = y;
    }
  }

  map<pair<ll,ll>,ll> ret;
  for(auto &q:mp){
    if(q.second == 0)continue;
    auto [x,y] = q.first;
    ret[{x- mi_n,y - mi_m}] = q.second;
  }
  return ret;

  
}

int main(){
  while(true){
  ll n,m,t,p;cin >> n >> m >> t >> p;
  if(n == 0 && m == 0 && t == 0 && p == 0){
    break;
  }
  map<pair<ll,ll>,ll> mp;
  // vector<vector<pair<ll,ll>>> g(n,vector<pair<ll,ll>>(m));
  rep(i,n){
     rep(j,m){
       mp[{i,j}] = 1;
     }
  }

  //おる
  rep(z,t){
    ll d,c;cin >> d >> c;
    map<pair<ll,ll>,ll> tmp_mp;
    if(d == 1){
      for(auto &q:mp){
        auto[x,y] = q.first;
        if(x <c){
          tmp_mp[{c+(c-x-1),y}] += q.second;
          q.second = 0;
        }
      }
    }else{
      for(auto &q:mp){
        auto[x,y] = q.first;
        if(y <c){
          tmp_mp[{x,c+(c-y- 1)}] += q.second;
          q.second = 0;
        }
      }
    }
    for(auto &q:tmp_mp){
      mp[q.first] += q.second;
    }
    mp = f(mp);

  }

  rep(z,p){
    ll x,y;cin >> x >> y;
    if(mp.count({x,y})){
      cout << mp[{x,y}] << endl;
    }else{
      //ないけど
      cout << 0 << endl;
    }
  }

    
  }
}
accuracy: 1
time_ms: 10
memory_kb: 3712
final_rank: 4