超级数组
来源:互联网 发布:minecraft像素画软件 编辑:程序博客网 时间:2024/04/30 06:24
题目:http://oi.tju.edu.cn/problem/view/1113.html
经典的东西。。求排名。
平衡树。。参照某大牛的模版打了一遍。用的treap实现的。。自己用了离散化+线段树实现了一遍。戳爆了。打了接近一个小时才搞定。
平衡树,treap版
#include <cstdio>#include <cstring>#include <cstdlib>#define inf 1000000000struct node{int aux,key,cnt,size;node *left,*right;};typedef node *Tnode;Tnode root,nilpoint;inline void renew(Tnode &root){root->size=root->cnt+root->left->size+root->right->size;}inline void LeftRotate(Tnode &root){Tnode t=root->left;root->left=t->right;t->right=root;renew(root);renew(t);root=t;}inline void RightRotate(Tnode &root){Tnode t=root->right;root->right=t->left;t->left=root;renew(root);renew(t);root=t;}inline void Insert(Tnode &root,int key){if (root==nilpoint){root=new node;root->left=root->right=nilpoint;root->key=key;root->cnt=root->size=1;root->aux=((rand()<<15)+rand())%inf;return;}if (root->key==key) ++root->cnt;else if (key<root->key){Insert(root->left,key);if (root->left->aux<root->aux) LeftRotate(root);}else if (key>root->key){Insert(root->right,key);if (root->right->aux<root->aux) RightRotate(root);}renew(root);}inline void Delete(Tnode &root,int key){if (key==root->key){if (root->left==nilpoint && root->right==nilpoint){if (root->cnt>1) --root->cnt,--root->size;else root=nilpoint;return;}if (root->left->aux<root->right->aux){LeftRotate(root);Delete(root->right,key);}else{RightRotate(root);Delete(root->left,key);}renew(root);return;}if (key<root->key) Delete(root->left,key);else Delete(root->right,key);renew(root);}inline int Query(Tnode &root,int rank){if (rank>root->size) return -1;if (rank<=root->left->size) return Query(root->left,rank);if (rank<=root->cnt+root->left->size) return root->key;return Query(root->right,rank-root->left->size-root->cnt);}int main(){freopen("arr.in","r",stdin);freopen("arr.out","w",stdout);nilpoint=new node;nilpoint->left=nilpoint->right=nilpoint;nilpoint->cnt=nilpoint->size=0;nilpoint->aux=inf;root=nilpoint;int n,m;scanf("%d%d",&n,&m);for (int i=0;i<m;++i){char op[10];int x,y;scanf("%s%d",op,&x);if (op[0]=='i') Insert(root,x);else{y=Query(root,x);printf("%d\n",y);Delete(root,y);}}return 0;}
离散化+线段树
#include <cmath>#include <ctime>#include <iostream>#include <string>#include <vector>#include <cstdio>#include <cstdlib>#include <cstring>#include <queue>#include <map>#include <set>#include <algorithm>#include <cctype>#include <stack>#include <deque>using namespace std;typedef long long LL;#define eps 10e-9#define inf 0x3f3f3f3fconst int maxn = 100000+100;char op[maxn][10];int a[maxn],c[maxn];struct node{ int in,v,nv; bool operator < (const node &c) const { return v<c.v; }}b[maxn];int sum[maxn<<2],flag[maxn<<2],val[maxn<<2];void build(int id,int l,int r){ if(l==r){ flag[id]=0; sum[id]=0; return ; } int m=(l+r)>>1; build(id<<1,l,m); build(id<<1|1,m+1,r);}void insert(int id,int goal,int l,int r,int op,int v){ if(l==goal&&r==goal){ flag[id]+=op; sum[id]+=op; val[id]=v; return ; } int m=(l+r)>>1; if(m>=goal){ insert(id<<1,goal,l,m,op,v); } else insert(id<<1|1,goal,m+1,r,op,v); sum[id]=sum[id<<1]+sum[id<<1|1];}int inde;int find(int id,int goal,int l,int r){ int m=(l+r)>>1; if(l==r){ inde=l; return val[id]; } if(sum[id<<1]>=goal){ find(id<<1,goal,l,m); } else { find(id<<1|1,goal-sum[id<<1],m+1,r); }}int main(){ freopen("arr.in","r",stdin); freopen("arr.out","w",stdout); int n,m; scanf("%d %d",&n,&m); int len=0,l=0; for(int i=0;i<m;i++){ scanf("%s %d",op[i],&a[i]); if(op[i][0]=='i'){ b[len].v=a[i]; b[len].in=l++; len++; } } sort(b,b+len); for(int i=0;i<len;i++) c[ b[i].in ]=i+1; build(1,1,m); int cur=0; for(int i=0;i<m;i++){ if(op[i][0]=='i'){ insert(1,c[cur++],1,m,1,a[i]); } else { printf("%d\n",find(1,a[i],1,m)); insert(1,inde,1,m,-1,0); } } return 0;}
- 超级数组
- 超级数组
- PHP超级全局变量数组
- PHP超级全局变量数组
- PHP超级全局变量数组
- 【SDOI2007】【COGS723】超级数组
- 超级数组(类似ArrayList)
- PHP超级全局数组$_FILES
- 超级数组,list底层实现模仿
- Java超级基础之集合与数组间的转换
- CodeForces 844A Diversity (超级超级水唯一需要想得就是开个标记数组吧)
- 关于C语言声明、指针、数组、函数、typedef等等的一通“超级扯”
- php9个超级全局数组变量和7个系统常量
- 包含《三目运算、BT交换、数组查询》的超级练习!
- 包含《三目运算、BT交换、数组查询》的超级练习! --来自 蚊子的专栏
- php超级预定数组( 类似javaWeb中域对象),函数,对象,进一步加深知识点
- HDU 4417 超级马里奥 数据结构+利用树状数组进行快速统计+多维统计转换
- json数组如何转换成string类型(超级好用)
- MFC获取Tree Control当前选择的值
- ajax讲解和入门实例
- 笔记本用无线路由器上网设置教程攻略
- 有没有有效及简便的方法来防止浏览器记住用户名及密码?
- SQL将一个数据库的某一张表移动到另外一个数据库中(表的结构不变)
- 超级数组
- try catch throw用法
- Android共享Activity,并设置无主入口启动图标的方式。
- 野猪和家猪的故事
- spring mvc 输出 json 异常处理
- 最近发现了一个高清计算机电子书和源码的网址
- vim退出后恢复终端内容
- UNIX Fork函数的理解
- 区