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
    和hdu5592一样的题目。hdu5592的博客
    代码如下:

#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<set>
#define ll long long
using namespace std;const int maxx=1e4+100;
struct node{int l;int r;int sum;
}p[maxx<<2];
int a[maxx],b[maxx];
int n;inline void pushup(int cur)
{p[cur].sum=p[cur<<1].sum+p[cur<<1|1].sum;
}
inline void build(int l,int r,int cur)
{p[cur].l=l;p[cur].r=r;if(l==r) {p[cur].sum=1;return ;}int mid=l+r>>1;build(l,mid,cur<<1);build(mid+1,r,cur<<1|1);pushup(cur);
}
inline void update(int pos,int cur)
{int L=p[cur].l;int R=p[cur].r;if(L==R){p[cur].sum=0;return ;}int mid=L+R>>1;if(pos<=mid) update(pos,cur<<1);else update(pos,cur<<1|1);pushup(cur);
}
inline int query(int cur,int k)
{int L=p[cur].l;int R=p[cur].r;if(L==R) return L;if(k<=p[cur<<1].sum) return query(cur<<1,k);else return query(cur<<1|1,k-p[cur<<1].sum);
}
int main()
{int x;scanf("%d",&n);set<int> s;for(int i=1;i<n;i++) scanf("%d",&a[i]);build(1,n,1);for(int i=n-1;i>=1;i--){b[i]=query(1,a[i]+1);update(b[i],1);}printf("%d\n",query(1,1));for(int i=1;i<n;i++) printf("%d\n",b[i]);return 0;
}

努力加油a啊,(o)/~

Lost Cows POJ - 2182(线段树)相关推荐

  1. POJ 2481 Cows POJ 2352 Stars(树状数组妙用)

    题目链接:POJ 2481 Cows POJ 2352 Stars 发现这两个题目都跟求逆序数有着异曲同工之妙,通过向树状数组中插入点的位置,赋值为1,或者++,然后通过求和来判断比当前 点 &quo ...

  2. POJ 3468 线段树+lazy标记

    lazy标记   Time Limit:5000MS     Memory Limit:131072KB     64bit IO Format:%I64d & %I64u  Submit S ...

  3. POJ 3264 线段树

    题意 传送门 POJ 3264 题解 线段树维护区间的最大值和最小值即可. #include <cstdio> #include <cstring> #include < ...

  4. poj(2325)线段树

    这里介绍另外一种解法,此题可以用线段树,可以用树状数组 其实这题求的都是下面的和左面的,线段树这种数组结构刚好可以满足,为什么呢?这里稍微解释下吧,也有助于以后的复习 看上面这个图,[1,1],[2, ...

  5. poj 3468 线段树

    线段树的 建立build(初始化+左右相等+两个递归+别忘了sum)+更新update(递归出口+更新delta+三向递归+修正当前节点的value)+查找query(如果左右相等+更新delta+三 ...

  6. POJ - 2528 线段树+离散化

    其实很早就在白书上的常用技巧上 看到离散化的操作,但是之前一直没遇到过需要离散化的题目(应该是我太菜的缘故),所以一直也没怎么重视,下面说说这道题目的考点,也就是离散化. 什么是离散化呢?请先自行百度 ...

  7. poj 2777(线段树+区间染色)

    解题思路:这道题利用了线段树+位运算的思想,由于颜色的种类只有30种,所以int可以存下来,所以我们在线段树的节点里面加上status的状态信息,表示这段区间内的颜色信息,而且我们可以知道,父节点的s ...

  8. poj 2352 线段树

    注意到题目中给的y是递增的,那个y没什么用,直接线段树维护一段1,x的区间. #include<string> #include<iostream> #include<c ...

  9. A Simple Problem with Integers POJ - 3468 (线段树)

    思路:线段树,区间更新,区间查找 1 #include<iostream> 2 #include<vector> 3 #include<string> 4 #inc ...

最新文章

  1. c+和python哪个快-python比c更快
  2. fsimage文件丢失_Fsimage 与 EditLog定义及合并过程
  3. 索引访问方法及索引优化
  4. Linux stat命令总结
  5. python删除重复文字_python如何删除文件中重复的字段
  6. java中 private final_Java笔记:final与private关键字
  7. 自定义动画属性java_创建酷炫动画效果的10个JavaScript库
  8. [u]intN_t - uint8_t, uint16_t, uint32_t, uint64_t
  9. Spring的入门学习笔记 (注解)
  10. Flex4_操作XML
  11. 用计算机弹奇迹再现谱子,《原神》奇迹再现乐谱分享 风物之诗琴乐谱分享
  12. boost boost::asio::read read_some receive 区别
  13. 电脑重装系统U盘引导不了
  14. setitime和相关函数
  15. 第一次结对作业:原型设计
  16. 隐藏win11任务栏时间,开启专注模式
  17. 机器人酷跑电脑版下载地址_机器人酷跑游戏
  18. 还在原地踏步,提高软件测试能力的方法你知道吗?
  19. 通过文件流解压压缩包
  20. flink生成Watermark之WatermarkStrategy

热门文章

  1. ios基础之归档和解档
  2. matlab 判断一个数组中有没有重复的元素
  3. python 字典取值的时间复杂度_五道常见的Python面试题一定不要答错!
  4. linux中动态链接库用扩展名,Linux操作系统下动态库的生成及链接方法是什么?...
  5. 圆点html span,HTML span 标签
  6. 如何防御syn flood的一些思路!
  7. could not perform addBatch
  8. Linux下SVN安装配置全程实录(转)
  9. 会话的清除与建立网络磁盘
  10. Oracle命令(二):Oracle数据库几种启动和关闭方式