Problem:
Problem
今、あるシュミレーションゲームRUPC(Rich Ultra Power Challenge)をしています。
このゲームは $H$ 行 $W$ 列の盤面からなり、盤面上には $N$ 個の斥力石と $Q$ 体の敵が存在しています。
以降、上から $y$ 行目、左から $x$ 列目のマスを $(y, x)$ と表します。
ただし、番号は $0$ から始まります。例えば、左上のマスは $(0, 0)$、右下のマスは $(H-1, W-1)$ です。
$i$ 番目の斥力石は $(a_i, b_i)$ に位置し、斥力 $m_i$ を持っています。
$j$ 番目の敵は初め $(c_j, d_j)$ に位置しています。
RUPCはターン制のゲームです。
それぞれの敵は $1$ ターンごとに次のルールで移動します。
敵の現在地を $(y, x)$ とする
敵は現在地から $\left ( \left [ y+\sum_{i=1}^N ((y - a_i) \times m_i) \right ] \bmod H, \left [ x+\sum_{i=1}^N ((x - b_i) \times m_i) \right ] \bmod W \right)$ へ移動する
$j$ 番目の敵の $k_j$ ターン後の位置を求めてください。
Input
入力は以下の形式で与えられる。
$H$ $W$ $N$ $Q$
$a_1$ $b_1$ $m_1$
$\vdots$
$a_N$ $b_N$ $m_N$
$c_1$ $d_1$ $k_1$
$\vdots$
$c_Q$ $d_Q$ $k_Q$
Constraints
入力は以下の条件を満たす。
$1 \le H, W, N, Q \le 10^5$
$0 \le a_i \lt H$
$0 \le b_i \lt W$
$1 \le m_i \le 10^4$
$0 \le c_j \lt H$
$0 \le d_j \lt W$
$1 \le k_j \le 10^{18}$
入力は全て整数である
Output
(13:40修正)
$Q$ 行出力せよ。
$j$ 行目には $j$ 番目の敵の $k_j$ ターン後の位置を行、列の順番で出力せよ。
Sample Input1
10 10 1 5
0 0 1
1 2 1
1 2 2
1 2 3
1 2 4
1 2 5
Sample Output1
2 4
4 8
8 6
6 2
2 4
Sample Input2
5 5 4 5
1 0 1
3 0 1
1 4 1
3 4 1
2 2 1
1 2 2
3 2 2
1 1 2
0 0 2
Sample Output2
2 2
2 2
2 2
2 2
2 2

Submitted solutions:

code_snippet: /* -*- coding: utf-8 -*-
 *
 * 3083.cc: Antigravity
 */

#include<cstdio>
#include<vector>
#include<algorithm>
 
using namespace std;

/* constant */

const int MAX_N = 100000;
const int BN = 60;

/* typedef */

typedef long long ll;

template <typename T>
struct SegTreeSumDelay {
  int e2;
  vector<T> nodes, das, dbs;
  T defv;
  SegTreeSumDelay() {}

  void init(int n, T _defv) {
    defv = _defv;
    for (e2 = 1; e2 < n; e2 <<= 1);
    nodes.assign(e2 * 2, defv);
    das.assign(e2 * 2, 0);
    dbs.assign(e2 * 2, 0);
  }

  T &geti(int i) { return nodes[e2 - 1 + i]; }
  void seti(int i, T v) { geti(i) = v; }

  void setall() {
    for (int j = e2 - 2; j >= 0; j--)
      nodes[j] = nodes[j * 2 + 1] + nodes[j * 2 + 2];
  }

  void __update(int k, int l) {
    if (das[k] != 0 || dbs[k] != 0) {
      int k0 = k * 2 + 1, k1 = k0 + 1, hl = l >> 1;
      ll a0 = das[k], a1 = das[k] + dbs[k] * hl;
      nodes[k0] += a0;
      nodes[k1] += a1;
      das[k0] += a0, dbs[k0] += dbs[k];
      das[k1] += a1, dbs[k1] += dbs[k];
      das[k] = dbs[k] = 0;
    }
  }

  void updateall(int k, int i0, int i1) {
    if (i0 + 1 < i1) {
      __update(k, i1 - i0);
      int im = (i0 + i1) / 2;
      updateall(k * 2 + 1, i0, im);
      updateall(k * 2 + 2, im, i1);
    }
  }
  void updateall() { updateall(0, 0, e2); }

  void add_range(int r0, int r1, T a, T b, int k, int i0, int i1) {
    if (r1 <= i0 || i1 <= r0) return;
    if (r0 <= i0 && i1 <= r1) {
      nodes[k] += a;
      das[k] += a, dbs[k] += b;
      return;
    }

    __update(k, i1 - i0);

    int im = (i0 + i1) / 2;
    int k0 = k * 2 + 1, k1 = k0 + 1;
    T a0 = a, a1 = a + b * (im - i0);
    add_range(r0, r1, a0, b, k0, i0, im);
    add_range(r0, r1, a1, b, k1, im, i1);
  }
  void add_range(int r0, int r1, T a, T b) {
    add_range(r0, r1, a, b, 0, 0, e2);
  }
};

/* global variables */

int as[MAX_N], bs[MAX_N], ms[MAX_N];
SegTreeSumDelay<ll> st;
int pas[MAX_N][BN], pbs[MAX_N][BN];

/* subroutines */

void calc(int k, int n, int xs[], int ms[], int ps[][BN]) {
  st.init(k, 0);
  for (int i = 0; i < n; i++) {
    ll x0 = -(ll)xs[i] * ms[i];
    st.add_range(0, k, x0, ms[i]);
  }
  st.updateall();
  //for (int i = 0; i < k; i++) printf("%d ", ps[i]); putchar('\n');

  for (int u = 0; u < k; u++)
    ps[u][0] = ((u + st.geti(u)) % k + k) % k;

  for (int i = 0; i < BN - 1; i++)
    for (int u = 0; u < k; u++)
      ps[u][i + 1] = ps[ps[u][i]][i];
}

/* main */

int main() {
  int h, w, n, qn;
  scanf("%d%d%d%d", &h, &w, &n, &qn);

  for (int i = 0; i < n; i++) scanf("%d%d%d", as + i, bs + i, ms + i);

  calc(h, n, as, ms, pas);
  calc(w, n, bs, ms, pbs);

  while (qn--) {
    int c, d;
    ll k;
    scanf("%d%d%lld", &c, &d, &k);

    for (int i = 0; i < BN; i++)
      if ((k >> i) & 1)
	c = pas[c][i], d = pbs[d][i];

    printf("%d %d\n", c, d);
  }

  return 0;
}
accuracy: 1
time_ms: 150
memory_kb: 56000
final_rank: 1

code_snippet: #include<bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 4000000000000000000 //オーバーフローに注意
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;



#define SIZE 100005
#define MAX 60

enum Type{
	X,
	Y,
};

struct Info{

	ll a,b,m;
};

ll H,W,N,num_Q;
ll POW[MAX+1];
ll MOVE[2][SIZE][MAX+1];
Info info[SIZE];

int main(){

	POW[0] = 1;
	for(int i = 1; i <= MAX; i++){

		POW[i] = POW[i-1]*2;
	}

	scanf("%lld %lld %lld %lld",&H,&W,&N,&num_Q);

	ll minus[2] = {0,0};

	ll sum_M = 0;

	for(ll i = 0; i < N; i++){

		scanf("%lld %lld %lld",&info[i].a,&info[i].b,&info[i].m);

		minus[X] += info[i].m*info[i].b;
		minus[Y] += info[i].m*info[i].a;

		sum_M += info[i].m;
	}

	minus[X] %= W;
	minus[Y] %= H;

	for(int loop = 0; loop < 2; loop++){

		ll mod;
		if(loop == 0){

			mod = W;
		}else{

			mod = H;
		}

		MOVE[loop][0][0] = (-minus[loop]+mod)%mod;
		for(ll i = 1; i <= mod-1; i++){ //1ターン後の座標

			MOVE[loop][i][0] = (i+i*sum_M-minus[loop]+mod)%mod;
		}

		for(ll p = 1; p <= MAX; p++){
			for(ll i = 0; i <= mod-1; i++){

				MOVE[loop][i][p] = MOVE[loop][MOVE[loop][i][p-1]][p-1];
			}
		}
	}

	ll row,col,turn;
	for(ll loop = 0; loop < num_Q; loop++){

		scanf("%lld %lld %lld",&row,&col,&turn);

		ll ans_row = row,ans_col = col;
		for(ll a = MAX; a >= 0; a--){
			if(turn < POW[a])continue;

			ans_row = MOVE[Y][ans_row][a];
			ans_col = MOVE[X][ans_col][a];

			turn -= POW[a];
		}

		printf("%lld %lld\n",ans_row,ans_col);
	}


	return 0;
}
accuracy: 1
time_ms: 320
memory_kb: 91600
final_rank: 5

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

#define int long long
int d[61][223456][2];//d[i][j][k]:jが2^iターン後にいる場所 k:0でx

signed main(){
  int h,w,n,q;cin>>h>>w>>n>>q;
  int asum=0,bsum=0,mhsum=0,mwsum=0;
  while(n--){
    int a,b,m;cin>>a>>b>>m;
    (asum+=a*m)%=h;
    (bsum+=b*m)%=w;
    (mhsum+=m)%=h;
    (mwsum+=m)%=w;
  }
  for(int i=0;i<w;i++)d[0][i][0]=(i+i*mwsum-bsum+w)%w;
  for(int i=0;i<h;i++)d[0][i][1]=(i+i*mhsum-asum+h)%h;
  for(int j=1;j<61;j++){
    for(int i=0;i<w;i++)
      d[j][i][0]=d[j-1][d[j-1][i][0]][0];
    for(int i=0;i<h;i++)
      d[j][i][1]=d[j-1][d[j-1][i][1]][1];
  }
  vector<pair<int,int>> ans;
  while(q--){
    int y,x,k;cin>>y>>x>>k;
    for(int i=60;i>=0;i--)
      if((k>>i)&1){
        x=d[i][x][0];
        y=d[i][y][1];
      }
    ans.emplace_back(y,x);
  }
  for(auto p:ans)cout<<p.first<<" "<<p.second<<endl;
}
accuracy: 1
time_ms: 390
memory_kb: 98372
final_rank: 10

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

#define int long long
int d[61][223456][2];//d[i][j][k]:jが2^iターン後にいる場所 k:0でx

signed main(){
  int h,w,n,q;cin>>h>>w>>n>>q;
  int asum=0,bsum=0,mhsum=0,mwsum=0;
  while(n--){
    int a,b,m;cin>>a>>b>>m;
    (asum+=a*m)%=h;
    (bsum+=b*m)%=w;
    (mhsum+=m)%=h;
    (mwsum+=m)%=w;
  }
  for(int i=0;i<w;i++)d[0][i][0]=(i+i*mwsum-bsum+w)%w;
  for(int i=0;i<h;i++)d[0][i][1]=(i+i*mhsum-asum+h)%h;
  for(int j=1;j<61;j++){
    for(int i=0;i<w;i++)
      d[j][i][0]=d[j-1][d[j-1][i][0]][0];
    for(int i=0;i<h;i++)
      d[j][i][1]=d[j-1][d[j-1][i][0]][1];
  }
  vector<pair<int,int>> ans;
  while(q--){
    int y,x,k;cin>>y>>x>>k;
    for(int i=60;i>=0;i--){
      if((k>>i)&1){
        x=d[i][x][0];
        y=d[i][y][1];
      }
    }
    ans.emplace_back(y,x);
  }
  for(auto p:ans)cout<<p.first<<" "<<p.second<<endl;
}
accuracy: 0.047619047619047616
time_ms: 340
memory_kb: 65144
final_rank: 19

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

#define int long long
int d[61][223456][2];//d[i][j][k]:jが2^iターン後にいる場所 k:0でx

signed main(){
  int h,w,n,q;cin>>h>>w>>n>>q;
  int asum=0,bsum=0,mhsum=0,mwsum=0;
  while(n--){
    int a,b,m;cin>>a>>b>>m;
    (asum+=a*m)%=h;
    (bsum+=b*m)%=w;
    (mhsum+=m)%=h;
    (mwsum+=m)%=w;
  }
  for(int i=0;i<w;i++)d[0][i][0]=(i+i*mwsum-bsum+w)%w;
  for(int i=0;i<h;i++)d[0][i][1]=(i+i*mhsum-asum+h)%h;
  for(int j=1;j<60;j++){
    for(int i=0;i<w;i++)
      d[j][i][0]=d[j-1][d[j-1][i][0]][0];
    for(int i=0;i<h;i++)
      d[j][i][1]=d[j-1][d[j-1][i][0]][1];
  }
  vector<pair<int,int>> ans;
  while(q--){
    int y,x,k;cin>>y>>x>>k;
    for(int i=60;i>=0;i--){
      if((k>>i)&1){
        x=d[i][x][0];
        y=d[i][y][1];
      }
    }
    ans.emplace_back(y,x);
  }
  for(auto p:ans)cout<<p.first<<" "<<p.second<<endl;
}
accuracy: 0.047619047619047616
time_ms: 330
memory_kb: 64204
final_rank: 18

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

#define int long long
int d[60][223456][2];//d[i][j][k]:jが2^iターン後にいる場所 k:0でx

signed main(){
  int h,w,n,q;cin>>h>>w>>n>>q;
  int asum=0,bsum=0,mhsum=0,mwsum=0;
  while(n--){
    int a,b,m;cin>>a>>b>>m;
    (asum+=a*m)%=h;
    (bsum+=b*m)%=w;
    (mhsum+=m)%=h;
    (mwsum+=m)%=w;
  }
  for(int i=0;i<w;i++)d[0][i][0]=(i+i*mwsum-bsum+w)%w;
  for(int i=0;i<h;i++)d[0][i][1]=(i+i*mhsum-asum+h)%h;
  for(int j=1;j<60;j++){
    for(int i=0;i<w;i++)
      d[j][i][0]=d[j-1][d[j-1][i][0]][0];
    for(int i=0;i<h;i++)
      d[j][i][1]=d[j-1][d[j-1][i][0]][1];
  }
  while(q--){
    int y,x,k;cin>>y>>x>>k;
    for(int i=60;i>=0;i--){
      if((k>>i)&1){
        x=d[i][x][0];
        y=d[i][y][1];
      }
    }
    cout<<y<<" "<<x<<endl;
  }
}
accuracy: 0.047619047619047616
time_ms: 340
memory_kb: 62440
final_rank: 16

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

#define int long long
int d[60][223456][2];//d[i][j][k]:jが2^iターン後にいる場所 k:0でx

signed main(){
  int h,w,n,q;cin>>h>>w>>n>>q;
  int asum=0,bsum=0,msum=0;
  while(n--){
    int a,b,m;cin>>a>>b>>m;
    (asum+=a*m)%=h;
    (bsum+=b*m)%=w;
    msum+=m;
  }
  for(int i=0;i<w;i++)d[0][i][0]=(i+i*msum-bsum+w)%w;
  for(int i=0;i<h;i++)d[0][i][1]=(i+i*msum-asum+h)%h;
  for(int j=1;j<60;j++){
    for(int i=0;i<w;i++)
      d[j][i][0]=d[j-1][d[j-1][i][0]][0];
    for(int i=0;i<h;i++)
      d[j][i][1]=d[j-1][d[j-1][i][0]][1];
  }
  while(q--){
    int y,x,k;cin>>y>>x>>k;
    for(int i=60;i>=0;i--){
      if((k>>i)&1){
        x=d[i][x][0];
        y=d[i][y][1];
      }
    }
    cout<<y<<" "<<x<<endl;
  }
}
accuracy: 0.047619047619047616
time_ms: 340
memory_kb: 62444
final_rank: 17

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

#define int long long
int d[60][223456][2];//d[i][j][k]:jが2^iターン後にいる場所 k:0でx

signed main(){
  int h,w,n,q;cin>>h>>w>>n>>q;
  int asum=0,bsum=0,msum=0;
  while(n--){
    int a,b,m;cin>>a>>b>>m;
    (asum+=a*m)%=w;
    (bsum+=b*m)%=h;
    msum+=m;
  }
  for(int i=0;i<w;i++)d[0][i][0]=(i+i*msum-asum+w)%w;
  for(int i=0;i<h;i++)d[0][i][1]=(i+i*msum-bsum+h)%h;
  for(int j=1;j<60;j++){
    for(int i=0;i<w;i++)
      d[j][i][0]=d[j-1][d[j-1][i][0]][0];
    for(int i=0;i<h;i++)
      d[j][i][1]=d[j-1][d[j-1][i][0]][1];
  }
  while(q--){
    int y,x,k;cin>>y>>x>>k;
    for(int i=60;i>=0;i--){
      if((k>>i)&1){
        x=d[i][x][0];
        y=d[i][y][1];
      }
    }
    cout<<y<<" "<<x<<endl;
  }
}
accuracy: 0.047619047619047616
time_ms: 340
memory_kb: 62340
final_rank: 15

code_snippet: #include <bits/stdc++.h>
using namespace std;
template <class T> inline bool chmax(T &a, T b) {
    if(a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <class T> inline bool chmin(T &a, T b) {
    if(a > b) {
        a = b;
        return 1;
    }
    return 0;
}
#define DEBUG
#ifdef DEBUG
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
    os << '(' << p.first << ',' << p.second << ')';
    return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
    os << '{';
    for(int i = 0; i < (int)v.size(); i++) {
        if(i) {
            os << ',';
        }
        os << v[i];
    }
    os << '}';
    return os;
}
void debugg() { cerr << endl; }
template <class T, class... Args>
void debugg(const T &x, const Args &... args) {
    cerr << " " << x;
    debugg(args...);
}
#define debug(...)                                                             \
    cerr << __LINE__ << " [" << #__VA_ARGS__ << "]: ", debugg(__VA_ARGS__)
#define dump(x) cerr << __LINE__ << " " << #x << " = " << (x) << endl
#else
#define debug(...) (void(0))
#define dump(x) (void(0))
#endif

struct Setup {
    Setup() {
        cin.tie(0);
        ios::sync_with_stdio(false);
        cout << fixed << setprecision(15);
    }
} __Setup;

using ll = long long;
#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()
const int INF = 1 << 30;
const ll LLINF = 1LL << 60;
constexpr int MOD = 1000000007;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

//-------------------------------------

int main() {
    ll H, W, N, Q;
    cin >> H >> W >> N >> Q;
    ll sum_am = 0;
    ll sum_bm = 0;
    ll sum_m = 0;
    for(ll i = 0; i < N; i++) {
        ll a, b, m;
        cin >> a >> b >> m;
        (sum_am += a * m % H) %= H;
        (sum_bm += b * m % W) %= W;
        sum_m += m;
    }
    vector duby(60, vector<ll>(H));
    vector dubx(60, vector<ll>(W));
    for(ll i = 0; i < H; i++) {
        duby[0][i] = ((sum_m + 1) % H * i % H - sum_am % H + H) % H;
    }
    for(ll i = 0; i < W; i++) {
        dubx[0][i] = ((sum_m + 1) % W * i % W - sum_bm % W + W) % W;
    }
    for(ll k = 1; k < 60; k++) {
        for(ll i = 0; i < H; i++) {
            duby[k][i] = duby[k - 1][duby[k - 1][i]];
        }
        for(ll i = 0; i < W; i++) {
            dubx[k][i] = dubx[k - 1][dubx[k - 1][i]];
        }
    }
    while(Q--) {
        ll c, d, k;
        cin >> c >> d >> k;
        for(ll i = 0; i < 60; i++) {
            if((1LL << i) & k) {
                c = duby[i][c];
                d = dubx[i][d];
            }
        }
        cout << c << " " << d << "\n";
    }
}
accuracy: 1
time_ms: 230
memory_kb: 89896
final_rank: 3

code_snippet: #include <bits/stdc++.h>
using namespace std;
template <class T> inline bool chmax(T &a, T b) {
    if(a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <class T> inline bool chmin(T &a, T b) {
    if(a > b) {
        a = b;
        return 1;
    }
    return 0;
}
#define DEBUG
#ifdef DEBUG
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
    os << '(' << p.first << ',' << p.second << ')';
    return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
    os << '{';
    for(int i = 0; i < (int)v.size(); i++) {
        if(i) {
            os << ',';
        }
        os << v[i];
    }
    os << '}';
    return os;
}
void debugg() { cerr << endl; }
template <class T, class... Args>
void debugg(const T &x, const Args &... args) {
    cerr << " " << x;
    debugg(args...);
}
#define debug(...)                                                             \
    cerr << __LINE__ << " [" << #__VA_ARGS__ << "]: ", debugg(__VA_ARGS__)
#define dump(x) cerr << __LINE__ << " " << #x << " = " << (x) << endl
#else
#define debug(...) (void(0))
#define dump(x) (void(0))
#endif

struct Setup {
    Setup() {
        cin.tie(0);
        ios::sync_with_stdio(false);
        cout << fixed << setprecision(15);
    }
} __Setup;

using ll = long long;
#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()
const int INF = 1 << 30;
const ll LLINF = 1LL << 60;
constexpr int MOD = 1000000007;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

//-------------------------------------

int main() {
    ll H, W, N, Q;
    cin >> H >> W >> N >> Q;
    ll sum_am = 0;
    ll sum_bm = 0;
    ll sum_m = 0;
    for(ll i = 0; i < N; i++) {
        ll a, b, m;
        cin >> a >> b >> m;
        (sum_am += a * m % H) %= H;
        (sum_bm += b * m % W) %= W;
        sum_m += m;
    }
    vector duby(60, vector<ll>(H));
    vector dubx(60, vector<ll>(W));
    for(ll i = 0; i < H; i++) {
        duby[0][i] = ((sum_m + 1) % H * i % H - sum_am % H + H) % H;
    }
    for(ll i = 0; i < W; i++) {
        dubx[0][i] = ((sum_m + 1) % H * i % W - sum_bm % W + W) % W;
    }
    for(ll k = 1; k < 60; k++) {
        for(ll i = 0; i < H; i++) {
            duby[k][i] = duby[k - 1][duby[k - 1][i]];
        }
        for(ll i = 0; i < W; i++) {
            dubx[k][i] = dubx[k - 1][dubx[k - 1][i]];
        }
    }
    while(Q--) {
        ll c, d, k;
        cin >> c >> d >> k;
        for(ll i = 0; i < 60; i++) {
            if((1LL << i) & k) {
                c = duby[i][c];
                d = dubx[i][d];
            }
        }
        cout << c << " " << d << "\n";
    }
}
accuracy: 0.047619047619047616
time_ms: 200
memory_kb: 60300
final_rank: 13

code_snippet: #include <bits/stdc++.h>
using namespace std;
template <class T> inline bool chmax(T &a, T b) {
    if(a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <class T> inline bool chmin(T &a, T b) {
    if(a > b) {
        a = b;
        return 1;
    }
    return 0;
}
#define DEBUG
#ifdef DEBUG
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
    os << '(' << p.first << ',' << p.second << ')';
    return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
    os << '{';
    for(int i = 0; i < (int)v.size(); i++) {
        if(i) {
            os << ',';
        }
        os << v[i];
    }
    os << '}';
    return os;
}
void debugg() { cerr << endl; }
template <class T, class... Args>
void debugg(const T &x, const Args &... args) {
    cerr << " " << x;
    debugg(args...);
}
#define debug(...)                                                             \
    cerr << __LINE__ << " [" << #__VA_ARGS__ << "]: ", debugg(__VA_ARGS__)
#define dump(x) cerr << __LINE__ << " " << #x << " = " << (x) << endl
#else
#define debug(...) (void(0))
#define dump(x) (void(0))
#endif

struct Setup {
    Setup() {
        cin.tie(0);
        ios::sync_with_stdio(false);
        cout << fixed << setprecision(15);
    }
} __Setup;

using ll = long long;
#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()
const int INF = 1 << 30;
const ll LLINF = 1LL << 60;
constexpr int MOD = 1000000007;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

//-------------------------------------

int main() {
    ll H, W, N, Q;
    cin >> H >> W >> N >> Q;
    ll sum_am = 0;
    ll sum_bm = 0;
    ll sum_m = 0;
    for(int i = 0; i < N; i++) {
        ll a, b, m;
        cin >> a >> b >> m;
        sum_am += a * m % H;
        sum_bm += b * m % W;
        sum_m += m;
    }
    vector duby(60, vector<ll>(H));
    vector dubx(60, vector<ll>(W));
    for(int i = 0; i < H; i++) {
        duby[0][i] = ((sum_m + 1) % H * i % H - sum_am % H + H) % H;
    }
    for(int i = 0; i < W; i++) {
        dubx[0][i] = ((sum_m + 1) % H * i % W - sum_bm % W + W) % W;
    }
    for(int k = 1; k < 60; k++) {
        for(int i = 0; i < H; i++) {
            duby[k][i] = duby[k - 1][duby[k - 1][i]];
        }
        for(int i = 0; i < W; i++) {
            dubx[k][i] = dubx[k - 1][dubx[k - 1][i]];
        }
    }
    while(Q--) {
        ll c, d, k;
        cin >> c >> d >> k;
        for(int i = 0; i < 60; i++) {
            if(k >> i & 1) {
                c = duby[i][c];
                d = dubx[i][d];
            }
        }
        cout << c << " " << d << "\n";
    }
}
accuracy: 0.047619047619047616
time_ms: 200
memory_kb: 60280
final_rank: 12

code_snippet: #include<iostream>
#include<string>
#include<iomanip>
#include<cmath>
#include<vector>
#include<algorithm>

using namespace std;

#define int long long
#define endl "\n"

constexpr long long INF = (long long)1e18;
constexpr long long MOD = 1000000007; 

#define a first.first
#define b first.second
#define m second

signed main(){
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	cout<<fixed<<setprecision(10);
	
	int H, W, N, Q;
	int summ = 0;
	vector<pair<pair<int,int>,int>> in;
	vector<int> tableh, tablew;
	vector<vector<int>> tableh2, tablew2;
	
	cin>>H>>W>>N>>Q;
	
	in.resize(N);
	
	for(int i = 0; i < N; i++){
		cin>>in[i].a>>in[i].b>>in[i].m;
		
		summ += in[i].m;
	}
	
	tableh.resize(H);
	tablew.resize(W);
	
	tableh2.resize(65, vector<int>(H));
	tablew2.resize(65, vector<int>(W));
	
	for(int i = 0; i < N; i++){
		tableh[0] += (H - (in[i].a * in[i].m) % H) % H;
		tableh[0] %= H;
		
		tablew[0] += (W - (in[i].b * in[i].m) % W) % W;
		tablew[0] %= W;
	}
	
	for(int i = 1; i < H; i++){
		tableh[i] = tableh[i-1] + summ % H;
		tableh[i] %= H;
	}
	
	for(int i = 1; i < W; i++){
		tablew[i] = tablew[i-1] + summ % W;
		tablew[i] %= W;
	}
	
	for(int i = 0; i < H; i++){
		tableh[i] += i;
		tableh[i] %= H;
		
		tableh2[0][i] = tableh[i];
	}
	
	for(int i = 0; i < W; i++){
		tablew[i] += i;
		tablew[i] %= W;
		
		tablew2[0][i] = tablew[i];
	}
	
	for(int i = 1; i < tableh2.size(); i++){
		for(int j = 0; j < H; j++){
			tableh2[i][j] = tableh2[i-1][tableh2[i-1][j]];
		}
	}
	
	for(int i = 1; i < tablew2.size(); i++){
		for(int j = 0; j < W; j++){
			tablew2[i][j] = tablew2[i-1][tablew2[i-1][j]];
		}
	}
	
	for(int i = 0; i < Q; i++){
		int c, d, k;
		int y = 0, x = 0;
		
		cin>>c>>d>>k;
		
		y = c, x = d;
		
		for(int j = 62; j >= 0; j--){
			if(k&(1ll<<j)) {
				y = tableh2[j][y];
			}
		}
		
		for(int j = 62; j >= 0; j--){
			if(k&(1ll<<j)) {
				x = tablew2[j][x];
			}
		}
		
		cout<<y<<" "<<x<<endl;
	}
	
	
	return 0;
}
accuracy: 1
time_ms: 370
memory_kb: 99204
final_rank: 9

code_snippet: #include <bits/stdc++.h>
//#include <chrono>
//#pragma GCC optimize("Ofast")
using namespace std;
#define reps(i,s,n) for(int i = s; i < n; i++)
#define rep(i,n) reps(i,0,n)
#define Rreps(i,n,e) for(int i = n - 1; i >= e; --i)
#define Rrep(i,n) Rreps(i,n,0)
#define ALL(a) a.begin(), a.end()
#define fi first
#define se second

using ll = long long;
using vec = vector<ll>;
using mat = vector<vec>;

ll N,M,H,W,Q,K,A,B;
string S;
typedef pair<ll, ll> P;
const ll INF = (1LL<<60);

class mint{
    public:
        ll x;
        long long mod;
        mint(){x = 0;}
        mint(ll _x, ll _mod = -1) : x(_x){
            if(_mod != -1) set_mod(_mod);
        }
        void set_mod(ll _mod){
            mod = _mod;
            x = (x < 0 ? ((x += (LLONG_MAX / mod) * mod) < 0 ? x + (LLONG_MAX / mod) * mod : x) : x);
            if(x >= mod) x %= mod;
        }
        mint operator-(){
            return {x == 0 ? 0 : mod - x, mod};
        }

        //"a.mod % mod == 0"
        mint& operator+=(mint a){
            a.set_mod(mod);
            if((x += a.x) >= mod) x -= mod;
            return *this;
        }
        mint operator+(const mint& a) const{
            mint res(*this);
            return res += a;
        }
        mint& operator-=(mint a){
            a.set_mod(mod);
            if((x -= a.x) < 0) x += mod;
            return *this;
        }
        mint operator-(const mint& a) const{
            mint res(*this);
            return res -= a;
        }
        mint& operator*=(mint a){
            a.set_mod(mod);
            (x *= a.x)%=mod;
            return *this;
        }
        mint operator*(const mint& a) const{
            mint res(*this);
            return res *= a;
        }
        mint pow(unsigned long long pw) const{
            mint res(1, mod), comp(*this);
            while(pw){
                if(pw&1) res *= comp;
                comp *= comp;
                pw >>= 1;
            }
            return res;
        }
        //以下、modが素数のときのみ
        mint inv() const{
            mint res(*this);
            return res.pow(mod - 2);
        }
        mint& operator/=(mint a){
            a.set_mod(mod);
            (x *= a.inv().x)%=mod;
            return *this;
        }
        mint operator/(const mint &a) const{
            mint res(*this);
            return res /= a;
        }
};
ostream& operator<<(ostream& os, const mint& a){
    os << a.x;
    return os;
}
typedef vector<mint> vm;


int main() {
    cin>>H>>W>>N>>Q;
    mint msum(0, H * W), ysum(0, H), xsum(0, W);
    rep(i, N){
        int a, b, m;
        cin>>a>>b>>m;
        msum += m;
        ysum -= a * m;
        xsum -= b * m;
    }
    mat dbl_x(60, vec(W)), dbl_y(60, vec(H));
    rep(x, W) {
        dbl_x[0][x] = xsum.x;
        xsum += msum + 1;
    }
    rep(y, H){
        dbl_y[0][y] = ysum.x;
        ysum += msum.x + 1;
    }
    reps(k, 1, 60) {
        rep(x, W) dbl_x[k][x] = dbl_x[k-1][dbl_x[k-1][x]];
        rep(y, H) dbl_y[k][y] = dbl_y[k-1][dbl_y[k-1][y]];
    }
    rep(_, Q){
        int c, d;
        ll k;
        cin>>c>>d>>k;
        Rrep(i, 60){
            if((k>>i)&1){
                c = dbl_y[i][c];
                d = dbl_x[i][d];
            }
        }
        cout<<c<<' '<<d<<endl;
    }
}
accuracy: 1
time_ms: 410
memory_kb: 89892
final_rank: 8

code_snippet: #include <bits/stdc++.h>
//#include <chrono>
//#pragma GCC optimize("Ofast")
using namespace std;
#define reps(i,s,n) for(int i = s; i < n; i++)
#define rep(i,n) reps(i,0,n)
#define Rreps(i,n,e) for(int i = n - 1; i >= e; --i)
#define Rrep(i,n) Rreps(i,n,0)
#define ALL(a) a.begin(), a.end()
#define fi first
#define se second

using ll = long long;
using vec = vector<ll>;
using mat = vector<vec>;

ll N,M,H,W,Q,K,A,B;
string S;
typedef pair<ll, ll> P;
const ll INF = (1LL<<60);

class mint{
    public:
        ll x;
        long long mod;
        mint(){x = 0;}
        mint(ll _x, ll _mod = -1) : x(_x){
            if(_mod != -1) set_mod(_mod);
        }
        void set_mod(ll _mod){
            mod = _mod;
            x = (x < 0 ? ((x += (LLONG_MAX / mod) * mod) < 0 ? x + (LLONG_MAX / mod) * mod : x) : x);
            if(x >= mod) x %= mod;
        }
        mint operator-(){
            return {x == 0 ? 0 : mod - x, mod};
        }
        mint& operator+=(const mint& a){
            if((x += a.x) >= mod) x -= mod;
            return *this;
        }
        mint operator+(const mint& a) const{
            mint res(*this);
            return res += a;
        }
        mint& operator-=(const mint& a){
            if((x -= a.x) < 0) x += mod;
            return *this;
        }
        mint operator-(const mint& a) const{
            mint res(*this);
            return res -= a;
        }
        mint& operator*=(const mint& a){
            (x *= a.x)%=mod;
            return *this;
        }
        mint operator*(const mint& a) const{
            mint res(*this);
            return res *= a;
        }
        mint pow(unsigned long long pw) const{
            mint res(1, mod), comp(*this);
            while(pw){
                if(pw&1) res *= comp;
                comp *= comp;
                pw >>= 1;
            }
            return res;
        }
        //以下、modが素数のときのみ
        mint inv() const{
            mint res(*this);
            return res.pow(mod - 2);
        }
        mint& operator/=(const mint &a){
            (x *= a.inv().x)%=mod;
            return *this;
        }
        mint operator/(const mint &a) const{
            mint res(*this);
            return res /= a;
        }
};
ostream& operator<<(ostream& os, const mint& a){
    os << a.x;
    return os;
}
typedef vector<mint> vm;


int main() {
    cin>>H>>W>>N>>Q;
    mint msum(0, H * W), ysum(0, H), xsum(0, W);
    rep(i, N){
        int a, b, m;
        cin>>a>>b>>m;
        msum += m;
        ysum -= (a * m)%H;
        xsum -= (b * m)%W;
    }
    mat dbl_x(60, vec(W)), dbl_y(60, vec(H));
    rep(x, W) {
        dbl_x[0][x] = xsum.x;
        xsum += (msum.x + 1)%W;
    }
    rep(y, H){
        dbl_y[0][y] = ysum.x;
        ysum += (msum.x + 1)%H;
    }
    reps(k, 1, 60) {
        rep(x, W) dbl_x[k][x] = dbl_x[k-1][dbl_x[k-1][x]];
        rep(y, H) dbl_y[k][y] = dbl_y[k-1][dbl_y[k-1][y]];
    }
    rep(_, Q){
        int c, d;
        ll k;
        cin>>c>>d>>k;
        Rrep(i, 60){
            if((k>>i)&1){
                c = dbl_y[i][c];
                d = dbl_x[i][d];
            }
        }
        cout<<c<<' '<<d<<endl;
    }
}
accuracy: 1
time_ms: 400
memory_kb: 89976
final_rank: 7

code_snippet: #include <iostream>
#include <string>
#include <vector>
#include <array>
#include <queue>
#include <deque>
#include <algorithm>
#include <set>
#include <map>
#include <bitset>
#include <cmath>
#include <functional>
#include <cassert>
#include <iomanip>
#define vll vector<ll>
#define vvvl vector<vvl>
#define vvl vector<vector<ll>>
#define VV(a, b, c, d) vector<vector<d>>(a, vector<d>(b, c))
#define VVV(a, b, c, d) vector<vvl>(a, vvl(b, vll (c, d)));
#define re(c, b) for(ll c=0;c<b;c++)
#define all(obj) (obj).begin(), (obj).end()
typedef long long int ll;
typedef long double ld;
using namespace std;

int main(){
  ll h, w, n, q;scanf("%lld %lld %lld %lld\n", &h, &w, &n, &q);
  ll A = 0, B = 0, C = 0, D = 0;
  for(int i=0;i<n;i++){
    ll a, b, m;scanf("%lld %lld %lld", &a, &b, &m);
    A = (A + m)%h;
    B = (B + (a * m))%h;
    C = (C + m)%w;
    D = (D + (b * m))%w;
  }

  //A:=∑mi
  //B:=∑aimi
  //C:=∑mj
  //D:=∑bjmj

  //y' = (1 + A)y - B mod h
  //x' = (1 + C)x - D mod w
  auto f = [&](ll y){return ((1+A)*y-B+h)%h;};
  auto g = [&](ll x){return ((1+C)*x-D+w)%w;};

  vvl Y = VV(h, 64, -1, ll);
  vvl X = VV(w, 64, -1, ll);

  for(int i=0;i<64;i++){
    if(i==0){
      for(ll j=0;j<h;j++) Y[j][0] = f(j);
      for(ll j=0;j<w;j++) X[j][0] = g(j);
    }else{
      for(int j=0;j<h;j++) Y[j][i] = Y[Y[j][i-1]][i-1];
      for(int j=0;j<w;j++) X[j][i] = X[X[j][i-1]][i-1];
    }
  }

  for(int i=0;i<q;i++){
    ll x, y, k;scanf("%lld %lld %lld", &y, &x, &k);
    int cnt = 0;
    while(k){
      if(k%2) y = Y[y][cnt], x = X[x][cnt];
      k/=2;
      cnt++;
    }
    printf("%lld %lld\n", y, x);
  }
}
accuracy: 1
time_ms: 380
memory_kb: 101872
final_rank: 11

code_snippet: #include <iostream>
#include <string>
#include <vector>
#include <array>
#include <queue>
#include <deque>
#include <algorithm>
#include <set>
#include <map>
#include <bitset>
#include <cmath>
#include <functional>
#include <cassert>
#include <iomanip>
#define vll vector<ll>
#define vvvl vector<vvl>
#define vvl vector<vector<ll>>
#define VV(a, b, c, d) vector<vector<d>>(a, vector<d>(b, c))
#define VVV(a, b, c, d) vector<vvl>(a, vvl(b, vll (c, d)));
#define re(c, b) for(ll c=0;c<b;c++)
#define all(obj) (obj).begin(), (obj).end()
typedef long long int ll;
typedef long double ld;
using namespace std;

int main(){
  ll h, w, n, q;scanf("%lld %lld %lld %lld\n", &h, &w, &n, &q);
  ll A = 0, B = 0, C = 0, D = 0;
  for(int i=0;i<n;i++){
    ll a, b, m;scanf("%lld %lld %lld", &a, &b, &m);
    A = (A + m)%h;
    B = (B + (a * m))%h;
    C = (C + m)%w;
    D = (D + (b * m))%w;
  }

  //A:=∑mi
  //B:=∑aimi
  //C:=∑mj
  //D:=∑bjmj

  //y' = (1 + A)y - B mod h
  //x' = (1 + C)x - D mod w
  auto f = [&](ll y){return ((1+A)*y-B+h)%h;};
  auto g = [&](ll x){return ((1+C)*x-D+w)%w;};

  vvl Y = VV(h, 64, -1, ll);
  vvl X = VV(w, 64, -1, ll);

  for(int i=0;i<64;i++){
    if(i==0){
      for(ll j=0;j<h;j++) Y[j][0] = f(j);
      for(ll j=0;j<w;j++) X[j][0] = g(j);
    }else{
      for(int j=0;j<h;j++) Y[j][i] = Y[Y[j][i-1]][i-1];
      for(int j=0;j<w;j++) X[j][i] = X[X[j][i-1]][i-1];
    }
  }

  for(int i=0;i<q;i++){
    if(i>5) break;
    ll x, y, k;scanf("%lld %lld %lld", &x, &y, &k);
    int cnt = 0;
    while(k){
      if(k%2) y = Y[y][cnt], x = X[x][cnt];
      k/=2;
      cnt++;
    }
    printf("%lld %lld\n", x, y);
  }
}
accuracy: 0.047619047619047616
time_ms: 100
memory_kb: 68312
final_rank: 14

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

using namespace std;

//----------------------- Print Function ----------------------//

inline void print() {
    cout << '\n';
}
template <typename First, typename... Rest>
void print(const First &first, const Rest &... rest) {
    cout << first << ' ';
    print(rest...);
}

template <typename T>
void print(const T &a) {
    for (auto e : a) cout << e << ' ';
    cout << '\n';
}

//------------------------- Libraries -------------------------//

//--------------------------- Solve ---------------------------//

void solve() {
    int H, W, N, Q; cin >> H >> W >> N >> Q;
    long long sum_am = 0, sum_bm = 0, sum_m = 0;
    for (int i = 0; i < N; i++) {
        int a, b, m; cin >> a >> b >> m;
        sum_am += a*m;
        sum_bm += b*m;
        sum_m += m;
    }

    vector<vector<long long> > doubling_h(61, vector<long long>(H));
    vector<vector<long long> > doubling_w(61, vector<long long>(W));
    for (int y = 0; y < H; y++) {
        doubling_h[0][y] = (y + y*sum_m - sum_am%H + H) % H;
    }
    for (int i = 1; i <= 60; i++) {
        for (int y = 0; y < H; y++) {
            doubling_h[i][y] = doubling_h[i-1][doubling_h[i-1][y]];
        }
    }

    for (int x = 0; x < W; x++) {
        doubling_w[0][x] = (x + x*sum_m - sum_bm%W + W) % W;
    }
    for (int i = 1; i <= 60; i++) {
        for (int x = 0; x < W; x++) {
            doubling_w[i][x] = doubling_w[i-1][doubling_w[i-1][x]];
        }
    }

    for (int q = 0; q < Q; q++) {
        long long c, d, k; cin >> c >> d >> k;
        int y = c, x = d;
        for (int i = 0; i < 61; i++) {
            if (k & (1LL<<i)) y = doubling_h[i][y];
            if (k & (1LL<<i)) x = doubling_w[i][x];
        }
        cout << y << ' ' << x << '\n';
    }
}

int main() {
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);

    solve();

    return 0;
}
accuracy: 1
time_ms: 220
memory_kb: 91192
final_rank: 4

code_snippet: #include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<utility>
#include<cassert>
#include<complex>
#include<numeric>
#include<array>
using namespace std;

//#define int long long
typedef long long ll;

typedef unsigned long long ul;
typedef unsigned int ui;
constexpr ll mod = 1000000007;
const ll INF = mod * mod;
typedef pair<int, int>P;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
#define all(v) (v).begin(),(v).end()
typedef pair<ll, ll> LP;
typedef long double ld;
typedef pair<ld, ld> LDP;
const ld eps = 1e-12;
const ld pi = acosl(-1.0);

ll mod_pow(ll x, ll n, ll m = mod) {
	ll res = 1;
	while (n) {
		if (n & 1)res = res * x % m;
		x = x * x % m; n >>= 1;
	}
	return res;
}
struct modint {
	ll n;
	modint() :n(0) { ; }
	modint(ll m) :n(m) {
		if (n >= mod)n %= mod;
		else if (n < 0)n = (n % mod + mod) % mod;
	}
	operator int() { return n; }
};
bool operator==(modint a, modint b) { return a.n == b.n; }
modint operator+=(modint& a, modint b) { a.n += b.n; if (a.n >= mod)a.n -= mod; return a; }
modint operator-=(modint& a, modint b) { a.n -= b.n; if (a.n < 0)a.n += mod; return a; }
modint operator*=(modint& a, modint b) { a.n = ((ll)a.n * b.n) % mod; return a; }
modint operator+(modint a, modint b) { return a += b; }
modint operator-(modint a, modint b) { return a -= b; }
modint operator*(modint a, modint b) { return a *= b; }
modint operator^(modint a, ll n) {
	if (n == 0)return modint(1);
	modint res = (a * a) ^ (n / 2);
	if (n % 2)res = res * a;
	return res;
}

ll inv(ll a, ll p) {
	return (a == 1 ? 1 : (1 - p * inv(p % a, a)) / a + p);
}
modint operator/(modint a, modint b) { return a * modint(inv(b, mod)); }

const int max_n = 1 << 19;
modint fact[max_n], factinv[max_n];
void init_f() {
	fact[0] = modint(1);
	for (int i = 0; i < max_n - 1; i++) {
		fact[i + 1] = fact[i] * modint(i + 1);
	}
	factinv[max_n - 1] = modint(1) / fact[max_n - 1];
	for (int i = max_n - 2; i >= 0; i--) {
		factinv[i] = factinv[i + 1] * modint(i + 1);
	}
}
modint comb(int a, int b) {
	if (a < 0 || b < 0 || a < b)return 0;
	return fact[a] * factinv[b] * factinv[a - b];
}

int xnex[1 << 17][60];
int ynex[1 << 17][60];
void solve(){
	int h, w, n, q; cin >> h >> w >> n >> q;
	vector<ll> a(n), b(n), m(n);
	rep(i, n) {
		cin >> a[i] >> b[i] >> m[i];
	}
	ll sa = 0;
	rep(i, n)sa += a[i] * m[i];
	ll sb = 0;
	rep(i, n)sb += b[i] * m[i];
	ll sm = 0;
	rep(i, n)sm += m[i];
	rep(i, h) {
		ll to = i + sm * i - sa;
		to %= h; if (to < 0)to += h;
		xnex[i][0] = to;
	}
	rep(j, w) {
		ll to = j + sm * j - sb;
		to %= w; if (to < 0)to += w;
		ynex[j][0] = to;
	}
	rep(j, 59) {
		rep(i, h) {
			xnex[i][j + 1] = xnex[xnex[i][j]][j];
		}
		rep(i, w) {
			ynex[i][j + 1] = ynex[ynex[i][j]][j];
		}
	}
	rep(i, q) {
		int c, d; ll k; cin >> c >> d >> k;
		rep(j, 60) {
			if (k & (1ll << j)) {
				c = xnex[c][j];
				d = ynex[d][j];
			}
		}
		cout << c << " " << d << "\n";
	}
}

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	//cout << fixed << setprecision(15);
	//init_f();
	//init();
	//expr();
	//int t; cin >> t; rep(i, t)
	solve();
	return 0;
}
accuracy: 1
time_ms: 280
memory_kb: 54720
final_rank: 2

code_snippet: #include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<complex>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<utility>
#include<tuple>
#include<cassert>
using namespace std;
typedef long long ll;
typedef unsigned int ui;
const ll INF = (ll)1000000007 * 1000000007;
typedef pair<int, int> P;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define Per(i,sta,n) for(int i=n-1;i>=sta;i--)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
typedef long double ld;
const ld eps = 1e-8;
const ld pi = acos(-1.0);
typedef pair<ll, ll> LP;
int dx[4]={1,-1,0,0};
int dy[4]={0,0,1,-1};
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;}

int h,w,n,q;
int c[100010],d[100010];ll t[100010];
int ans[100010][2];

vector<vector<ll>> mul(vector<vector<ll>> &A,vector<vector<ll>> &B,ll mo){
  int n=A.size();
  vector<vector<ll>> C(n,vector<ll>(n,0));
  rep(i,n){
    rep(j,n){
      rep(k,n){
        C[i][j]+=A[i][k]*B[k][j];
        C[i][j]%=mo;
      }
    }
  }
  rep(i,n){
    rep(j,n){
      C[i][j]+=mo;C[i][j]%=mo;
    }
  }
  return C;
}

vector<vector<ll>> pow(vector<vector<ll>> A,long long n,ll m) {
  vector<vector<ll>> R(A.size(),vector<ll>(A.size(),0));
  for (int i = 0; i < A.size(); ++i) R[i][i] = 1;
  while (n > 0) {
    if (n & 1) R = mul(R,A,m);
    A=mul(A,A,m);
    n>>=1;
  }
  return R;
}

void solve(){
  cin >> h >> w >> n >> q;
  ll A1=1,B1=0,A2=1,B2=0;
  rep(i,n){
    ll p,q,m;cin >> p >> q >> m;
    A1+=m;A2+=m;B1-=p*m;B2-=q*m;
  }
  rep(i,q){
    cin >> c[i] >> d[i] >> t[i];
  }
  vector<vector<ll>> A(2,vector<ll>(2));A[0][0]=A1%h;A[0][1]=B1%h;A[1][1]=1;
  rep(i,q){
    vector<vector<ll>> R=pow(A,t[i],h);
    ans[i][0]=(R[0][0]*c[i]+R[0][1])%h;
  }
  vector<vector<ll>> B(2,vector<ll>(2));B[0][0]=A2%w;B[0][1]=B2%w;B[1][1]=1;
  rep(i,q){
    vector<vector<ll>> R=pow(B,t[i],w);
    ans[i][1]=(R[0][0]*d[i]+R[0][1])%w;
  }
  rep(i,q) cout << ans[i][0] << " " << ans[i][1] << endl;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << fixed << setprecision(50);
    solve();
}
accuracy: 1
time_ms: 2410
memory_kb: 5704
final_rank: 6

code_snippet: #include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<complex>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<utility>
#include<tuple>
#include<cassert>
using namespace std;
typedef long long ll;
typedef unsigned int ui;
const ll INF = (ll)1000000007 * 1000000007;
typedef pair<int, int> P;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define Per(i,sta,n) for(int i=n-1;i>=sta;i--)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
typedef long double ld;
const ld eps = 1e-8;
const ld pi = acos(-1.0);
typedef pair<ll, ll> LP;
int dx[4]={1,-1,0,0};
int dy[4]={0,0,1,-1};
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;}

int h,w,n,q;
int c[100010],d[100010];ll t[100010];
int ans[100010][2];

vector<vector<ll>> mul(vector<vector<ll>> &A,vector<vector<ll>> &B,ll mo){
  int n=A.size();
  vector<vector<ll>> C(n,vector<ll>(n,0));
  rep(i,n){
    rep(j,n){
      rep(k,n){
        C[i][j]+=A[i][k]*B[k][j];
        C[i][j]%=mo;
      }
    }
  }
  rep(i,n){
    rep(j,n){
      C[i][j]+=mo;C[i][j]%=mo;
    }
  }
  return C;
}

vector<vector<ll>> pow(vector<vector<ll>> A,long long n,ll m) {
  vector<vector<ll>> R(A.size(),vector<ll>(A.size(),0));
  for (int i = 0; i < A.size(); ++i) R[i][i] = 1;
  while (n > 0) {
    if (n & 1) R = mul(R,A,m);
    A=mul(A,A,m);
    n>>=1;
  }
  return R;
}

void solve(){
  cin >> h >> w >> n >> q;
  ll A1=1,B1=0,A2=1,B2=0;
  rep(i,n){
    ll p,q,m;cin >> p >> q >> m;
    A1+=m;A2+=m;B1-=p*m;B2-=q*m;
  }
  rep(i,q){
    cin >> c[i] >> d[i] >> t[i];
  }
  vector<vector<ll>> A(2,vector<ll>(2));A[0][0]=A1;A[0][1]=B1;A[1][1]=1;
  rep(i,q){
    vector<vector<ll>> R=pow(A,t[i],h);
    ans[i][0]=(R[0][0]*c[i]+R[0][1])%h;
  }
  vector<vector<ll>> B(2,vector<ll>(2));B[0][0]=A2;B[0][1]=B2;B[1][1]=1;
  rep(i,q){
    vector<vector<ll>> R=pow(B,t[i],w);
    ans[i][1]=(R[0][0]*d[i]+R[0][1])%w;
  }
  rep(i,q) cout << ans[i][0] << " " << ans[i][1] << endl;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << fixed << setprecision(50);
    solve();
}
accuracy: 0.047619047619047616
time_ms: 1920
memory_kb: 5032
final_rank: 20