Advertisement
K_Y_M_bl_C

Untitled

Sep 25th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.20 KB | None | 0 0
  1. //#define _GLIBCXX_DEBUG
  2. #ifdef _DEBUG
  3. #include "opt.h"
  4. #endif
  5. #include <bits/stdc++.h>
  6.  
  7. using namespace std;
  8.  
  9. typedef long long ll;
  10. typedef pair<int, int> pii;
  11. typedef vector<int> vi;
  12.  
  13. #define mk make_pair
  14. #define inb push_back
  15. #define X first
  16. #define Y second
  17. #define all(v) v.begin(), v.end()
  18. #define sqr(x) (x) * (x)
  19.  
  20. int solve();
  21.  
  22. int main()
  23. {
  24. //ios_base::sync_with_stdio(0);
  25. //cin.tie(0);
  26. #define TASK "carno"
  27. #ifndef _DEBUG
  28. //freopen(TASK".in", "r", stdin), freopen(TASK".out", "w", stdout);
  29. #endif
  30. solve();
  31. }
  32.  
  33. const int SZ = (int)2e6 + 3;
  34.  
  35. struct HashTable
  36. {
  37. vector<pii> a;
  38. HashTable() { a.resize(SZ); };
  39. int getpos(int x)
  40. {
  41. int i = x % SZ;
  42. while (a[i].X && a[i].X != x)
  43. {
  44. ++i;
  45. if (i == SZ)
  46. i = 0;
  47. }
  48. return i;
  49. }
  50. int swappos(int x, int y)
  51. {
  52. int posx = getpos(x);
  53. int posy = getpos(y);
  54. int valx = a[posx].Y;
  55. int valy = a[posy].Y;
  56. if (!valx)
  57. valx = x, a[posx] = mk(x, x);
  58. if (!valy)
  59. valy = y, a[posy] = mk(y, y);
  60. int ans = abs(valx - valy);
  61. swap(a[posx].Y, a[posy].Y);
  62. return ans;
  63. }
  64. };
  65.  
  66. int solve()
  67. {
  68. int n;
  69. n = readInt();
  70. HashTable T;
  71. for (int i = 0; i < n; ++i)
  72. {
  73. int x, y;
  74. x = readInt();
  75. y = readInt();
  76. writeInt(T.swappos(x, y), '\n');
  77. }
  78. return 0;
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement