线段树


Description

N (2 <= N <= 8,000) cows have unique brands in the range 1..N. In a spectacular display of poor judgment, they visited the neighborhood 'watering hole' and drank a few too many beers before dinner. When it was time to line up for their evening meal, they did not line up in the required ascending numerical order of their brands.

Regrettably, FJ does not have a way to sort them. Furthermore, he's not very good at observing problems. Instead of writing down each cow's brand, he determined a rather silly statistic: For each cow in line, he knows the number of cows that precede that cow in line that do, in fact, have smaller brands than that cow.

Given this data, tell FJ the exact ordering of the cows.

Input

  • Line 1: A single integer, N

  • Lines 2..N: These N-1 lines describe the number of cows that precede a given cow in line and have brands smaller than that cow. Of course, no cows precede the first cow in line, so she is not listed. Line 2 of the input describes the number of preceding cows whose brands are smaller than the cow in slot #2; line 3 describes the number of preceding cows whose brands are smaller than the cow in slot #3; and so on.

Output

  • Lines 1..N: Each of the N lines of output tells the brand of a cow in line. Line #1 of the output tells the brand of the first cow in line; line 2 tells the brand of the second cow; and so on.

Sample Input

5

1

2

1

0

Sample Output

2

4

5

3

1


题目大意

1 ~ n 中的 n 个数以某一个顺序排列,给出这个排列中对于于第 2 到第 n 个数,在它前面并且比它小的数字个数。让你输出这个排列。

题解

这道题比较基本,和poj2828很相似,我就直接用那道题的代码改了。用线段树来维护每个数是否出现过。

从后往前更新。

例如样例 (0), 1, 2, 1, 0;

先更新第 5 个位置,查询第 1 个还没出现过的数字,得到 1,将其赋值为 0 (已出现过),更新ans[5] = 1;

然后更新第 4 个位置,查询第 2 个没出现过的数字,得到 3 ,更新ans[4] = 3;

......剩下的应该知道怎么做了吧,线段树中的值表示该区间中没有出现过的数的个数。

代码

#include <iostream>
using namespace std;
#define pushup(u) {sum[u] = sum[u<<1] + sum[u<<1|1];}
#define ls u<<1,l,mid
#define rs u<<1|1,mid+1,r const int maxn = 2e5 + 5;
int sum[maxn << 2];
int ans[maxn];
int pos[maxn]; void build(int u,int l,int r) {
sum[u] = r - l + 1;
if(l == r)return;
int mid = (l + r) >> 1;
build(ls);
build(rs);
} int update(int u,int l,int r,int x,int a) {
if(l == r){
sum[u] = 0;
return l;
}
int res;
int mid = (l + r) >> 1;
if(sum[u << 1] >= x)res = update(ls,x,a);
else res = update(rs,x - sum[u<<1],a);
pushup(u);
return res;
} int main() {
ios::sync_with_stdio(false); cin.tie(0);
int n;
while(cin >> n) {
build(1,1,n);
pos[1] = 0;
for(int i = 2;i <= n;i++) {
cin >> pos[i];
}
for(int i = n;i > 0;i--) {
ans[i] = update(1,1,n,pos[i] + 1,i);
}
for(int i = 1;i <= n;i++) {
cout << ans[i] << endl;
}
} return 0;
}

[poj2182] Lost Cows (线段树)的更多相关文章

  1. POJ 2481 Cows (线段树)

    Cows 题目:http://poj.org/problem?id=2481 题意:有N头牛,每仅仅牛有一个值[S,E],假设对于牛i和牛j来说,它们的值满足以下的条件则证明牛i比牛j强壮:Si &l ...

  2. POJ 2182 Lost Cows (线段树)

    题目大意: 有 n 头牛,编号为 1 - n 乱序排成一列,现已知每头牛前面有多少头牛比它的编号小,从前往后输出每头牛的编号. 思路: 从后往前推,假如排在最后的一头牛比他编号小的数量为a,那么它的编 ...

  3. Lost Cows(线段树 POJ2182)

    Lost Cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10354 Accepted: 6631 Descriptio ...

  4. [POJ2182]Lost Cows(树状数组,二分)

    题目链接:http://poj.org/problem?id=2182 题意:给定1~n个数和n个位置,已知ai表示第i个位置前有ai个数比当前位置的数小,求这个排列. 和刚才YY的题意蛮接近的,用树 ...

  5. hdu 2711&&poj2182 Lost Cows (线段树)

    从后往前查第一个为0的奶牛肯定应该排在第一个.每次从后往前找到第一个为0的数,这个数应该插在第j位.查找之后,修改节点的值为极大值,当整棵树的最小值不为0的时候查找结束. 至于这种查找修改的操作,再没 ...

  6. poj2182(线段树求序列第k小)

    题目链接:https://vjudge.net/problem/POJ-2182 题意:有n头牛,从1..n编号,乱序排成一列,给出第2..n个牛其前面有多少比它编号小的个数,记为a[i],求该序列的 ...

  7. POJ2182题解——线段树

    POJ2182题解——线段树 2019-12-20 by juruoOIer 1.线段树简介(来源:百度百科) 线段树是一种二叉搜索树,与区间树相似,它将一个区间划分成一些单元区间,每个单元区间对应线 ...

  8. POJ 2182&& POJ 2828:Lost Cows 从后往前 线段树

    Lost Cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10544   Accepted: 6754 Descri ...

  9. ACM/ICPC 之 数据结构-线段树思想(POJ2182,含O(n^2)插入式解法)

    这道题在一定程度上体现了线段树的一种用法,解决的问题是:对于总计n个元素的第i个元素,已知其在[1,i]上部分序列的排名,求第i个元素在所有n个元素中的排名. 当然这道题数据比较水,所以用O(n^2) ...

随机推荐

  1. Vanishing point detection

    https://marcosnietoblog.wordpress.com/code/

  2. Java语言程序设计(基础篇)第一章

    第一章 计算机.程序和Java概述 1.1 引言 什么是程序设计呢? 程序设计就是创建(或者开发)软件,软件也称为程序. 1.2 什么是计算机 计算机是存储和处理数据的电子设备,计算机包括硬件(har ...

  3. asp.net中http提交数据所遇到的那些坑

    http提交数据有两种形式,get和post,不知道的同学请联系度娘. 1.aspnet:MaxHttpCollectionKeys 业务场景:业务很简单,手机端读取本地通讯录,将所有通讯录提交到后台 ...

  4. Linux 下多用户申请git公钥方法

    问题:目前大家多是通过root用户来登录编译机,导致各自生成的公钥相互覆盖,而导致无法无法多人同时使用 解决方法: 登陆编译机添加用户   # useradd -m a00123456 进入切换为自己 ...

  5. 区间K 大数查询

      算法训练 区间k大数查询   时间限制:1.0s   内存限制:256.0MB 问题描述 给定一个序列,每次询问序列中第l个数到第r个数中第K大的数是哪个. 输入格式 第一行包含一个数n,表示序列 ...

  6. C++之路进阶codevs1269(匈牙利游戏)

    1269 匈牙利游戏 2012年CCC加拿大高中生信息学奥赛  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond       题目描述 Description ...

  7. 纯html、css3、js的时钟

    之前在网上看了一些使用js写的时钟,但感觉实现的方法有点麻烦,所以就自己重新写了一个例子,样子有点丑,但方法比较简单,大家就凑合看吧 其中采用的主要方法是原生js里面的Data(时期)对象,以及它的. ...

  8. 夺命雷公狗-----React---27--小案例之react经典案例todos(清除已完成)

    这个功能其实也是很简单的,就只是让todos里面的内isDown进行取反即可 然后在Zong里面进行下修改即可 效果如下所示: 代码如下所示: <!DOCTYPE html> <ht ...

  9. linux Centos下搭建gitolite服务器

    1.安装git sudo yum install git -y 2.添加git管理账号 sudo adduser git 3.将gitolite克隆到本地,并安装 sudo mkdir /var/gi ...

  10. 代码高亮美化插件-----SyntaxHighlighter

    IT类文章博客,代码高亮美化插件-----SyntaxHighlighter 最近在做一个类似个人博客的网站,因为文章中会用到各种代码,主要是Javascript,CSS,PHP,XML等.这些代码如 ...