poj2828 Buy Tickets

PS: 起初,很多人说这道题是一道线段树题,可是为什么呢?我一直没有头绪,后来看了解题报告的第一行,我明白了。原来这道题就是基本的线段树。

那个解题报告的第一行写着:倒叙插入,位置就是前边的空格数。如果对线段树了解的话,那你也就明白了。

废话不说了,上代码……

View Code
 1 #include<iostream>
 2 #include<string>
 3 #include<queue>
 4 #include<map>
 5 #include<stack>
 6 #include<cmath>
 7 #include<functional>
 8 #include<algorithm>
 9 using namespace std;
10 #define lson l , m , rt << 1
11 #define rson m + 1 , r , rt << 1 | 1
12 const int maxn = 200010;
13 int sum[maxn<<2];
14 int result[maxn];
15 int n;
16 pair<int,int> order[maxn];
17 
18 int operate(int a,int b){
19     return a+b;
20 }
21 
22 
23 void PushUp(int rt){
24     sum[rt]=operate(sum[rt<<1],sum[rt<<1|1]);
25 }
26 
27 void bulid(int l=1,int r=n,int rt=1){
28     if(l==r){
29         sum[rt]=1;return ;
30     }
31     int m=(l+r)>>1;
32     bulid(lson);
33     bulid(rson);
34     PushUp(rt);
35 }
36 
37 void update(int p,int add,int l=1,int r=n,int rt=1){
38     if(l==r){
39         sum[rt]=0;
40         result[l]=add;
41         return ;
42     }
43     int m=(l+r)>>1;
44     if(p<sum[rt<<1])update(p,add,lson);
45     else update(p-sum[rt<<1],add,rson);
46     PushUp(rt);
47 }
48 
49 int main(){
50     
51     int tmp[maxn];
52     while(~scanf("%d",&n)){
53         bulid();
54 
55         for(int i=0;i<n;i++){
56             scanf("%d%d",&order[i].first,&order[i].second);
57         }
58         for(int i=n-1;i>=0;i--){
59             update(order[i].first,order[i].second);
60         }
61         for(int i=1;i<=n;i++){
62             printf("%d%c",result[i],(i^n)?' ':'\n');
63         }
64         
65     }
66 
67     return 0;
68 }

转载于:https://www.cnblogs.com/tiankonguse/archive/2012/07/27/2611986.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值