Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include <iostream>
- #include <cstdio>
- #include <string>
- #include <stdlib.h>
- #include <set>
- #include <vector>
- #include <list>
- #include <algorithm>
- #include <regex>
- #include <math.h>
- #include <map>
- #include <iterator>
- #include <limits>
- #include <numeric>
- #include <bitset>
- #include <unordered_map>
- #include <unordered_set>
- #include <iomanip>
- #include <queue>
- #include <deque>
- #include <stack>
- #include <array>
- #include <valarray>
- #include <complex>
- using namespace std;
- typedef array<int, 2> hv;
- typedef int integer;
- #define int long long
- #define double long double
- #define make(type, x) type x; cin>>x
- #define make2(type, x, y) type x, y; cin>>x>>y
- #define make3(type, x, y, z) type x, y, z; cin>>x>>y>>z
- #define make4(type, x, y, z, q) type x, y, z, q; cin>>x>>y>>z>>q
- #define ALL(x) (x).begin(), (x).end()
- #define FORIT(p) for(auto it = p.begin(); it != p.end(); it++)
- #define FOR(b) for(int i = 0; i <(b); i++)
- #define VI vector<int>
- #define VVI vector<vector<int>>
- #define PII pair<int, int>
- #define PPII pair<PII, int>
- void Solve() {
- make3(int, n, m, k);
- vector<PII> a(k);
- vector<int> ans(k, -1);
- multimap<int, int> ds, dm;
- FOR(k) {
- cin >> a[i].first >> a[i].second;
- ds.insert({ a[i].first + a[i].second, i });
- dm.insert({ a[i].first - a[i].second, i });
- }
- int stI = 0, stJ = 0;
- int time = 0;
- bool isS = false;
- while (true) {
- int nextI, nextJ;
- if (isS) {
- int sm = stI + stJ;
- int i1, j1, i2, j2, i3, j3, i4, j4;
- i1 = 0, j1 = sm;
- i2 = sm, j2 = 0;
- i3 = n; j3 = sm - n;
- i4 = sm - m; j4 = m;
- if (j1 > m) {}
- if (sm > n) { i2 = n; j2 = sm - n; }
- else { i2 = sm; j2 = 0; }
- if (i1 == stI) { i1 = i2; j1 = j2; }
- nextI = i1; nextJ = j1;
- while (ds.count(sm)) {
- auto it = ds.find(sm);
- int index = it->second;
- dm.erase(sm);
- if (ans[index] != -1) continue;
- ans[index] = time + abs(stI - a[index].first);
- }
- }
- else {
- int d = stI - stJ;
- int i1, i2, j1, j2;
- if (m < n - d) { i2 = d + m; j2 = m; }
- else { i2 = n; j2 = n - d; }
- if (d < 0) { i1 = 0; j1 = -d; }
- else { i1 = d; j1 = 0; }
- if (i1 == stI) { i1 = i2; j1 = j2; }
- nextI = i1; nextJ = j1;
- while (dm.count(d)) {
- auto it = dm.find(d);
- int index = it->second;
- dm.erase(it);
- if (ans[index] != -1) continue;
- ans[index] = time + abs(stI - a[index].first);
- }
- }
- time += abs(nextI - stI);
- stI = nextI;
- stJ = nextJ;
- if (stI == 0 && (stJ == 0 || stJ == m)) break;
- if (stI == n && (stJ == 0 || stJ == m)) break;
- isS = !isS;
- }
- FOR(k) cout << ans[i] << endl;
- }
- integer main() {
- ios_base::sync_with_stdio(0);
- cin.tie();
- #if _DEBUG
- freopen("input.txt", "r", stdin);
- freopen("output.txt", "w", stdout);
- Solve();
- while (!cin.eof()) {
- string s = "_";
- do getline(cin, s);
- while (!cin.eof() && !s.empty());
- if (cin.eof()) return 0;
- FOR(3) cout << endl;
- Solve();
- }
- #else
- Solve();
- #endif
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement