[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 ...
随机推荐
- Linux: 查找使用中的port
找出所有使用中的port netstat -tulpn 输出 Active Internet connections (only servers) Proto Recv-Q Send-Q Local ...
- ubuntu 搭建 svn服务器,使用http方式访问
原文: http://blog.csdn.net/wobuxingfang/article/details/70835414 参考:http://www.cnblogs.com/zzugyl/p/36 ...
- Spring MVC : Java模板引擎 Thymeleaf (二)
本文原计划直接介绍Thymeleaf的视图解析,但考虑到学习的方便,决定先构建一个spring-mvc. 以下的全部过程仅仅要一个记事本和JDK就够了. 第一步,使用maven构建一个web app. ...
- [PsTools]psexec.exe使用范例-运行远程电脑程序(exe、bat等)
前置条件 先下载psexec.exe.放置到C盘根文件夹 下载地址:http://download.csdn.net/detail/whylaughing/8885893 命令范例:(注意空格) C: ...
- python基础练习-猜年龄、编写登陆接口小程序
python基础练习: 一.猜年龄 , 可以让用户最多猜三次! age=40 count = 1 while count <=3 : user_guess=int(input("i ...
- UVA - 10324 Zeros and Ones
Description Given a string of 0's and 1's up to 1000000 characters long and indices i and j, you are ...
- ios14--购物车优化2
// // ViewController.m // 03-综合练习 // // Created by xiaomage on 15/12/28. // Copyright © 2015年 小码哥. A ...
- JSP页面的跳转及传值
1.response.sendRedirect("跳转到页面的URL"); 该方法通过修改HTTP协议的HEADER部分,对浏览器下达重定向指令的,使浏览器显示重定向网页的内容. ...
- 杂项:BPM
ylbtech-杂项:BPM 1.返回顶部 1. BPM,即业务流程管理,是一种以规范化的构造端到端的卓越业务流程为中心,以持续的提高组织业务绩效为目的的系统化方法,常见商业管理教育如EMBA.MBA ...
- astgo-官方功能更新日志
2014年9月 2014-9-7:更新 1.安卓.苹果客户端添加字幕广告(点击字幕跳转打开网址) 2.安卓.苹果客户端添加公告推送功能 3.修正Astgo软交换管理平台修删除充值卡.用户账号,造成整个 ...