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的更多相关文章

  1. AC日记——[HEOI2012]旅行问题 bzoj 2746

    2746 思路: 建立ac自动机,然后把fail树抽出来: 然后在fail树上走lca(神奇): 代码: #include <cstdio> #include <vector> ...

  2. AC日记——[HNOI2008]GT考试 bzoj 1009

    1009 思路: KMP上走DP(矩阵加速): DP[i][j]表示当前在第i位,同是匹配到不吉利串的第j位的方案数: 代码: #include <bits/stdc++.h> using ...

  3. AC日记——明明的烦恼 bzoj 1005

    1005 思路: prufer编码+组合数: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 1005 #de ...

  4. AC日记——Mato的文件管理 bzoj 3289

    3289 思路: 莫队求区间逆序对个数,树状数组维护: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 500 ...

  5. AC日记——[Scoi2010]序列操作 bzoj 1858

    1858 思路: 恶心: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 100005 struct Tree ...

  6. AC日记——[ZJOI2007]报表统计 bzoj 1058

    1058 思路: 平衡树的题: 然而我的平衡树写一次炸一次QwQ: 而且各种tle: 所以stl水过: 代码: #include <set> #include <cstdio> ...

  7. AC日记——[JSOI2007]建筑抢修 bzoj 1029

    1029 思路: 贪心,而且,stl水过: 然而神特么输出que.size()就错! 代码: #include <queue> #include <cstdio> #inclu ...

  8. AC日记——[JSOI2008]火星人prefix bzoj 1014

    1014 思路: 平衡树+二分答案+hash: 好了懂了吧. 代码: #include <cstdio> #include <cstring> #include <ios ...

  9. AC日记——[HAOI2007]覆盖问题 bzoj 1052

    1052 思路: 二分答案: 二分可能的长度: 然后递归判断长度是否可行: 先求出刚好覆盖所有点的矩形: 可行的第一个正方形在矩形的一个角上: 枚举四个角上的正方形,然后删去点: 删去一个正方形后,递 ...

随机推荐

  1. 机顶盒demux的工作原理

    在机顶盒中demux部分相对来说是比较复杂的部分,对于机顶盒软件开发的新手来说通常在这里会遇到一些困难,今天特意研究了一下驱动层代码,有一点自己的理解,因此写下来记录一下学习过程. 机顶盒中数据是如何 ...

  2. Linux文件类型 扩展名的作用

    链接类型文件 查找显示管道文件 普通文件类型 file 查看文件的类型 data文件类型 创建块字和符设备 mknod 1,.tar .tar.gz .tgz .zip tar.bz 表示压缩文件,创 ...

  3. 查询语句为“%string_”的情况

    select * from t_user where user_name like '%Joe_%'实际查询出来的语句为: 而不像预计的前两条.

  4. Android BadgeView 工具包

    前言:消息未读,显示红点或者红色数字,其实就是一个TextView,有推送一般就有badgeView. Step 1 因为在github上看到了一些类似的第三方库,嫌麻烦,不如直接封装一个类,直接使用 ...

  5. laravel5.2总结--composer使用和自动加载介绍

    首先看下phpcomposer官方的定义,composer是 PHP 用来管理依赖(dependency)关系的工具.你可以在自己的项目中声明所依赖的外部工具库(libraries),Composer ...

  6. STL学习笔记4--set and multiset

    集合(Set)是一种包含已排序对象的关联容器.多元集合(MultiSets)和集合(Sets)相像,只不过支持重复对象,其用法与set基本相同. 用法介绍 1.insert()函数 首先把头文件set ...

  7. hnust 土豪金的加密解密

    问题 G: 土豪金的加密与解密 时间限制: 1 Sec  内存限制: 128 MB提交: 466  解决: 263[提交][状态][讨论版] 题目描述     有一位姓金的同学因为买了一部土豪金,从此 ...

  8. Leetcode 583.两个字符串的删除操作

    两个字符串的删除操作 给定两个单词 word1 和 word2,找到使得 word1 和 word2 相同所需的最小步数,每步可以删除任意一个字符串中的一个字符. 示例 1: 输入: "se ...

  9. 菜鸟之路——机器学习之SVM分类器学习理解以及Python实现

    SVM分类器里面的东西好多呀,碾压前两个.怪不得称之为深度学习出现之前表现最好的算法. 今天学到的也应该只是冰山一角,懂了SVM的一些原理.还得继续深入学习理解呢. 一些关键词: 超平面(hyper ...

  10. linq本质扩展方法+lambda表达式

    string[] names = { "aa","bb","cc","dd"}; /* IEnumerable<s ...