[HNOI2004]宠物收养所 题解
一杯茶,一包烟,一道水题调一天
这题一眼看上去就是个裸板子对吧
本来以为要两棵splay,读了一下题发现店里只能有一种生物(人/宠物)
所以记录一下当前店里的状态就行了
老年手速20min过编译,
嗯?
检查了30min发现没取mod
嗯?
检查调试对拍了2h+30min重构代码
发现Splay打错了
int nxt()
{
if(cnt[root]>)return root;
int now=son[root][];
while(son[now][])now=son[now][];
return now;
}
//√
int nxt()
{
if(cnt[root]>)return root;
int now=son[root][];
while(son[now][])now=son[now][];
return now;
}
//×
我打的时候在想什么啊啊啊啊啊身败名裂辽
总结:数据结构题如果迟迟调不过 不如检查一下是否在数据结构本身上犯了白痴错误
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>
using namespace std;
const int N=,mod=1e6,inf=0x3f3f3f3f;
int fa[N],cnt[N],son[N][],size[N],key[N],type,root;
int n,num[];
void clear(int x)
{
fa[x]=cnt[x]=son[x][]=son[x][]=size[x]=key[x]=;
}
bool judge(int x)
{
return son[fa[x]][]==x;
}
void up(int x)
{
if(x)
{
size[x]=cnt[x];
if(son[x][])size[x]+=size[son[x][]];
if(son[x][])size[x]+=size[son[x][]];
}
}
void rotate(int x)
{
int old=fa[x],oldf=fa[old],lr=judge(x);
son[old][lr]=son[x][lr^];
fa[son[old][lr]]=old;
son[x][lr^]=old;
fa[old]=x;
fa[x]=oldf;
if(oldf)son[oldf][son[oldf][]==old]=x;
up(old);up(x);
}
void splay(int x)
{
for(int f;f=fa[x];rotate(x))
if(fa[f])rotate(judge(x)==judge(f)?f:x);
root=x;
}
void ins(int x)
{
if(!root)
{
type++;
key[type]=x;
root=type;
cnt[type]=size[type]=;
fa[type]=son[type][]=son[type][]=;
return ;
}
int now=root,f=;
while()
{
if(x==key[now])
{
cnt[now]++;
up(now);
up(f);
splay(now);
return ;
}
f=now;now=son[now][key[now]<x];
if(!now)
{
type++;
size[type]=cnt[type]=;
son[type][]=son[type][]=;
son[f][x>key[f]]=type;
fa[type]=f;
key[type]=x;
up(f);splay(type);
return ;
}
}
}
int pre()
{
if(cnt[root]>)return root;
int now=son[root][];
while(son[now][])now=son[now][];
return now;
}
int nxt()
{
if(cnt[root]>)return root;
int now=son[root][];
while(son[now][])now=son[now][];
return now;
}
void changeroot(int x)
{
int now=root;
while()
{
if(x<key[now])now=son[now][];
else
{
if(x==key[now])
{
splay(now);
return ;
}
now=son[now][];
}
}
}
void del(int x)
{
changeroot(x);
if(cnt[root]>)
{
cnt[root]--;
up(root);
return ;
}
if(!son[root][]&&!son[root][])
{
clear(root);
root=;
return ;
}
if(!son[root][])
{
int old=root;
root=son[root][];
fa[root]=;
clear(old);
return ;
}
else if(!son[root][])
{
int old=root;
root=son[root][];
fa[root]=;
clear(root);
return ;
}
int old=root,L=pre();
splay(L);
son[root][]=son[old][];
fa[son[old][]]=root;
clear(old);
up(root);
}
inline int read()
{
int x=,f=;char ch=getchar();
while(ch<''||ch>'')
{if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<='')
{x=(x<<)+(x<<)+ch-'';ch=getchar();}
return x*f;
}
int now_=-;
int ans;
int main()
{
n=read();
ins(inf);ins(-inf);
for(int i=;i<=n;i++)
{
int op=read(),val=read();
//cout<<"root="<<root<<endl;
if(now_==-)
{
now_=op;
ins(val);
num[op]++;
continue;
}
if(now_==op)
{
ins(val);
num[op]++;
continue;
}
ins(val);
int pre_=pre(),nxt_=nxt(),preval=key[pre_],nxtval=key[nxt_];
del(val);
if(val-preval<=nxtval-val)ans+=(val-preval)%mod,del(preval),num[op^]--;
else ans+=(nxtval-val)%mod,del(nxtval),num[op^]--;
if(num[]==&&num[]==)now_=-;
ans%=mod;
}
cout<<ans%mod<<endl;
return ;
}
[HNOI2004]宠物收养所 题解的更多相关文章
- BZOJ1208:[HNOI2004]宠物收养所——题解
http://www.lydsy.com/JudgeOnline/problem.php?id=1208 Description 最近,阿Q开了一间宠物收养所.收养所提供两种服务:收养被主人遗弃的宠物 ...
- BZOJ 1208: [HNOI2004]宠物收养所 SET的妙用
1208: [HNOI2004]宠物收养所 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 4902 Solved: 1879 题目连接 http:/ ...
- BZOJ 1208: [HNOI2004]宠物收养所
1208: [HNOI2004]宠物收养所 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 7514 Solved: 2982[Submit][Sta ...
- bzoj 1208: [HNOI2004]宠物收养所 set
1208: [HNOI2004]宠物收养所 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 7328 Solved: 2892[Submit][Sta ...
- 数据结构(set):COGS 62. [HNOI2004] 宠物收养所
62. [HNOI2004] 宠物收养所 ★★★ 输入文件:pet.in 输出文件:pet.out 简单对比时间限制:1 s 内存限制:128 MB 最近,阿Q开了一间宠物收养所.收养 ...
- bzoj1208 [HNOI2004]宠物收养所(STL,Treap)
1208: [HNOI2004]宠物收养所 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 5956 Solved: 2317[Submit][Sta ...
- BZOJ 1208: [HNOI2004]宠物收养所(BST)
本来想先用set写一遍,再自己写个splay或treap,不过用set过了之后就懒得去写了....以后有空再来写吧..(不会有空的吧= = ------------------------------ ...
- BZOJ_1208_[HNOI2004]宠物收养所_SPLAY
BZOJ_1208_[HNOI2004]宠物收养所_SPLAY Description 最近,阿Q开了一间宠物收养所.收养所提供两种服务:收养被主人遗弃的宠物和让新的主人领养这些宠物.每个领养者都希望 ...
- bzoj 1208: [HNOI2004]宠物收养所 (Treap)
链接: https://www.lydsy.com/JudgeOnline/problem.php?id=1208 题面: 1208: [HNOI2004]宠物收养所 Time Limit: 10 ...
随机推荐
- 基于FFI模块CAPI与JavaScript的各种类型匹配总结
0.写在前面: 1)涉及的关键词定义: 传入:JavaScript向CAPI传值 传出:CAPI向JavaScript传值 2)关于类和结构体的封装,需要严格执行内存对齐,以防止读取越界,但是避免不了 ...
- java NPE就是空指针异常,null pointer exception
java NPE就是空指针异常,null pointer exception
- 【分享】迅为iTOP4412开发板-Android系统屏幕旋转设置
1.1概述 Android4.0,Androd4.4源代码能够编译成手机模式和平板模式,讯为iTop4412 开发平台 的Android系统默认编译为平板模式.客户须要依据自己的产品设计及应用环境,切 ...
- 红帽/CentOS ext4无法格式化大分区
红帽/CentOS 6.5的e2fsprogs版本号为1.41.12较低.无法创建16TB以上的文件系统 下载最新的e2fsprogs,编译安装 ftp://ftp.ntu.edu.tw/linux/ ...
- 犀牛Phinoceros 如何切换中文语言
Tools-Options-Rhino Options-Appearance,然后改成中文
- Android Studio最新配置教程2016
http://blog.csdn.net/wen_demo 一.Android studio 基本简单介绍 1.Android studio和Eclipse的差别: 1.Studio中有Project ...
- 【CODEFORCES】 D. CGCDSSQ
D. CGCDSSQ time limit per test 2 seconds memory limit per test 256 megabytes input standard input ou ...
- 小贝_mysql sql语句优化过程
sql语句优化 一.SQL优化的一般步骤 (1).通过show status命令了解各种SQL的运行频率. (2).定位运行效率较低的SQL语句-(重点select) (3).通过explain分析低 ...
- 第一篇 Windows 8 开发Windows Metro style app环境配置
半 饱问 题 到 我 这 里 为 止! 第一篇 Windows 8 开发Windows Metro style app环境配置 2012-09-24 08:24 by 半饱, 1289 阅读, 3 ...
- VS2010中使用AnkhSvn
VS2010中使用AnkhSvn 今天想到要在自己的开发环境IDE(Visual Studio 2010)中安装一个代码管理器的插件,本人在使用VS2005的时候一直都是使用AnkhSvn-2.1 ...