[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 ...
随机推荐
- bootstrap日期控件
http://www.bootcss.com/p/bootstrap-datetimepicker/ <link href="js/b/css/bootstrap-datetimepi ...
- spark sql读hbase
项目背景 spark sql读hbase据说官网如今在写,但还没稳定,所以我基于hbase-rdd这个项目进行了一个封装,当中会区分是否为2进制,假设是就在配置文件里指定为#b,如long#b,还实用 ...
- HDOJ 5383 Yu-Gi-Oh! 最大费用最大流
网络流裸题: 分两部分建图,求不要求满流的最大费用最大流..... Yu-Gi-Oh! Time Limit: 4000/2000 MS (Java/Others) Memory Limit: ...
- javascript 将中文符号转换成英文符号
javascript 将中文符号转换成英文符号 CreateTime--2018年3月30日09:01:29 Author:Marydon /** * 将中文符号转换成英文符号 */ functi ...
- DetachedCriteria的使用
设计上能够灵活的依据 Criteria 的特点来方便地进行查询条件的组装.如今对 Hibernate的Criteria 的使用方法进行总结: Hibernate 设计了 CriteriaSpec ...
- es bulk 批量删除
bulk [root@hadoop2 ~]# cat bulk.del.es.json {"delete":{"_index":"direct_vot ...
- java 定位工具
#查看JVM所有进程及启动类信息以及PID jps -mlvV #查看JVM运行各种状态信息,包括GC,类加载,堆内存信息,jit编译信息等jstat -gcutil <PID> (堆内存 ...
- 【BeijingWc 2008】 秦腾与教学评估
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=1271 [算法] 二分 [代码] #include<bits/stdc++.h& ...
- java线程异常处理方法
工作中常发现有些程序发生异常但却没有错误日志,原因就是一些开发线程异常处理错误,导致程序报错但异常信息打印到堆栈上,不好在生产环境中定位问题. 在java多线程程序中,所有线程都不允许抛出未捕获的ch ...
- FTP FtpWebRequest 异步上传文件
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...