Description:

凡凡开了一间宠物收养场。收养场提供两种服务:收养被主人遗弃的宠物和让新的主人领养这些宠物。

每个领养者都希望领养到自己满意的宠物,凡凡根据领养者的要求通过他自己发明的一个特殊的公式,得出该领养者希望领养的宠物的特点值a(a是一个正整数,a<2^31),而他也给每个处在收养场的宠物一个特点值。这样他就能够很方便的处理整个领养宠物的过程了,宠物收养场总是会有两种情况发生:被遗弃的宠物过多或者是想要收养宠物的人太多,而宠物太少。

被遗弃的宠物过多时,假若到来一个领养者,这个领养者希望领养的宠物的特点值为a,那么它将会领养一只目前未被领养的宠物中特点值最接近a的一只宠物。(任何两只宠物的特点值都不可能是相同的,任何两个领养者的希望领养宠物的特点值也不可能是一样的)如果有两只满足要求的宠物,即存在两只宠物他们的特点值分别为a-b和a+b,那么领养者将会领养特点值为a-b的那只宠物。

收养宠物的人过多,假若到来一只被收养的宠物,那么哪个领养者能够领养它呢?能够领养它的领养者,是那个希望被领养宠物的特点值最接近该宠物特点值的领养者,如果该宠物的特点值为a,存在两个领养者他们希望领养宠物的特点值分别为a-b和a+b,那么特点值为a-b的那个领养者将成功领养该宠物。

一个领养者领养了一个特点值为a的宠物,而它本身希望领养的宠物的特点值为b,那么这个领养者的不满意程度为abs(a-b)。

你得到了一年当中,领养者和被收养宠物到来收养所的情况,请你计算所有收养了宠物的领养者的不满意程度的总和。这一年初始时,收养所里面既没有宠物,也没有领养者。

Input:

第一行为一个正整数n,n<=80000,表示一年当中来到收养场的宠物和领养者的总数。接下来的n行,按到来时间的先后顺序描述了一年当中来到收养场的宠物和领养者的情况。每行有两个正整数a, b,其中a=0表示宠物,a=1表示领养者,b表示宠物的特点值或是领养者希望领养宠物的特点值。(同一时间呆在收养所中的,要么全是宠物,要么全是领养者,这些宠物和领养者的个数不会超过10000个)

Output:

仅有一个正整数,表示一年当中所有收养了宠物的领养者的不满意程度的总和mod 1000000以后的结果。

Sample Input:

5
0 2
0 4
1 3
1 2
1 5

Sample Output:

3

注:abs(3-2) + abs(2-4)=3,
最后一个领养者没有宠物可以领养。

题目链接 题目链接

若店里有没被领取的宠物且有领养者来领宠物则领养者领取和他期望值之差最小的宠物

若店里有没领取宠物的领养者且有宠物来认领领养者则宠物认领和他期望值之差最小的领养者

所以用伸展树维护序列,并快速地找到其前驱和后继(这里为了练习 <math> <semantics> <mrow> <mi> S </mi> <mi> p </mi> <mi> l </mi> <mi> a </mi> <mi> y </mi> </mrow> <annotation encoding="application&#47;x&#45;tex"> Splay </annotation> </semantics> </math>Splay 不考虑 <math> <semantics> <mrow> <mi> s </mi> <mi> e </mi> <mi> t </mi> </mrow> <annotation encoding="application&#47;x&#45;tex"> set </annotation> </semantics> </math>set

由于题目保证同一时间呆在收养所中的,要么全是宠物,要么全是领养者,所以可以只用一棵 <math> <semantics> <mrow> <mi> S </mi> <mi> p </mi> <mi> l </mi> <mi> a </mi> <mi> y </mi> </mrow> <annotation encoding="application&#47;x&#45;tex"> Splay </annotation> </semantics> </math>Splay <math> <semantics> <mrow> <mi> T </mi> <mi> r </mi> <mi> e </mi> <mi> e </mi> </mrow> <annotation encoding="application&#47;x&#45;tex"> Tree </annotation> </semantics> </math>Tree 来进行维护(用一个标记变量记录此时 <math> <semantics> <mrow> <mi> S </mi> <mi> p </mi> <mi> l </mi> <mi> a </mi> <mi> y </mi> </mrow> <annotation encoding="application&#47;x&#45;tex"> Splay </annotation> </semantics> </math>Splay <math> <semantics> <mrow> <mi> T </mi> <mi> r </mi> <mi> e </mi> <mi> e </mi> </mrow> <annotation encoding="application&#47;x&#45;tex"> Tree </annotation> </semantics> </math>Tree 存储地是宠物还是领养者)

AC代码:

#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
const int maxn = 1e5 + 5;
const int mod = 1000000;

class splay_tree {
  public:
    int rt, tot;
    int fa[maxn], son[maxn][2];
    int val[maxn], cnt[maxn];
    int sz[maxn];

    splay_tree() {
      rt = tot = 0;
    }

    void Push(int o) {
      sz[o] = sz[son[o][0]] + sz[son[o][1]] + cnt[o];
    }

    bool Get(int o) {
      return o == son[fa[o]][1];
    }

    void Clear(int o) {
      son[o][0] = son[o][1] = fa[o] = val[o] = sz[o] = cnt[o] = 0;
    }

    void Rotate(int o) {
      int p = fa[o], q = fa[p], ck = Get(o);
      son[p][ck] = son[o][ck ^ 1];
      fa[son[o][ck ^ 1]] = p;
      son[o][ck ^ 1] = p;
      fa[p] = o; fa[o] = q;
      if (q) son[q][p == son[q][1]] = o;
      Push(p); Push(o);
    }

    void Splay(int o) {
      for (int f = fa[o]; f = fa[o], f; Rotate(o))
        if (fa[f]) Rotate(Get(o) == Get(f) ? f : o);
      rt = o;
    }

    void Insert(int x) {
      if (!rt) {
        val[++tot] = x;
        cnt[tot]++;
        rt = tot;
        Push(rt);
        return;
      }
      int cur = rt, f = 0;
      while (true) {
        if (val[cur] == x) {
          cnt[cur]++;
          Push(cur); Push(f);
          Splay(cur);
          break;
        }
        f = cur;
        cur = son[cur][val[cur] < x];
        if (!cur) {
          val[++tot] = x;
          cnt[tot]++;
          fa[tot] = f;
          son[f][val[f] < x] = tot;
          Push(tot); Push(f);
          Splay(tot);
          break;
        }
      }
    }

    int GetRank(int x) {
      int ans = 0, cur = rt;
      while (true) {
        if (x < val[cur]) cur = son[cur][0];
        else {
          ans += sz[son[cur][0]];
          if (x == val[cur]) {
            Splay(cur);
            return ans + 1;
          }
          ans += cnt[cur];
          cur = son[cur][1];
        }
      }
    }

    int GetKth(int k) {
      int cur = rt;
      while (true) {
        if (son[cur][0] && k <= sz[son[cur][0]]) cur = son[cur][0];
        else {
          k -= cnt[cur] + sz[son[cur][0]];
          if (k <= 0) return cur;
          cur = son[cur][1];
        }
      }
    }

    // after insert
    int GetPrev() {
      int cur = son[rt][0];
      while (son[cur][1]) cur = son[cur][1];
      return cur;
    }

    int GetNext() {
      int cur = son[rt][1];
      while (son[cur][0]) cur = son[cur][0];
      return cur;
    }

    void Delete(int x) {
      GetRank(x);
      if (cnt[rt] > 1) {
        cnt[rt]--;
        Push(rt);
        return;
      }
      if (!son[rt][0] && !son[rt][1]) {
        Clear(rt);
        rt = 0;
        return;
      }
      if (!son[rt][0]) {
        int cur = rt;
        rt = son[rt][1];
        fa[rt] = 0;
        Clear(cur);
        return;
      }
      if (!son[rt][1]) {
        int cur = rt;
        rt = son[rt][0];
        fa[rt] = 0;
        Clear(cur);
        return;
      }
      int p = GetPrev(), cur = rt;
      Splay(p);
      fa[son[cur][1]] = p;
      son[p][1] = son[cur][1];
      Clear(cur);
      Push(rt);
    }
};

splay_tree spt;

int main() {
  ios::sync_with_stdio(false); cin.tie(0);
  int n; cin >> n;
  int cur = 0, ans = 0;
  spt.Insert(inf); spt.Insert(-inf);
  for (int i = 0, a, b; i < n; ++i) {
    cin >> a >> b;
    if (cur == 0) {
      spt.Insert(b);
      if (a == 0) ++cur;
      else --cur;
    }
    else if (cur > 0) {
      if (a == 0) {
        spt.Insert(b);
        ++cur;
      }
      else {
        spt.Insert(b);
        int pre = spt.val[spt.GetPrev()], nxt = spt.val[spt.GetNext()];
        if (abs(b - pre) <= abs(b - nxt)) {
          ans = (ans + abs(b - pre)) % mod;
          spt.Delete(pre);
        }
        else {
          ans = (ans + abs(b - nxt)) % mod;
          spt.Delete(nxt);
        }
        spt.Delete(b);
        --cur;
      }
    }
    else {
      if (a == 0) {
        spt.Insert(b);
        int pre = spt.val[spt.GetPrev()], nxt = spt.val[spt.GetNext()];
        if (abs(b - pre) <= abs(b - nxt)) {
          ans = (ans + abs(b - pre)) % mod;
          spt.Delete(pre);
        }
        else {
          ans = (ans + abs(b - nxt)) % mod;
          spt.Delete(nxt);
        }
        spt.Delete(b);
        ++cur;
      }
      else {
        spt.Insert(b);
        --cur;
      }
    }
  }
  cout << ans << endl;
  return 0;
}