POJ2828
来源:互联网 发布:mysql schema 编辑:程序博客网 时间:2024/04/28 17:23
一道线段树的好题..poj2828
刚开始学线段树,感觉进度有点落后了…
每个人都有一个value值和pos值,进入队伍时会把pos上的人挤到后面去。
考虑逆序建立线段树,因为这样插入之后可以保证位置不会再被改变。
插入时,如果左子节点已经站不下了,就站到右子节点。
#include<iostream>#include<cstring>#include<cstdlib>#include<cstdio>#include<cmath>#include<string>#include<algorithm>#include<vector>using namespace std;#define MAXN 200010#define lson rt<<1#define rson rt<<1 | 1 #define mid int m = (l + r) >> 1int x[MAXN<<2];int map[MAXN<<2],n;int re[MAXN][2];void build(int l,int r,int rt){ if(l == r) { x[rt] = 1; return; } int m = (l + r) >> 1; build(l,m,lson); build(m+1,r,rson); x[rt] = x[lson] + x[rson];}void update(int l,int r,int rt,int val,int pos){ //cout<<l<<' '<<r<<' '<<rt<<' '<<val<<' '<<pos<<endl; if(l == r) { map[rt] = val; x[rt] --; return; } mid; if(x[lson] > pos) update(l,m,lson,val,pos); else update(m+1,r,rson,val,pos-x[lson]); x[rt] = x[lson] + x[rson];}void print_ans(int l,int r,int rt){ if(l == r) { if(l == n -1) cout<<map[rt]<<endl; else cout<<map[rt]<<' '; return; } mid; print_ans(l,m,lson); print_ans(m+1,r,rson);}int main(){ ios::sync_with_stdio(false); while(cin>>n){ build(0,n-1,1); for(int i = 0;i<n;i++){ cin>>re[i][0]>>re[i][1]; } for(int i = n - 1;i>-1;i--) update(0,n-1,1,re[i][1],re[i][0]); print_ans(0,n-1,1); }}
0 0
- POJ2828
- poj2828
- poj2828
- poj2828
- poj2828
- poj2828
- poj2828
- POJ2828
- poj2828
- poj2828
- poj2828
- poj2828
- POJ2828
- poj2828
- POJ2828 插队
- 【poj2828】无键跳表
- poj2828 买票
- poj2828【线段树】
- jni编译时错误在console里一闪而过
- ProgressDialog详解
- 运行QEMU时,出现错误:(process:8559): GLib-WARNING...gmem.c:483: custom memory allocation vtable not support
- 甲骨文研酒爰精密解讀:甲在畀中
- 张正友标定算法原理详解
- POJ2828
- 用feof()判断文件时出现多读内容的问题
- gradle -v时报ERROR:JAVA_HOME is set to an invalid directory xxx解决方法
- compile single
- 开放api接口签名验证
- HDU Function Run Fun
- iOS项目代码行数统计
- 第七章 上级三
- swift3.0_第一篇tableView