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

FHQtreap新模板

洛谷P4291

#include<bits/stdc++.h>
using namespace std;
#define LL long long
const LL MAXN=250005;
LL root,tot,ch[MAXN][2],siz[MAXN],pri[MAXN],val[MAXN];
string nm[MAXN];
map<string,LL>vis;
void pushup(LL rt){siz[rt]=siz[ch[rt][0]]+siz[ch[rt][1]]+1;
}
void split_val(LL rt,LL &rt1,LL &rt2,LL k){if(!rt){rt1=rt2=0;return ;}else if(k>=val[rt]){rt1=rt;split_val(ch[rt][1],ch[rt][1],rt2,k);}else{rt2=rt;split_val(ch[rt][0],rt1,ch[rt][0],k);}pushup(rt);
}
void split_rank(LL rt,LL &rt1,LL &rt2,LL k){if(!rt){rt1=rt2=0;return ;}else if(k>=siz[ch[rt][0]]+1){rt1=rt;split_rank(ch[rt][1],ch[rt][1],rt2,k-siz[ch[rt][0]]-1);}else{rt2=rt;split_rank(ch[rt][0],rt1,ch[rt][0],k);}pushup(rt);
}
LL merge(LL x,LL y){if(!x||!y)return x+y;if(pri[x]<pri[y]){ch[x][1]=merge(ch[x][1],y);pushup(x);return x;}else{ch[y][0]=merge(x,ch[y][0]);pushup(y);return y;}
}
void erase(LL k){LL x,y,z;x=y=z=0;split_val(root,x,z,k);split_val(x,x,y,k-1);root=merge(x,z);
}
LL new_node(string s,LL sc,LL tim){siz[++tot]=1;nm[tot]=s;val[tot]=sc*MAXN-tim;pri[tot]=rand();return tot;
}
void insert(string s,LL sc,LL tim){if(vis[s])erase(val[vis[s]]);vis[s]=new_node(s,sc,tim);LL x,y;x=y=0;split_val(root,x,y,val[tot]);root=merge(merge(x,tot),y);
}
void print(LL rt){if(!rt)return ;print(ch[rt][1]);cout<<nm[rt]<<" ";print(ch[rt][0]);
}
void printindex(LL k){LL nowtot=siz[root];LL x,y,z;x=y=z=0;split_rank(root,x,z,nowtot-k+1);split_rank(x,x,y,max(nowtot-k-9,0ll));print(y);printf("\n");root=merge(merge(x,y),z);
}
void printrank(string s){LL rt=vis[s];LL x,y;x=y=0;split_val(root,x,y,val[rt]);printf("%lld\n",siz[y]+1);root=merge(x,y);
}
int main(){// freopen("test.in","r",stdin);srand(time(0));LL n;scanf("%lld",&n);for(LL i=1;i<=n;i++){char opt;while(1){scanf("%c",&opt);if(opt=='+'||opt=='?')break;}if(opt=='+'){string name;cin>>name;LL num;scanf("%lld",&num);insert(name,num,i);// cout<<opt<<"*"<<name<<"*"<<num<<endl;}else{string name;cin>>name;if(name[0]>='0'&&name[0]<='9'){LL num=0;for(LL j=0;j<name.size();j++){num=num*10+name[j]-'0';}printindex(num);// cout<<opt<<"*"<<num<<endl;}else{printrank(name);// cout<<opt<<"*"<<name<<endl;}}}
}

http://www.mrgr.cn/news/54889.html

相关文章:

  • Virtuoso Layout无法显示元件,出现pcellEvalFailed错误问题解析
  • 使用 Docker compose 部署 Nacos(达梦数据库)
  • <项目代码>YOLOv8路面病害识别<目标检测>
  • 第三季度中国游戏市场收入创历史新高;京东物流与淘宝天猫达成合作;YouTube 上线“用相机拍摄”标签....|网易数智日报
  • 探究互联网数字化商品管理变革:从数据化到精准运营的路径转型
  • 【智能大数据分析 | 实验三】Storm实验:实时WordCountTopology
  • 诊断知识:NRC78(Response Pending)的回复时刻
  • @RequestMapping(“/api/users“)详细解释一下这行代码
  • 【云从】八、HTTPS流程与建站
  • Redux (八) 路由React-router、嵌套路由、路由传参、路由懒加载
  • 【Android】浅析OkHttp(1)
  • MySQL-29.事务-四大特性
  • web3学习-区块链基础知识
  • 图文深入介绍oracle资源管理(续)
  • 10.20学习
  • Linux基本指令一眼看懂(简洁表示)
  • C语言实践中的补充知识 Ⅱ
  • 【AIGC视频生成】视频扩散模型(综述+最新进展)
  • Python第五节 迷宫王国的奇幻旅程
  • TypeScript中 interface接口 type关键字 enum枚举类型
  • Guava防击穿回源-异步防击穿
  • nginx的配置
  • 电子政务的类型
  • SHELL函数之的使用
  • 【论文翻译】ICLR 2018 | DCRNN:扩散卷积递归神经网络:数据驱动的交通预测
  • Python练习3