【题解】

参考https://blog.csdn.net/acmer_hades/article/details/46272605。设置数组pre_smaller,其中第i个元素即为输入的第i项,则显然pre_smaller[1] = 0。build_tree建立树结构,分解区间[1, N],其中每个节的度要么为2,要么为0。query中参数smaller_num表示自身以及队列前方比自己编号小的cows的总数。确实是非常简洁的方法,将线段数组运用得淋漓尽致!不愧是NOI大神

【代码】

 #include <iostream>
#include <cstdlib>
using namespace std; #define maxn 100001 int N;
int pre_smaller[maxn], ans[maxn]; struct node
{
int left_val, right_val, len;
}s[*maxn]; void build_tree(int root, int left_val, int right_val)
{
s[root].left_val = left_val;
s[root].right_val = right_val;
s[root].len = right_val - left_val + ;
if (left_val == right_val)return;
build_tree( * root, left_val, (left_val + right_val) / );
build_tree( * root + , + (left_val + right_val) / , right_val);
} int query(int root, int smaller_num)
{
s[root].len--;
if (s[root].left_val == s[root].right_val)return s[root].left_val;
else if (s[ * root].len >= smaller_num)return query( * root, smaller_num);
else return query( * root + , smaller_num - s[ * root].len);
} int main()
{
cin >> N;
for (int i = ; i <= N; i++)cin >> pre_smaller[i];
pre_smaller[] = ;
build_tree(, , N);
for (int i = N; i >= ; i--)ans[i] = query(, pre_smaller[i] + );
for (int i = ; i <= N; i++)printf("%d\n", ans[i]);
return ;
}

POJ Lost Cows的更多相关文章

  1. 树状数组 POJ 2481 Cows

    题目传送门 #include <cstdio> #include <cstring> #include <algorithm> using namespace st ...

  2. POJ 2481 Cows

    Cows Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 16546   Accepted: 5531 Description ...

  3. 2018.07.08 POJ 2481 Cows(线段树)

    Cows Time Limit: 3000MS Memory Limit: 65536K Description Farmer John's cows have discovered that the ...

  4. POJ 2481 Cows (线段树)

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

  5. POJ 2481 Cows(树状数组)

                                                                      Cows Time Limit: 3000MS   Memory L ...

  6. POJ 3348 - Cows 凸包面积

    求凸包面积.求结果后不用加绝对值,这是BBS()排序决定的. //Ps 熟练了template <class T>之后用起来真心方便= = //POJ 3348 //凸包面积 //1A 2 ...

  7. POJ 2186-Popular Cows (图论-强联通分量Korasaju算法)

    题目链接:http://poj.org/problem?id=2186 题目大意:有n头牛和m对关系, 每一对关系有两个数(a, b)代表a牛认为b牛是“受欢迎”的,且这种关系具有传递性, 如果a牛认 ...

  8. POJ 2481 Cows (数组数组求逆序对)

    题目链接:http://poj.org/problem?id=2481 给你n个区间,让你求每个区间被真包含的区间个数有多少,注意是真包含,所以要是两个区间的x y都相同就算0.(类似poj3067, ...

  9. POJ 3621Sightseeing Cows

    Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9851   Accepted: 3375 Description Farme ...

  10. POJ 3348 Cows [凸包 面积]

    Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9022   Accepted: 3992 Description ...

随机推荐

  1. 100 webhook implementations

    转自: https://streamdata.io/blog/100-webhook-implementations/  很不错的整理 What is the scope of the event-d ...

  2. PHP 统计一维数组value同样的元素的个数num,并将其转化为下标为数字,值是value和num的二维数组

    近期做一个项目.从数据库查询某个字段得到一个数组key是数字值是channel的一维数组$res,现须要将这个数组变成键是数字值是channel和num(num为同样channel的数量,默觉得0). ...

  3. UWA 技术分享连载 转载

    技术分享连载1 Q1:Texture占用内存总是双倍,这个是我们自己的问题,还是Unity引擎的机制? Q2:我现在发现两个因素直接影响Overhead,一个是Shader的复杂度,一个是空Updat ...

  4. Python 不可变对象练习

    Python 不可变对象练习 str 是不可变对象,就是对这个对象进行操作不会改变这个对象的数据. 如下: >>> a = 'abc' >>> a.replace( ...

  5. 如何使用百度bae部署web项目

    百度bae提供了支持各种开发环境的的应用引擎,包括node.js.php.java等,而且还免费提供了一定容量的mysql.mongodb.redis等数据库,所以,可以把它当作一个云服务器来使用.而 ...

  6. 通过 DDNS 解决宽带拨号 ip 变化问题

    前面你的文章我已经写了 写了 DMZ 内网映射的 方式. 这样内网主机已经暴露在外网中了. 但是 拨号上网我们的ip是 会变化的.大概规律就是 每次拨号都会变化.如果不拨号,每 24 小时 ip也会自 ...

  7. mwan3多wan叠加成功

    参考: http://www.right.com.cn/forum/forum.php?mod=viewthread&tid=147109&highlight=mwan3 wan口为路 ...

  8. 使用vivado将bit文件转化为mcs文件

    使用vivado将bit文件转化为mcs文件 1.在Tcl Console中运行脚本: write_cfgmem -force -format MCS -size 64 -interface spix ...

  9. debian下erlang新版本安装

    debian下的erlang版本太老 安装kerl,并且在profile内添加到PATH curl -o ~/dev/erlang/kerl https://raw.githubusercontent ...

  10. vi 编辑器基本命令

    命令模式(esc) k 上移一行j 下移一行h 左移一行l 右移一行 6j  下移6行 5k 上移5行 0 将游标放在一行的开始$ 将游标放在一行的末尾w 将游标移动到下一个单词b 将游标移动到上一个 ...