AC日记——宠物收养所 bzoj 1208
思路:
一棵splay树;
如果来者是宠物且树空,就将其加入树中;
如果树不空,则查找前驱后继,取最优,然后删点;
对人亦然;
注意边界和取模,最后的ans用long long其余用int即可;
来,上代码:
#include <cstdio>
#include <iostream>
#include <algorithm> using namespace std; #define maxn 80005
#define mod 1000000
#define INF 0x7fffffff struct TreeNodeType {
int w,key,opi,size,ch[]; void destroy()
{
w=key=opi=size=ch[]=ch[]=;
} void create(int x)
{
destroy();
key=x;
}
};
struct TreeNodeType tree[maxn<<]; int n,tot,flag,root; long long ans; inline int getson(int now)
{
return tree[tree[now].opi].ch[]==now;
} inline void updata(int now)
{
tree[now].size=tree[now].w;
if(tree[now].ch[]) tree[now].size+=tree[tree[now].ch[]].size;
if(tree[now].ch[]) tree[now].size+=tree[tree[now].ch[]].size;
} inline void rotate(int now)
{
int opi=tree[now].opi,fopi=tree[opi].opi,pos=getson(now);
tree[opi].ch[pos]=tree[now].ch[pos^];
tree[tree[opi].ch[pos]].opi=opi,tree[now].opi=fopi;
if(fopi) tree[fopi].ch[getson(opi)]=now;
tree[opi].opi=now,tree[now].ch[pos^]=opi;
updata(opi),updata(now);
} void splay(int now)
{
for(int opi;opi=tree[now].opi;rotate(now))
{
if(tree[opi].opi) rotate(getson(now)==getson(opi)?opi:now);
}
root=now;
} void insert(int x)
{
if(!root) tree[++tot].create(x),root=tot;
else
{
int now=root,opi=;
while()
{
if(tree[now].key==x)
{
tree[now].w++;
tree[now].size++;
splay(now);
return ;
}
opi=now,now=tree[now].ch[x>tree[now].key];
if(!now)
{
tree[++tot].create(x);
tree[tot].opi=opi;
tree[opi].ch[x>tree[opi].key]=tot;
splay(tot);
return ;
}
}
}
} inline int pre()
{
if(tree[root].w>) return root;
if(!tree[root].ch[]) return ;
int now=tree[root].ch[];
while(tree[now].ch[]) now=tree[now].ch[];
return now;
} inline int suc()
{
if(tree[root].w>) return root;
if(!tree[root].ch[]) return ;
int now=tree[root].ch[];
while(tree[now].ch[]) now=tree[now].ch[];
return now;
} void del()
{
if(tree[root].w>)
{
tree[root].w--;
tree[root].size--;
return ;
}
if(!tree[root].ch[]&&!tree[root].ch[])
{
tree[root].destroy();
root=;tree[root].destroy();return ;
}
if(tree[root].ch[]&&!tree[root].ch[])
{
int tmp=root;
root=tree[root].ch[];
tree[tmp].destroy();
tree[root].opi=;
return ;
}
if(!tree[root].ch[]&&tree[root].ch[])
{
int tmp=root;
root=tree[root].ch[];
tree[tmp].destroy();
tree[root].opi=;
return ;
}
int tmp=pre(),pos=root;
tree[tmp].ch[]=tree[root].ch[];
tree[tree[tmp].ch[]].opi=tmp;
root=tree[root].ch[],tree[root].opi=;
tree[pos].destroy();
splay(tree[tmp].ch[]);
} int main()
{
scanf("%d",&n);int ty,x;
while(n--)
{
scanf("%d%d",&ty,&x);
if(!root)
{
flag=ty;
insert(x);
}
else
{
if(ty==flag) insert(x);
else
{
insert(x);
int pr=pre(),su=suc(),to;
if(!pr) ans+=abs(tree[su].key-x),to=su;
else if(!su) ans+=abs(tree[pr].key-x),to=pr;
else
{
if(abs(tree[pr].key-x)<=abs(tree[su].key-x)) ans+=abs(tree[pr].key-x),to=pr;
else ans+=abs(tree[su].key-x),to=su;
}
del(),splay(to),del();
}
}
}
cout<<ans%mod;
return ;
}
AC日记——宠物收养所 bzoj 1208的更多相关文章
- AC日记——[HEOI2012]旅行问题 bzoj 2746
2746 思路: 建立ac自动机,然后把fail树抽出来: 然后在fail树上走lca(神奇): 代码: #include <cstdio> #include <vector> ...
- AC日记——[HNOI2008]GT考试 bzoj 1009
1009 思路: KMP上走DP(矩阵加速): DP[i][j]表示当前在第i位,同是匹配到不吉利串的第j位的方案数: 代码: #include <bits/stdc++.h> using ...
- AC日记——明明的烦恼 bzoj 1005
1005 思路: prufer编码+组合数: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 1005 #de ...
- AC日记——Mato的文件管理 bzoj 3289
3289 思路: 莫队求区间逆序对个数,树状数组维护: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 500 ...
- AC日记——[Scoi2010]序列操作 bzoj 1858
1858 思路: 恶心: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 100005 struct Tree ...
- AC日记——[ZJOI2007]报表统计 bzoj 1058
1058 思路: 平衡树的题: 然而我的平衡树写一次炸一次QwQ: 而且各种tle: 所以stl水过: 代码: #include <set> #include <cstdio> ...
- AC日记——[JSOI2007]建筑抢修 bzoj 1029
1029 思路: 贪心,而且,stl水过: 然而神特么输出que.size()就错! 代码: #include <queue> #include <cstdio> #inclu ...
- AC日记——[JSOI2008]火星人prefix bzoj 1014
1014 思路: 平衡树+二分答案+hash: 好了懂了吧. 代码: #include <cstdio> #include <cstring> #include <ios ...
- AC日记——[HAOI2007]覆盖问题 bzoj 1052
1052 思路: 二分答案: 二分可能的长度: 然后递归判断长度是否可行: 先求出刚好覆盖所有点的矩形: 可行的第一个正方形在矩形的一个角上: 枚举四个角上的正方形,然后删去点: 删去一个正方形后,递 ...
随机推荐
- notification 使用的基本方法
当某个应用程序希望向用户发出一些提示信息,而应用程序又不在前台,可以借助Notification来实现.发出一条通知后,手机最上方额通知栏会显示一个图标,下来状态栏以后可以看到详细内容. 一.通知的基 ...
- Spring---浅谈IOC
概念 IOC(Inversion of Control 控制反转)是spring的核心,贯穿始终.所谓IOC,对于spring框架来说,就是由spring来负责控制对象的生命周期和对象间的关系. 传统 ...
- Neural Network
逻辑回归用神经网络节点的方式表示 前面已经介绍过逻辑回归的模型,样本为(x,y) 其中y的值为1或0,假设x有2个特征,则对应关系如下图所示.  实际情况是需要求需要三个参数,因此输入层需要添加一个 ...
- Apache shiro学习总结
Apache shiro集群实现 (一) shiro入门介绍 Apache shiro集群实现 (二) shiro 的INI配置 Apache shiro集群实现 (三)shiro身份认证(Shiro ...
- Asp.net自定义控件开发任我行(8)-数据集绑定
摘要 已经有好几天没有写博客了,今天继续,前几天写到了注册自定义事件,今天我们来讲数据集绑定. 先把运行效果截个图给大家看,让大家心里也有个底.(大家要从第一章开始看起,我们每一章都是接着前面没做完的 ...
- phpMyAdmin 4.7.x CSRF
phpMyAdmin 4.7.x CSRF 场景:管理员登陆phpmyadmin之后,我试验了一下,发现只要是登陆session没有失效应该是都可以的, 利用,phpmyadmin可以通过get方式操 ...
- php 不重新编译增加openssl扩展
安装openssl和开发包 yum install openssl openssl-devel 跳转到PHP源码下的openssl cd /usr/local/src/php-5.5.27/ext/o ...
- [HDU2829] Lawrence [四边形不等式优化dp]
题面: 传送门 思路: 依然是一道很明显的区间dp 我们设$dp\left[i\right]\left[j\right]$表示前$j$个节点分成了$i$块的最小花费,$w\left[i\right]\ ...
- 优化Angularjs的$watch方法
Angularjs的$watch相信大家都知道,而且也经常使用,甚至,你还在为它的某些行为感到恼火.比如,一进入页面,它就会调用一次,我明明希望它在我初始化之后,值再次变动才调用.这种行为给我们带来许 ...
- webpack编译vue出现dev警告
alias配置 vue < 2.1.0 module.exports = { ... resolve: { alias: { 'vue$': 'vue/dist/vue.js' } }, ... ...