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. linux lvm扩容

    1.分区,  查看磁盘使用:fdisk -l 对磁盘分区:fdisk /dev/sdb 2.创建pv pvcreate /dev/sdb1 查看pv: pvdisplay 3.查看vg  vgdisp ...

  2. [Hdu3652]B-number(数位DP)

    Description 题目大意:求小于n是13的倍数且含有'13'的数的个数. (1 <= n <= 1000000000) Solution 数位DP,题目需要包含13,且被13整除, ...

  3. Maven web项目的项目如何创建

    1.eclipse--创建maven项目--选择打包方式为war 2.此时项目还缺少一些东西,比如web-inf,web.xml配置文件之类的 3.项目右键-properties-project fa ...

  4. python拼接

    拼接: name=zhuhuan age=23 salary=333 info=''' ----- info of %s----- age:%s name:%s salary:%s %(name,ag ...

  5. SQL 基础语法详解

    SQL 命令一般分为 DQL.DML.DDL DQL:数据查询语句,基本就是 SELECT 查询命令,用于数据查询 DML:Data Manipulation Language 的简称,即数据操纵语言 ...

  6. 冒泡排序(Bubble Sort)及优化

    原理介绍 冒泡排序算法的原理如下: 比较相邻的元素.如果第一个比第二个大,就交换他们两个. 对每一对相邻元素做同样的工作,从开始第一对到结尾的最后一对.在这一点,最后的元素应该会是最大的数. 针对所有 ...

  7. 感谢beyond,感谢家驹

  8. 第二篇:python基础_2

    本篇内容 数字 字符串 元祖 字典 列表 集合 for循环 二进制 字符编码 文件处理 一.数字 1.int(整型) 在32位机器上,整数的位数为32位,取值范围为-2**31-2**31-1,即-2 ...

  9. CodeForces875C[拓扑排序] Codeforces Round #440 [Div2E/Div1C]

    只要保存每相邻两行字符串 第一个不同位 即可.然后按照 第一个不同位上的字符有: " 来自下一行的 大于 来自上一行的" 构图,跑拓扑排序即可. 当然要判断一下有没有环构成, 有环 ...

  10. POJ 3678 Katu Puzzle(2-SAT,合取范式大集合)

    Katu Puzzle Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9987   Accepted: 3741 Descr ...