国家级示范建设网站,jquery 网站根目录,学网页设计在哪学,温州知名网站1208: [HNOI2004]宠物收养所 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 9775 Solved: 3918[Submit][Status][Discuss]Description 最近#xff0c;阿Q开了一间宠物收养所。收养所提供两种服务#xff1a;收养被主人遗弃的宠物和让新的主人领养这些宠物。每个领养者都… 1208: [HNOI2004]宠物收养所 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 9775 Solved: 3918[Submit][Status][Discuss] Description 最近阿Q开了一间宠物收养所。收养所提供两种服务收养被主人遗弃的宠物和让新的主人领养这些宠物。每个领养者都希望领养到自己满意的宠物阿Q根据领养者的要求通过他自己发明的一个特殊的公式得出该领养者希望领养的宠物的特点值aa是一个正整数a2^31而他也给每个处在收养所的宠物一个特点值。这样他就能够很方便的处理整个领养宠物的过程了宠物收养所总是会有两种情况发生被遗弃的宠物过多或者是想要收养宠物的人太多而宠物太少。 1 被遗弃的宠物过多时假若到来一个领养者这个领养者希望领养的宠物的特点值为a那么它将会领养一只目前未被领养的宠物中特点值最接近a的一只宠物。任何两只宠物的特点值都不可能是相同的任何两个领养者的希望领养宠物的特点值也不可能是一样的如果有两只满足要求的宠物即存在两只宠物他们的特点值分别为a-b和ab那么领养者将会领养特点值为a-b的那只宠物。 2 收养宠物的人过多假若到来一只被收养的宠物那么哪个领养者能够领养它呢能够领养它的领养者是那个希望被领养宠物的特点值最接近该宠物特点值的领养者如果该宠物的特点值为a存在两个领养者他们希望领养宠物的特点值分别为a-b和ab那么特点值为a-b的那个领养者将成功领养该宠物。 一个领养者领养了一个特点值为a的宠物而它本身希望领养的宠物的特点值为b那么这个领养者的不满意程度为abs(a-b)。【任务描述】你得到了一年当中领养者和被收养宠物到来收养所的情况希望你计算所有收养了宠物的领养者的不满意程度的总和。这一年初始时收养所里面既没有宠物也没有领养者。 Input 第一行为一个正整数nn80000表示一年当中来到收养所的宠物和领养者的总数。接下来的n行按到来时间的先后顺序描述了一年当中来到收养所的宠物和领养者的情况。每行有两个正整数a, b其中a0表示宠物a1表示领养者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 依旧模板 #includeiostream
#includecstdio
#includecstring
using namespace std;
typedef long long ll;
const int MOD1000000;
const int MAXN1000009;
int Abs(int a) { return a0?a:-a; }
int fa[MAXN],ch[MAXN][2],key[MAXN],cnt[MAXN],size[MAXN],root,sz;
void init()
{rootsz0;memset(ch,0,sizeof(ch));memset(fa,0,sizeof(fa));memset(cnt,0,sizeof(cnt));memset(size,0,sizeof(size));
}
inline void clear(int x) { fa[x]ch[x][0]ch[x][1]cnt[x]size[x]0; }
inline int get(int x) { return ch[fa[x]][1]x; }
inline void update(int x)
{if(x){size[x]cnt[x];if(ch[x][0]) size[x]size[ch[x][0]];if(ch[x][1]) size[x]size[ch[x][1]];}
}
inline void rotate(int x)
{int fatherfa[x],ffatherfa[father],whichget(x);ch[father][which]ch[x][!which];fa[ch[father][which]]father;ch[x][!which]father;fa[father]x;fa[x]ffather;if(ffather) ch[ffather][ch[ffather][1]father]x;update(father);update(x);
}
inline void splay(int x)
{for(int father;(fatherfa[x]);rotate(x))if(fa[father])rotate((get(x)get(father)?father:x));rootx;
}
inline void insert(int x)
{if(root0) { rootsz;fa[sz]ch[sz][0]ch[sz][1]0;cnt[sz]size[sz]1;key[sz]x;return; }int nowroot,father0;while(1){if(key[now]x) { cnt[now];update(now);update(father);splay(now);return; }fathernow;nowch[father][key[now]x];if(now0){sz;fa[sz]father;ch[father][key[father]x]sz;ch[sz][0]ch[sz][1]0;cnt[sz]size[sz]1;key[sz]x;update(father);splay(sz);return;}}
}
inline int find(int x)//找到x的位置
{int nowroot,ans0;while(1){if(xkey[now]) nowch[now][0];else{if(ch[now][0]) anssize[ch[now][0]];if(xkey[now]) { splay(now);return ans1; }anscnt[now];nowch[now][1];}}
}
inline int rank(int x)//找到排名为x的数
{int nowroot;while(1){if(ch[now][0]xsize[ch[now][0]]) nowch[now][0];else{int tmp(ch[now][0]?size[ch[now][0]]:0)cnt[now];if(xtmp) return key[now];xx-tmp;nowch[now][1];}}
}
inline int pre()//找前驱
{int nowch[root][0];while(ch[now][1]) nowch[now][1];return now;
}
inline int suf()//找后继
{int nowch[root][1];while(ch[now][0]) nowch[now][0];return now;
}
inline void del(int x)//删去一个x
{find(x);if(cnt[root]1) { cnt[root]--;update(root);return; }else if(!ch[root][0]!ch[root][1]) { rootsz0;clear(root);return; }else if(!ch[root][0]){int oldrootroot;rootch[root][1];fa[root]0;clear(oldroot);return;}else if(!ch[root][1]){int oldrootroot;rootch[root][0];fa[root]0;clear(oldroot);return;}int leftbigpre(),oldrootroot;splay(leftbig);ch[root][1]ch[oldroot][1];fa[ch[root][1]]root;clear(oldroot);update(root);
}
int main()
{//freopen(in.txt,r,stdin);int n,a,b,flag0,sum0;ll ans0;init();scanf(%d,n);while(n--){scanf(%d%d,a,b);if(flaga||sum0){insert(b);flaga;sum;}else if(flag(!a)sum){insert(b);if(cnt[root]1) del(b);else{int tmp1pre(),tmp2suf();tmp1(tmp10?-1:key[tmp1]);tmp2(tmp20?-1:key[tmp2]);if(tmp1-1) { ansAbs(b-tmp2);del(tmp2); }else if(tmp2-1) { ansAbs(b-tmp1);del(tmp1); }else if(Abs(b-tmp1)Abs(b-tmp2)){ansAbs(b-tmp1);del(tmp1);}else{ansAbs(b-tmp2);del(tmp2);}ans%MOD;}del(b);sum--;}}printf(%lld\n,ans);return 0;
} 转载于:https://www.cnblogs.com/--ZHIYUAN/p/7811565.html