POJ 2828 线段树 逆序插入
思路:
1.线段树 逆着插入就OK了
2.块状链表 (可是我并不会写)
//By SiriusRen
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int n,xx,tree[1000050],cnt[1000050];
struct Node{int num,wei;}node[1000050];
void build(int l,int r,int pos,int id){
if(l==r){tree[pos]=1;if(id)printf("%d ",cnt[pos]);return;}
int mid=(l+r)>>1,lson=pos<<1,rson=pos<<1|1;
build(l,mid,lson,id),build(mid+1,r,rson,id);
tree[pos]=tree[lson]+tree[rson];
}
void insert(int l,int r,int pos){
if(l==r){tree[pos]=0,cnt[pos]=node[xx].wei;return;}
int mid=(l+r)>>1,lson=pos<<1,rson=pos<<1|1;
if(tree[lson]<=node[xx].num)node[xx].num-=tree[lson],insert(mid+1,r,rson);
else insert(l,mid,lson);
tree[pos]=tree[lson]+tree[rson];
}
int main(){
while(~scanf("%d",&n)){
for(int i=1;i<=n;i++)
scanf("%d%d",&node[i].num,&node[i].wei);
build(1,n,1,0);
for(xx=n;xx;xx--)insert(1,n,1);
build(1,n,1,1),putchar('\n');
}
}
POJ 2828 线段树 逆序插入的更多相关文章
- poj 2828(线段树单点更新)
Buy Tickets Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 18561 Accepted: 9209 Desc ...
- poj 2828 线段树
http://poj.org/problem?id=2828 学到的思维: 1.变化的或者后来的优先影响前面的,那么从最后一个往前看,最后一个就成了 确定的, 而且后来的也能够确定----假设从前往后 ...
- POJ 2828 线段树(想法)
Buy Tickets Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 15422 Accepted: 7684 Desc ...
- poj 2828(线段树 逆向思考) 插队是不好的行为
http://poj.org/problem?id=2828 插队问题,n个人,下面n行每行a,b表示这个人插在第a个人的后面和这个人的编号为b,最后输出队伍的情况 涉及到节点的问题可以用到线段树,这 ...
- POJ-2299 Ultra_QuickSort 线段树+逆序对数
Ultra-QuickSort Time Limit: 7000MS Memory Limit: 65536K Total Submissions: 50737 Accepted: 18595 Des ...
- POJ 2828 线段树活用
题目大意:依次描述了一个N个人的队伍,每个人所站的序号以及他的价值,依次描述每个人的过程中,存在序号相同的人,表示该人插入到了前一个序号相同的人的前面.最后输出整个队伍的值排列情况. 这个题目确实难以 ...
- POJ 2828 (线段树 单点更新) Buy Tickets
倒着插,倒着插,这道题是倒着插! 想一下如果 Posi 里面有若干个0,那么排在最前面的一定是最后一个0. 从后往前看,对于第i个数,就应该插在第Posi + 1个空位上,所以用线段树来维护区间空位的 ...
- (中等) POJ 2828 Buy Tickets , 逆序+线段树。
Description: Railway tickets were difficult to buy around the Lunar New Year in China, so we must ge ...
- POJ 2828 线段树单点更新 离线搞
Description Railway tickets were difficult to buy around the Lunar New Year in China, so we must get ...
随机推荐
- Picking up Jewels
Picking up Jewels There is a maze that has one entrance and one exit. Jewels are placed in passages ...
- ThinkPHP5.0框架开发--第8章 TP5.0 模型
ThinkPHP5.0框架开发--第8章 TP5.0 模型 第8章 TP5.0 模型 ================================================= 今日学习 1. ...
- 12.boost有向图无向图(矩阵法)
#include <iostream> #include <boost/config.hpp> //图 #include <boost/graph/adjacency_m ...
- Windows下本机简易监控系统搭建(Telegraf+Influxdb+Grafana)--转
原文地址:http://www.cnblogs.com/liugh/p/6683488.html 一.文件准备 1.1 文件名称 telegraf-1.2.1_windows_amd64.zip in ...
- Bringing up the Avnet MicroZed with Vivado
Bringing up the Avnet MicroZed with Vivado I recently received the Adam Taylor Edition of Avnet's Zy ...
- Java Servlet 3.0 新特性
Servlet 3.0 新特性概述 Servlet 3.0 作为 Java EE 6 规范体系中一员,随着 Java EE 6 规范一起发布.该版本在前一版本(Servlet 2.5)的基础上提供了若 ...
- 51nod 1402 最大值 3级算法题 排序后修改限制点 时间复杂度O(m^2)
代码: 题意,第一个数为0,相邻的数相差0或者1,有一些点有限制,不大于给定值,求这组数中可能的最大的那个数. 这题我们看一个例子:第5个数的限制为2 1 2 3 4 5 6 7 8 9 0 1 2 ...
- Java数据库连接——jdbc-odbc桥连接方式及汉字乱码问题
jdbc-odbc桥连接方式操作数据库SU(Course),其中Course属性有Cno,Cname,Cpno,Ccredit. 步骤: 1.配置数据源 控制面板下搜索管理工具->ODBC数据源 ...
- 分库分表中间件Sharding-JDBC
数据库分库分表从互联网时代开启至今,一直是热门话题.在NoSQL横行的今天,关系型数据库凭借其稳定.查询灵活.兼容等特性,仍被大多数公司作为首选数据库.因此,合理采用分库分表技术应对海量数据和高并发对 ...
- asp.net 连接字符串的多种写法
一.使用OleDbConnection对象连接OLE DB数据源 1.连接Access 数据库 Access 2000: “provider=Microsoft.Jet.Oledb.3.5;Data ...