Advertisement
Guest User

Untitled

a guest
Jan 21st, 2018
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.98 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2.  
  3. #include <iostream>
  4. #include <cstdio>
  5. #include <string>
  6. #include <stdlib.h>
  7. #include <set>
  8. #include <vector>
  9. #include <list>
  10. #include <algorithm>
  11. #include <regex>
  12. #include <math.h>
  13. #include <map>
  14. #include <iterator>
  15. #include <limits>
  16. #include <numeric>
  17. #include <bitset>
  18. #include <unordered_map>
  19. #include <unordered_set>
  20. #include <iomanip>
  21. #include <queue>
  22. #include <deque>
  23. #include <stack>
  24. #include <array>
  25. #include <valarray>
  26. #include <complex>
  27.  
  28.  
  29. using namespace std;
  30.  
  31. typedef array<int, 2> hv;
  32. typedef int integer;
  33. #define int long long
  34. #define double long double
  35. #define make(type, x) type x; cin>>x
  36. #define make2(type, x, y) type x, y; cin>>x>>y
  37. #define make3(type, x, y, z) type x, y, z; cin>>x>>y>>z
  38. #define make4(type, x, y, z, q) type x, y, z, q; cin>>x>>y>>z>>q
  39. #define ALL(x) (x).begin(), (x).end()
  40. #define FORIT(p) for(auto it = p.begin(); it != p.end(); it++)
  41. #define FOR(b) for(int i = 0; i <(b); i++)
  42. #define VI vector<int>
  43. #define VVI vector<vector<int>>
  44. #define PII pair<int, int>
  45. #define PPII pair<PII, int>
  46.  
  47. void Solve() {
  48. make3(int, n, m, k);
  49. vector<PII> a(k);
  50. vector<int> ans(k, -1);
  51. multimap<int, int> ds, dm;
  52. FOR(k) {
  53. cin >> a[i].first >> a[i].second;
  54. ds.insert({ a[i].first + a[i].second, i });
  55. dm.insert({ a[i].first - a[i].second, i });
  56. }
  57. int stI = 0, stJ = 0;
  58. int time = 0;
  59. bool isS = false;
  60. while (true) {
  61. int nextI, nextJ;
  62. if (isS) {
  63. int sm = stI + stJ;
  64. int i1, j1, i2, j2, i3, j3, i4, j4;
  65. i1 = 0, j1 = sm;
  66. i2 = sm, j2 = 0;
  67. i3 = n; j3 = sm - n;
  68. i4 = sm - m; j4 = m;
  69. if (j1 > m) {}
  70. if (sm > n) { i2 = n; j2 = sm - n; }
  71. else { i2 = sm; j2 = 0; }
  72. if (i1 == stI) { i1 = i2; j1 = j2; }
  73. nextI = i1; nextJ = j1;
  74. while (ds.count(sm)) {
  75. auto it = ds.find(sm);
  76. int index = it->second;
  77. dm.erase(sm);
  78. if (ans[index] != -1) continue;
  79. ans[index] = time + abs(stI - a[index].first);
  80. }
  81. }
  82. else {
  83. int d = stI - stJ;
  84. int i1, i2, j1, j2;
  85. if (m < n - d) { i2 = d + m; j2 = m; }
  86. else { i2 = n; j2 = n - d; }
  87. if (d < 0) { i1 = 0; j1 = -d; }
  88. else { i1 = d; j1 = 0; }
  89. if (i1 == stI) { i1 = i2; j1 = j2; }
  90. nextI = i1; nextJ = j1;
  91. while (dm.count(d)) {
  92. auto it = dm.find(d);
  93. int index = it->second;
  94. dm.erase(it);
  95. if (ans[index] != -1) continue;
  96. ans[index] = time + abs(stI - a[index].first);
  97. }
  98. }
  99. time += abs(nextI - stI);
  100. stI = nextI;
  101. stJ = nextJ;
  102. if (stI == 0 && (stJ == 0 || stJ == m)) break;
  103. if (stI == n && (stJ == 0 || stJ == m)) break;
  104. isS = !isS;
  105. }
  106. FOR(k) cout << ans[i] << endl;
  107. }
  108.  
  109.  
  110. integer main() {
  111. ios_base::sync_with_stdio(0);
  112. cin.tie();
  113. #if _DEBUG
  114. freopen("input.txt", "r", stdin);
  115. freopen("output.txt", "w", stdout);
  116. Solve();
  117. while (!cin.eof()) {
  118. string s = "_";
  119. do getline(cin, s);
  120. while (!cin.eof() && !s.empty());
  121. if (cin.eof()) return 0;
  122. FOR(3) cout << endl;
  123. Solve();
  124. }
  125. #else
  126. Solve();
  127. #endif
  128. return 0;
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement