Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //#define _GLIBCXX_DEBUG
- #ifdef _DEBUG
- #include "opt.h"
- #endif
- #include <bits/stdc++.h>
- using namespace std;
- typedef long long ll;
- typedef pair<int, int> pii;
- typedef vector<int> vi;
- #define mk make_pair
- #define inb push_back
- #define X first
- #define Y second
- #define all(v) v.begin(), v.end()
- #define sqr(x) (x) * (x)
- int solve();
- int main()
- {
- //ios_base::sync_with_stdio(0);
- //cin.tie(0);
- #define TASK "carno"
- #ifndef _DEBUG
- //freopen(TASK".in", "r", stdin), freopen(TASK".out", "w", stdout);
- #endif
- solve();
- }
- const int SZ = (int)2e6 + 3;
- struct HashTable
- {
- vector<pii> a;
- HashTable() { a.resize(SZ); };
- int getpos(int x)
- {
- int i = x % SZ;
- while (a[i].X && a[i].X != x)
- {
- ++i;
- if (i == SZ)
- i = 0;
- }
- return i;
- }
- int swappos(int x, int y)
- {
- int posx = getpos(x);
- int posy = getpos(y);
- int valx = a[posx].Y;
- int valy = a[posy].Y;
- if (!valx)
- valx = x, a[posx] = mk(x, x);
- if (!valy)
- valy = y, a[posy] = mk(y, y);
- int ans = abs(valx - valy);
- swap(a[posx].Y, a[posy].Y);
- return ans;
- }
- };
- int solve()
- {
- int n;
- n = readInt();
- HashTable T;
- for (int i = 0; i < n; ++i)
- {
- int x, y;
- x = readInt();
- y = readInt();
- writeInt(T.swappos(x, y), '\n');
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement