POJ 2828 Buy Tickets(线段树)
来源:互联网 发布:mac怎么玩qq飞车 编辑:程序博客网 时间:2024/06/16 22:45
题目链接:http://poj.org/problem?id=2828
题目大意:给定n对数字 p[i], v[i](数据量小于2*10^5)。p为插入时到排首的距离,v为插入的值,问最后的队伍的顺序是如何的。
解析:插入时倒序插入,那么直接可以确定其位置,为队首后p[i]个空位处,如果直接使用数列每次都遍历求得位置,会TLE。利用线段树,定义每个节点的数值的含义为:当前节点范围内的叶子节点有多少是空的,通过update函数找到符合当前所需要空位的位置。
知识点:线段树单点更新。
AC代码如下:
#include <iostream>#pragma comment(linker, "/STACK:1024000000,1024000000") #include <stdio.h>#include <fstream>#include <iomanip>#include <cmath>#include <string>#include <string.h>#include <sstream>#include <cctype>#include <climits>#include <set>#include <map>#include <deque>#include <queue>#include <vector>#include <iterator>#include <algorithm>#include <stack>#include <functional>//cout << "OK" << endl;#define _clr(x,y) memset(x,y,sizeof(x))#define _inf(x) memset(x,0x3f,sizeof(x))#define pb push_back#define mp make_pair#define FORD(i,a,b) for (int i=(a); i<=(b); i++)#define FORP(i,a,b) for (int i=(a); i>=(b); i--)#define REP(i,n) for (int i=0; i<(n); i++)using namespace std;const int INF = 0x3f3f3f3f;const double eps = 1e-8;const double EULER = 0.577215664901532860;const double PI = 3.1415926535897932384626;const double E = 2.71828182845904523536028;typedef long long LL;LL pow_mod(LL a,LL n,LL m){ if(n == 0) return 1; LL x = pow_mod(a,n>>1,m); LL ans = x*x%m; if(n&1) ans = ans*a%m; return ans;}int gcd(int a,int b){return b == 0 ? a : gcd(b,a%b);}int ans[2000010];int arr[2000010<<2];void pushup(int node){ arr[node] = arr[node<<1] + arr[node<<1|1]; //当前节点管理下,有多少空位}void build(int l, int r, int node){ if(l == r){ arr[node] = 1; //初始化将每个叶子节点定义为1 return; } int m = (l+r)>>1; build(l,m,node<<1); build(m+1,r,node<<1|1); pushup(node);}void update(int p, int v, int l, int r, int node){ if(l == r){ arr[node] = 0; ans[l] = v; return; } //每次寻找到队首的第p[i]个空位处,将v[i]插入 int m = (l+r)>>1; if(p <= arr[node<<1]){ update(p,v,l,m,node<<1); //如果左节点管理的空位大于等于当前所需要的,那么更新左子树 } else update(p-arr[node<<1],v,m+1,r,node<<1|1); //如果左边节点的管理的空位小于当前所需要的,那么将需要的空位p[i]减去左边节点代表的已经有的空位,更新右子树。 pushup(node); }int p[200020], v[200020];int main(){ int n; while(cin >> n){ build(1,n,1); for(int i = 1; i <= n; i++){ scanf("%d%d",&p[i],&v[i]); p[i]++; } for(int i = n; i > 0; i--){ update(p[i],v[i],1,n,1); } for(int i = 1; i <= n; i++){ printf("%d ",ans[i]); } cout << endl; } return 0;}
0 0
- poj 2828 Buy Tickets(线段树)
- POJ 2828 Buy Tickets(线段树)
- poj 2828 Buy Tickets(线段树)
- POJ 2828 Buy Tickets(线段树)
- poj 2828 Buy Tickets (线段树)
- POJ 2828--Buy Tickets(线段树)
- POJ 2828 Buy Tickets(线段树)
- POJ 2828 Buy Tickets(线段树)
- POJ 2828 Buy Tickets(线段树)
- POJ 2828 Buy Tickets(线段树)
- POJ - 2828 - Buy Tickets (线段树)
- POJ 2828 Buy Tickets(线段树)
- poj 2828 Buy Tickets(线段树)
- POJ 2828 Buy Tickets(线段树)
- POJ 2828 Buy Tickets(线段树)
- poj-2828-Buy Tickets(线段树)
- poj 2828 Buy Tickets(线段树)
- POJ 2828 Buy Tickets(线段树)
- 你真的会用UIButton吗? UIButton详细介绍
- POJ1664(递归,放苹果)
- 关于使用Jquery的appen动态添加元素的事件绑定
- Mac的FTP下载正常,但上传失败,超时
- HDU 2147 kiki's game (巴什博弈、PN图)
- POJ 2828 Buy Tickets(线段树)
- Java容器Set详解
- 数状数组浅析
- 典型的服务器模型
- 第三章:linear models for regression exericse 17-24
- Android获取SDCard路径/Android获取存储器挂载点
- 嵌入式学习-驱动开发-lesson4-按键混杂设备驱动
- python入门
- Android中使用surfaceview+MediaPlayer播放视频