BZOJ1208: [HNOI2004]宠物收养所

1208: [HNOI2004]宠物收养所

Time Limit: 10 Sec  Memory Limit: 162 MB
Submit: 9992  Solved: 3990
[Submit][Status][Discuss]

Description

最 近,阿Q开了一间宠物收养所。收养所提供两种服务:收养被主人遗弃的宠物和让新的主人领养这些宠物。每个领养者都希望领养到自己满意的宠物,阿Q根据领养 者的要求通过他自己发明的一个特殊的公式,得出该领养者希望领养的宠物的特点值a(a是一个正整数,a<2^31),而他也给每个处在收养所的宠物 一个特点值。这样他就能够很方便的处理整个领养宠物的过程了,宠物收养所总是会有两种情况发生:被遗弃的宠物过多或者是想要收养宠物的人太多,而宠物太 少。 1. 被遗弃的宠物过多时,假若到来一个领养者,这个领养者希望领养的宠物的特点值为a,那么它将会领养一只目前未被领养的宠物中特点值最接近a的一只宠物。 (任何两只宠物的特点值都不可能是相同的,任何两个领养者的希望领养宠物的特点值也不可能是一样的)如果有两只满足要求的宠物,即存在两只宠物他们的特点 值分别为a-b和a+b,那么领养者将会领养特点值为a-b的那只宠物。 2. 收养宠物的人过多,假若到来一只被收养的宠物,那么哪个领养者能够领养它呢?能够领养它的领养者,是那个希望被领养宠物的特点值最接近该宠物特点值的领养 者,如果该宠物的特点值为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,最后一个领养者没有宠物可以领养)

HINT

Source

 

【题解】

splay卡时题,new_bzoj上过了,BZOJ上TLE,欢迎去bzoj.cf

 

  1 #include <iostream>
  2 #include <cstdio>
  3 #include <cstring>
  4 #include <cstdlib>
  5 #include <algorithm>
  6 #include <queue>
  7 #include <vector>
  8 #define min(a, b) ((a) < (b) ? (a) : (b))
  9 #define max(a, b) ((a) > (b) ? (a) : (b))
 10 #define abs(a) ((a) < 0 ? (-1 * (a)) : (a))
 11 #define son(x) ((x) == (ch[1][fa[(x)]]))
 12 inline void swap(int &a, int &b)
 13 {
 14     int tmp = a;a = b;b = tmp;
 15 }
 16 
 17 const int INF = 0x3f3f3f3f;
 18 const int MAXN = 800000 + 10;
 19 const int MOD = 1000000;
 20 
 21 int fa[MAXN], size[MAXN], data[MAXN], cnt[MAXN], ch[3][MAXN], root, nn;
 22 void pushup(int rt)
 23 {
 24     size[rt] = size[ch[0][rt]] + size[ch[1][rt]] + cnt[rt];
 25 }
 26 void rotate(int x)
 27 {
 28     int y = fa[x], z = fa[y], b = son(x), c = son(y), a = ch[!b][x];
 29     if(z) ch[c][z] = x; else root = x; fa[x] = z;
 30     if(a) fa[a] = y; ch[b][y] = a;
 31     ch[!b][x] = y,fa[y] = x;
 32     pushup(y),pushup(x);
 33 }
 34 void splay(int x, int i)
 35 {
 36     while(fa[x] != i)
 37     {
 38         int y = fa[x], z = fa[y];
 39         if(z == i) rotate(x);
 40         else
 41             if(son(x) == son(y))rotate(y), rotate(x);
 42             else rotate(x), rotate(x);
 43     }
 44 }
 45 void insert(int &rt, int x)
 46 {
 47     if(rt == 0)
 48     {
 49         rt = ++nn, data[nn] = x, size[nn] = cnt[nn] = 1;
 50         return;
 51     }
 52     if(data[rt] == x)
 53     {
 54         ++cnt[rt], ++size[rt];
 55         return;
 56     }
 57     if(x < data[rt]) insert(ch[0][rt], x), fa[ch[0][rt]] = rt;
 58     else insert(ch[1][rt], x), fa[ch[1][rt]] = rt;
 59     pushup(rt);
 60 }
 61 int getpre(int rt, int x)
 62 {
 63     int p = rt, ans = -1;
 64     while(p)
 65         if(x < data[p]) p = ch[0][p];
 66         else ans = p, p = ch[1][p];
 67     return ans;
 68 }
 69 int getnxt(int rt, int x)
 70 {
 71     int p = rt, ans = -1;
 72     while(p)
 73         if(x > data[p]) p = ch[1][p];
 74         else ans = p, p = ch[0][p];
 75     return ans;
 76 }
 77 int getmn(int rt)
 78 {
 79     int p = rt, ans = -1;
 80     while(p)
 81         ans = p, p = ch[0][p];
 82     return ans;
 83 }
 84 void del(int rt, int x)
 85 {
 86     if(data[rt] == x)
 87     {
 88         if(cnt[rt] > 1) -- cnt[rt], -- size[rt];
 89         else
 90         {
 91             splay(rt, 0);
 92             int p = getmn(ch[1][rt]);
 93             if(p != -1)
 94             {
 95                 splay(p, rt);
 96                 root = p, fa[p] = 0, ch[0][p] = ch[0][rt], fa[ch[0][rt]] = p;
 97                 pushup(p);
 98             }
 99             else root = ch[0][rt], fa[ch[0][rt]] = 0;
100         }
101         return;
102     }
103     if(x < data[rt]) del(ch[0][rt], x);
104     else del(ch[1][rt], x);
105     pushup(rt);
106 }
107 int n,tot;
108 long long ans;
109 
110 int main()
111 {
112     scanf("%d", &n);
113     int flag = -1;
114     for(register int i = 1;i <= n;++ i)
115     {
116         int a,b;
117         if(scanf("%d%d", &a, &b) == EOF) break;
118         if(a == flag) 
119         {
120             insert(root, b);
121             ++ tot;
122             continue;
123         }
124         else if(flag == -1) 
125         {
126             flag = a;
127             insert(root, b);
128             ++ tot;
129             continue;
130         }
131         else -- tot;
132         if(tot == 0) flag = -1;
133             int pre = getpre(root, b), nxt = getnxt(root, b);
134             if(pre == -1 && nxt == -1) continue;
135             else if(pre == -1)
136             {
137                 ans += (abs(b - data[nxt]) % MOD);
138                 if(ans >= MOD) ans -= MOD;
139                 del(root, data[nxt]);
140             }
141             else if(nxt == -1)
142             {
143                 ans += (abs(b - data[pre]) % MOD);
144                 if(ans >= MOD) ans -= MOD;
145                 del(root, data[pre]);
146             }
147             else if(abs(b - data[nxt]) < abs(b - data[pre]))
148             {
149                 ans += (abs(b - data[nxt]) % MOD);
150                 if(ans >= MOD) ans -= MOD;
151                 del(root, data[nxt]);
152             }
153             else
154             {
155                 ans += (abs(b - data[pre]) % MOD);
156                 if(ans >= MOD) ans -= MOD;
157                 del(root, data[pre]);
158             }
159     }
160     printf("%lld", ans);
161     return 0;
162 } 
BZOJ1208

 

posted @ 2017-12-11 23:39  嘒彼小星  阅读(173)  评论(0编辑  收藏  举报