两个平衡树是复制粘贴的,看一个就可以,当然,也有可能我太弱了,主函数出锅
#include <iostream>
#include <cstdlib>
#include <cmath>
using namespace std;
struct fhq
{
int k, p, s, l, r;
} man[800100], pet[800100];
//man
int root, cnt;
inline void update (int tp)
{
man[tp].s = man[man[tp].l].s + man[man[tp].r].s + 1;
}
inline void split (int tp, int k, int &x, int &y)
{
if (!tp) x = y = 0;
else
{
if (man[tp].k <= k) x = tp, split (man[tp].r, k, man[x].r, y);
else y = tp, split (man[tp].l, k, x, man[y].l);
update (tp);
}
}
inline int merge (int x, int y)
{
if (!x || !y) return x | y;
if (man[x].p < man[y].p)
{
man[x].r = merge (man[x].r, y);
update (x); return x;
}
else
{
man[y].l = merge (x, man[y].l);
update (y); return y;
}
}
inline int create (int k)
{
man[++cnt].k = k;
man[cnt].p = rand ();
man[cnt].s = 1;
return cnt;
}
inline void insert (int k)
{
int x, y;
split (root, k - 1, x, y);
root = merge (merge (x, create (k)), y);
}
inline int pre (int k)
{
int x, y;
split (root, k, root, y);
x = root;
while (man[x].r) x = man[x].r;
int ans = man[x].k;
root = merge (root, y);
return ans;
}
inline int suc (int k)
{
int x, y;
split (root, k - 1, x, root);
y = root;
while (man[y].r) x = man[y].r;
int ans = man[y].k;
root = merge (x, root);
return ans;
}
inline void remove (int k)
{
int x, y, z;
split (root, k, x, y);
split (x, k - 1, x, z);
z = merge (man[z].l, man[z].r);
root = merge (merge (x, z), y);
}
inline int size ()
{
return man[root].s;
}
//pet
int _root, _cnt;
inline void _update (int tp)
{
pet[tp].s = pet[pet[tp].l].s + pet[pet[tp].r].s + 1;
}
inline void _split (int tp, int k, int &x, int &y)
{
if (!tp) x = y = 0;
else
{
if (pet[tp].k <= k) x = tp, _split (pet[tp].r, k, pet[tp].r, y);
else y = tp, _split (pet[tp].l, k, x, pet[tp].l);
_update (tp);
}
}
inline int _merge (int x, int y)
{
if (!x || !y) return x | y;
if (pet[x].p < pet[y].p)
{
pet[x].r = _merge (pet[x].r, y);
_update (x); return x;
}
else
{
pet[y].l = _merge (x, pet[y].l);
_update (y); return y;
}
}
inline int _create (int k)
{
pet[++_cnt].k = k;
pet[_cnt].p = rand ();
pet[_cnt].s = 1;
return _cnt;
}
inline void _insert (int k)
{
int x, y;
_split (_root, k - 1, x, y);
_root = _merge (_merge (x, _create (k)), y);
}
inline int _pre (int k)
{
int x, y;
_split (_root, k, _root, y);
x = _root;
while (pet[x].r) x = pet[x].r;
int ans = pet[x].k;
_root = _merge (_root, y);
return ans;
}
inline int _suc (int k)
{
int x, y;
_split (_root, k - 1, x, _root);
y = _root;
while (pet[y].r) x = pet[y].r;
int ans = pet[y].k;
_root = _merge (x, _root);
return ans;
}
inline void _remove (int k)
{
int x, y, z;
_split (_root, k, x, y);
_split (x, k - 1, x, z);
z = _merge (pet[z].l, pet[z].r);
_root = _merge (_merge (x, z), y);
}
inline int _size ()
{
return pet[_root].s;
}
int main ()
{
srand (114514);
int n, ans = 0, mod = 1000000;
cin >> n;
for (int i = 1; i <= n; ++i)
{
int a, b;
cin >> a >> b;
if (a == 0)
{
if (size ())
{
int p = pre (b);
int s = suc (b);
if (!p) p = 1e9;
if (!s) s = 1e9;
if (abs (b - p) <= abs (s - b))
{
ans = (ans + abs (b - p)) % mod;
remove (p);
// cout << "DEBUG: " << p << "\n";
}
else
{
ans = (ans + abs (s - b)) % mod;
remove (s);
// cout << "DEBUG: " << s << "\n";
}
}
else _insert (b);
}
else
{
if (_size ())
{
int p = _pre (b);
int s = _suc (b);
if (!p) p = 1e9;
if (!s) s = 1e9;
if (abs (b - p) <= abs (s - b))
{
ans = (ans + abs (b - p)) % mod;
_remove (p);
// cout << "DEBUG: " << p << "\n";
}
else
{
ans = (ans + abs (s - b)) % mod;
_remove (s);
// cout << "DEBUG: " << s << "\n";
}
}
else insert (b);
}
}
cout << ans;
return 0;
}