• 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 }
  • 相关阅读:
    ubuntu18.04阿里源
    C# 去开头字符串
    思维导图 电商运营思路
    思维导图 淘宝淘宝流量来源
    学习 名词
    Java面试题之“==”和“equals()”方法的区别?
    修改本地MySQL的root身份密码
    添加并启动MySQL服务
    在centos7上进行hadoop-3.1.2的伪分布搭建
    TreeMap树映射取出对象的方式
  • 原文地址:https://www.cnblogs.com/tiankonguse/p/2611986.html
Copyright © 2020-2023  润新知