POJ 2182 线段树

链接:

http://poj.org/problem?id=2182

题意:

有N头牛,编号1~N,乱序排成一列,现在已知每头牛前面有多少头牛比它的编号小,

求排队后从前往后数,每头牛的编号

题解:

从后往前扫描,遇到数字a,说明它是剩余序列中的第a+1个数,找到改编号并删除。

代码:

31 int a[MAXN];
32 int tree[MAXN];
33 
34 void pushup(int rt) {
35     tree[rt] = tree[rt << 1] + tree[rt << 1 | 1];
36 }
37 void build(int l, int r, int rt) {
38     if (l == r) {
39         tree[rt] = 1;
40         return;
41     }
42     int m = (l + r) >> 1;
43     build(lson);
44     build(rson);
45     pushup(rt);
46 }
47 
48 int query(int x, int l, int r, int rt) {
49     tree[rt]--;
50     if (l == r) return l;    
51     int m = (l + r) >> 1;
52     if (x <= tree[rt << 1]) return query(x, lson);
53     else return query(x - tree[rt << 1], rson);
54 }
55 
56 int main() {
57     int n;
58     cin >> n;
59     build(1, n, 1);
60     rep(i, 1, n) cin >> a[i];
61     per(i, 0, n) a[i] = query(a[i] + 1, 1, n, 1);
62     rep(i, 0, n) cout << a[i] << endl;
63     return 0;
64 }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值