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. HDU 5119 Happy Matt Friends (14北京区域赛 类背包dp)

    Happy Matt Friends Time Limit: 6000/6000 MS (Java/Others)    Memory Limit: 510000/510000 K (Java/Oth ...

  2. [BZOJ3312][USACO]不找零(状压DP)

    Description 约翰带着 N 头奶牛在超市买东西,现在他们正在排队付钱,排在第 i 个位置的奶牛需要支付 Ci元.今天说好所有东西都是约翰请客的,但直到付账的时候,约翰才意识到自己没带钱,身上 ...

  3. 自定义 popWindow弹框 工具包

    前言:因为Android 没有像IOS一样的ActionSheet,虽然在github上看到有一些类似ActionSheet的库,总觉得不好用,不如自己写一个弹框通用类,样式全部自已来多好. Step ...

  4. Linux下Oracle JDK替换Open JDK

    Oracle的产品需要Oracle JDK,但是Linux发行版附带的都是开源的Open JDK,这里给出的方法是在不删除原有Open JDK的情况下,安装Oracle JDK 环境 系统:CentO ...

  5. NOI p 2017 TG游记

    嗨小朋友们大家好 还记得我是谁吗 对了我就是为iot配音的演员 弹鸡鸡 今天呐我特别的要向长沙市的oier们 洛谷的oier们 还有cnblogs的oier们问声好 为什么呢 因为我们在2017年11 ...

  6. Migrate a Domain-based Namespace to Windows Server 2008 Mode

    TechNet Library Scripting with Windows PowerShell Windows and Windows Server Automation with Windows ...

  7. 12、jQuery知识总结-2

    1.避免冲突 jQuery 使用 $ 符号作为 jQuery 的简介方式 <html> <head> <script type="text/javascript ...

  8. 34、Java集合框架List,Map,Set等全面介绍(转载)

      Java Collections Framework是Java提供的对集合进行定义,操作,和管理的包含一组接口,类的体系结构.   Java集合框架的基本接口/类层次结构: java.util.C ...

  9. leetcode 【 Merge k Sorted Lists 】python 实现

    题目: Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexit ...

  10. c++ 中pair类模板的用法详解

    pair: 头文件:#include<utility> 类模板:template <class T1, class T2> struct pair 参数:T1是第一个值的数据类 ...