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. 谭浩强C第四版(p141)16.输出以下图案

    运行结果: * *** ***** ******* ***** *** * Press any key to continue #include<stdio.h> int main() { ...

  2. 51NOD:1639-绑鞋带

    传送门:https://www.51nod.com/onlineJudge/submitDetail.html#!judgeId=475129 1639 绑鞋带 基准时间限制:1 秒 空间限制:131 ...

  3. MAX(数论)

    Description 小C有n个区间,其中第i个区间为[li,ri],小C想从每个区间中各选出一个整数,使得所有选出的数and起来得到的结果最大,请你求出这个值. Input Format 第一行一 ...

  4. PAT Basic 1085

    1085 PAT单位排行 每次 PAT 考试结束后,考试中心都会发布一个考生单位排行榜.本题就请你实现这个功能. 输入格式: 输入第一行给出一个正整数 N(≤10​5​​),即考生人数.随后 N 行, ...

  5. 从头开始学习数据库及ADO.NET之PostgreSql字段约束——竹子整理

    约束数据表列执行的规则.这些是用来防止无效的数据被输入到数据库中..这确保数据库中的数据的准确性和可靠性. 约束可以是列级或表级.仅适用于表级约束被应用到整个表的列级约束.为列定义的数据类型,本身是一 ...

  6. Ubuntu下的定时备份数据库

    1.编写备份数据库的shell脚本 mysqldump -uUserName -pPassword dbName >/XXX/XXXX/XXXX/fileName_$(date +%Y%m%d_ ...

  7. UnicodeDecodeError: 'ascii' codec can't decode byte 0xe4 in position 19: ordinal not in range(128)

    解决方案: 1: 在网上找到的解决方案是: 在调用import matplotlib.pyplot as plt前 import sys sys.setdefaultencoding(“gbk”) 让 ...

  8. json对象数据列数

    // var len = data.length(); // alert(data.Rows.length); var colCount = (function count(){//一条记录中有几个键 ...

  9. 300万PV的ASP.NET网站使用阿里云的配置建议

    @老牛吃肉在博文“今天的访问高峰,扛过去了”的评论中询问了这样一个问题: 你好,站长,本公司正在考虑用阿里云.用途:互联网网站,主要站点:asp.net开发目前的考虑情况:访问ip 15-20万,pv ...

  10. 【Maximal Rectangle】cpp

    题目: Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones ...