Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdio>
- #include <cstdlib>
- #include <algorithm>
- #include <vector>
- #include <set>
- using namespace std;
- #define x first
- #define y second
- #define mp make_pair
- #define pb push_back
- typedef pair <int, int> pii;
- typedef pair <int, pii> pkk;
- const int N = 1e5 + 5;
- int n, m, k;
- set <int> v[N];
- int u[N];
- pii d[N];
- int main()
- {
- freopen("towers.in", "r", stdin);
- freopen("towers.out", "w", stdout);
- cin >> n >> m >> k;
- for (int i = 0; i < k; i++)
- {
- int x, y;
- scanf("%d%d", &x, &y);
- x--; y--;
- v[x].insert(y);
- }
- cin >> k;
- for (int i = 0; i < k; i++)
- {
- int a, b, c, d1;
- scanf("%d%d%d%d", &a, &b, &c, &d1);
- u[0] = a;
- for (int j = 1; j < n; j++)
- u[j] = (1LL * u[j - 1] * b + c) % d1;
- d[0] = mp((1LL * u[n - 1] * b + c) % d1, 0);
- for (int j = 1; j < m; j++)
- d[j] = mp((1LL * d[j - 1].x * b + c) % d1, j);
- /*for (int j = 0; j < n; j++)
- cout << u[j] << " ";
- cout << endl;
- for (int j = 0; j < m; j++)
- cout << d[j].x << " ";
- cout << endl;*/
- sort(d, d + m);
- pkk ans = mp(-1, mp(-1, -1));
- for (int j = 0; j < n; j++)
- {
- for (int z = m - 1; z >= 0; z--)
- if (v[j].find(d[z].y) == v[j].end())
- {
- if (ans.x < (u[j] + d[z].x))
- ans = mp(u[j] + d[z].x, mp(j, d[z].y));
- break;
- }
- }
- if (ans.x == -1) cout << -1 << endl;
- else cout << ans.y.x + 1 << " " << ans.y.y + 1 << endl;
- }
- return 0;
- }
- /*
- 4 5 3
- 1 2
- 1 3
- 4 5
- 1
- 7 2 4 199
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement