当前位置: 首页 > news >正文

上海网站自然排名优化价格/小程序开发系统

上海网站自然排名优化价格,小程序开发系统,网站建设的可行性要求,阿里云 云虚拟主机 wordpress嘟嘟嘟 看完题,思路一秒就出来了:建两棵平衡树,分别维护宠物和领养者。然后就是正常的插入,找前驱后继,删除操作了。 然后旁边的lba巨佬说只用建一棵就行,如果宠物多了就维护宠物,否则维护领养者…

嘟嘟嘟

看完题,思路一秒就出来了:建两棵平衡树,分别维护宠物和领养者。然后就是正常的插入,找前驱后继,删除操作了。
然后旁边的lba巨佬说只用建一棵就行,如果宠物多了就维护宠物,否则维护领养者。
总而言之这就是一道板儿题。

然而我刚学\(splay\)啊!
于是一上午就这么过去了。
旋转,插入,找前驱后继这些操作就不说了,可以看我昨天的博客。主要是删除操作困扰了我半天。
啊不对,找前驱后继得说一下。
拿找前驱为例。众所周知,我们先找\(x\),然后把他旋到根,这样的话前驱就是先走一步左儿子,然后右儿子一直走下去。
但是这道题\(x\)可能不在平衡树里。于是我就懵了,想了半天都不知道怎么改进,最后还是拿\(bst\)的找法去写的,然而写完后就发现删除操作写不了了。因为删除我需要\(x\)的前驱和后继的节点编号,但是按\(bst\)的写法只能返回值,而没有节点编号。
因此最后我还不得不回来看\(lba\)的代码。然而看了半天我也没看出来他怎么处理这一点的,问他他说没考虑到这一点,但这样的话样例怎么解释呀!在各种磨叽后我终于看到了百行代码中的一条不寻常的特判:就是在找前驱的时候,旋转完如果根的值比\(x\)小就直接返回!然后我想了想,发现奥秘在这里:查找的时候如果\(x\)不存在,找到的是离\(x\)最近的数\(y\),然后把他旋了上去,但不知道比\(x\)大还是比\(x\)小。这样找前驱的时候,如果根节点(就是\(y\))比\(x\)小,说明\(y\)\(x\)小,直接返回\(y\)。后继同理。
嗯,就是这样。

然后叨叨一下删除。
删除权值为\(x\)的节点,先找\(x\)的前驱\(a\)和后继\(b\),然后把\(a\)旋到根上,再把\(b\)旋到\(a\)的右儿子上。这样\(x\)就一定是\(b\)的左儿子,而且\(b\)的左子树只有\(x\)。所以如果\(x\)有多个,就\(cnt--\),否则把\(b\)的左子树变为\(0\)
代码

#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<vector>
#include<stack>
#include<queue>
using namespace std;
#define enter puts("") 
#define space putchar(' ')
#define Mem(a, x) memset(a, x, sizeof(a))
#define rg register
typedef long long ll;
typedef double db;
const int INF = 2147483647;
const db eps = 1e-8;
const int maxn = 8e4 + 5;
const int mod = 1000000;
inline ll read()
{ll ans = 0;char ch = getchar(), last = ' ';while(!isdigit(ch)) last = ch, ch = getchar();while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();if(last == '-') ans = -ans;return ans;
}
inline void write(ll x)
{if(x < 0) x = -x, putchar('-');if(x >= 10) write(x / 10);putchar(x % 10 + '0');
}int n, tot = 0;
ll ans = 0;struct Tree
{int ch[2], fa;int cnt, siz; ll val;
}t[maxn];
int root, ncnt = 0;void pushup(int now)
{t[now].siz = t[t[now].ch[0]].siz + t[t[now].ch[1]].siz + t[now].cnt;
}
void rotate(int x)
{int y = t[x].fa, z = t[y].fa, k = (t[y].ch[1] == x);t[z].ch[t[z].ch[1] == y] = x; t[x].fa = z;t[y].ch[k] = t[x].ch[k ^ 1]; t[t[x].ch[k ^ 1]].fa = y;t[x].ch[k ^ 1] = y; t[y].fa = x;pushup(x); pushup(y);
}
void splay(int x, int s)
{while(t[x].fa != s){int y = t[x].fa, z = t[y].fa;if(z != s){if((t[z].ch[0] == y) ^ (t[y].ch[0] == x)) rotate(x);else rotate(y);}rotate(x);}if(!s) root = x;
}
void insert(int x)
{int now = root, f = 0;while(now && t[now].val != x) f = now, now = t[now].ch[x > t[now].val];if(now) t[now].cnt++;else{now = ++ncnt;t[now].ch[0] = t[now].ch[1] = 0;if(f) t[f].ch[x > t[f].val] = now;t[now].fa = f;t[now].cnt = t[now].siz = 1;t[now].val = x;}splay(now, 0);
}
void find(int x)
{int now = root;if(!now) return;while(t[now].ch[x > t[now].val] && t[now].val != x) now = t[now].ch[x > t[now].val];splay(now, 0);
}
int pre(int x)
{find(x);  if(t[root].val < x) return root;int now = t[root].ch[0];while(t[now].ch[1]) now = t[now].ch[1];return now;
}
int nxt(int x)
{find(x);if(t[root].val > x) return root;int now = t[root].ch[1];while(t[now].ch[0]) now = t[now].ch[0];return now;
}
void del(int x)
{int a = pre(x), b = nxt(x);splay(a, 0); splay(b, a);int k = t[b].ch[0];if(t[k].cnt > 1) t[k].cnt--;else t[b].ch[0] = 0;
}void _Debug(int now)
{if(!now) return;printf("()()%d %lld zuo:%lld you:%lld\n", now, t[now].val, t[t[now].ch[0]].val, t[t[now].ch[1]].val);_Debug(t[now].ch[0]); _Debug(t[now].ch[1]);
}int main()
{n = read();insert(INF); insert(-INF);for(int i = 1; i <= n; ++i){int op = read(); ll x = read();if(!op){if(tot >= 0) insert(x);else{ll a = t[pre(x)].val, b = t[nxt(x)].val;if(x - a <= b - x) ans = (ans + x - a) % mod, del(a);else ans = (ans + b - x) % mod, del(b);}tot++;}else{if(tot <= 0) insert(x);else{//_Debug(root);ll a = t[pre(x)].val, b = t[nxt(x)].val;//puts("------");//_Debug(root);if(x - a <= b - x) ans = (ans + x - a) % mod, del(a);else ans = (ans + b - x) % mod, del(b);}tot--;}}write(ans), enter;return 0;
}

转载于:https://www.cnblogs.com/mrclr/p/10048835.html

http://www.lbrq.cn/news/1460341.html

相关文章:

  • 网站制作模板免费下载/app优化建议
  • 网站关键词seo费用/制作网站需要什么
  • 西安网站建设公司排名/郑州网络推广代理
  • 注册域名之后怎么做网站/上海做推广的引流公司
  • 开个做网站公司/怎么优化标题和关键词排名
  • 网站建设套模板/2024年重大新闻摘抄
  • 湖北 网站 备案 时间/启动互联全网营销推广
  • 昆明网站建设天猫运营/企业网站seo贵不贵
  • 佛山网站建设网络公司/列举网络推广的方式
  • 网站建设温州/如何自己做网站
  • 浦口做网站价格/江苏seo外包
  • 网上有女的叫你建网站/微信广告投放推广平台多少费用
  • 南京高端网站建设工作室/拉新平台哪个好佣金高
  • php开发微网站开发/长沙网络营销外包哪家好
  • 美工做图详情页设计/什么叫seo网络推广
  • 怎样把自己做的网站发布/怎么把广告发到各大平台
  • 快速搭建网站 数据存储/百度发布
  • 网站开发和前端和数据媒体/全球搜官网
  • 网站空间服务器排名/公司怎么推广网络营销
  • 太原网站建设优化/营销型网站开发公司
  • 电脑手机网站制作/市场调研问卷调查怎么做
  • visual studio 开发网站开发/无锡百度竞价公司
  • 网站建设登录注册怎么做/排行榜哪个网站最好
  • 英文建设网站/百度搜索引擎优化指南最新版
  • 理卖做各视频网站的会员/如何推广网页
  • 达州市建设局网站/网上销售
  • 禅城网站建设代理/新闻联播直播 今天
  • 深圳专业企业网站建/上海百度推广排名优化
  • 南阳教育网站平台/佛山网络推广哪里好
  • 广州网络公司网络推广/上海网络seo优化公司
  • 03.一键编译安装Redis脚本
  • lumerical——锥形波导偏振转换
  • 批发订货系统:私有化部署与源代码支持越来越受市场追捧
  • 电脑声音标志显示红叉的原因
  • 知识点汇集(二)-misc
  • 每日五个pyecharts可视化图表-bars(2)