zoukankan      html  css  js  c++  java
  • 【POJ 2828】Buy Tickets

    【题目链接】

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

    【算法】

               离线用线段树维护序列即可

    【代码】

              

    #include <algorithm>  
    #include <bitset>  
    #include <cctype>  
    #include <cerrno>  
    #include <clocale>  
    #include <cmath>  
    #include <complex>  
    #include <cstdio>  
    #include <cstdlib>  
    #include <cstring>  
    #include <ctime>  
    #include <deque>  
    #include <exception>  
    #include <fstream>  
    #include <functional>  
    #include <limits>  
    #include <list>  
    #include <map>  
    #include <iomanip>  
    #include <ios>  
    #include <iosfwd>  
    #include <iostream>  
    #include <istream>  
    #include <ostream>  
    #include <queue>  
    #include <set>  
    #include <sstream>  
    #include <stdexcept>  
    #include <streambuf>  
    #include <string>  
    #include <utility>  
    #include <vector>  
    #include <cwchar>  
    #include <cwctype>  
    #include <stack>  
    #include <limits.h> 
    using namespace std;
    #define MAXN 200010
    
    int i,n;
    int pos[MAXN],val[MAXN];
    
    struct SegmentTree
    {
        struct Node
        {
            int l,r;
            int cnt,val;
        } Tree[MAXN<<2];
        inline void build(int index,int l,int r)    
        {
            int mid;
            Tree[index].l = l;
            Tree[index].r = r;
            Tree[index].cnt = r - l + 1;
            if (l == r) return;
            mid = (l + r) >> 1;
            build(index<<1,l,mid);
            build(index<<1|1,mid+1,r);
        }
        inline void update(int index)
        {
            Tree[index].cnt = Tree[index<<1].cnt + Tree[index<<1|1].cnt;
        }
        inline void insert(int index,int pos,int val)
        {
            int mid;
            if (Tree[index].l == Tree[index].r) 
            {
                Tree[index].cnt = 0;
                Tree[index].val = val;
                return;
            }
            mid = (Tree[index].l + Tree[index].r) >> 1;
            if (Tree[index<<1].cnt >= pos) insert(index<<1,pos,val);
            else insert(index<<1|1,pos - Tree[index<<1].cnt,val);
            update(index);
        }
        inline void output(int index)
        {
            int mid;
            if (Tree[index].l == Tree[index].r) printf("%d ",Tree[index].val);
            else
            {
                output(index<<1);
                output(index<<1|1);
            }
        }
    } T;
    
    int main()
    {
        
        while (scanf("%d",&n) != EOF)
        {
            for (i = 1; i <= n; i++) scanf("%d%d",&pos[i],&val[i]);
            T.build(1,1,n);
            for (i = n; i >= 1; i--) T.insert(1,pos[i]+1,val[i]);
            T.output(1);
            printf("
    ");
        }
    
        return 0;
    }
  • 相关阅读:
    URAL 1998 The old Padawan 二分
    URAL 1997 Those are not the droids you're looking for 二分图最大匹配
    URAL 1995 Illegal spices 贪心构造
    URAL 1993 This cheeseburger you don't need 模拟题
    URAL 1992 CVS
    URAL 1991 The battle near the swamp 水题
    Codeforces Beta Round #92 (Div. 1 Only) A. Prime Permutation 暴力
    Codeforces Beta Round #7 D. Palindrome Degree hash
    Codeforces Beta Round #7 C. Line Exgcd
    Codeforces Beta Round #7 B. Memory Manager 模拟题
  • 原文地址:https://www.cnblogs.com/evenbao/p/9316525.html
Copyright © 2011-2022 走看看