zoukankan      html  css  js  c++  java
  • Cinema in Akiba(线段树)

    I - Cinema in Akiba

    Cinema in Akiba (CIA) is a small but very popular cinema in Akihabara. Every night the cinema is full of people. The layout of CIA is very interesting, as there is only one row so that every audience can enjoy the wonderful movies without any annoyance by other audiences sitting in front of him/her.

    The ticket for CIA is strange, too. There are n seats in CIA and they are numbered from 1 to n in order. Apparently, n tickets will be sold everyday. When buying a ticket, if there are k tickets left, your ticket number will be an integer i (1 ≤ i ≤ k), and you should choose the ith empty seat (not occupied by others) and sit down for the film.

    On November, 11th, n geeks go to CIA to celebrate their anual festival. The ticket number of the ith geek is ai. Can you help them find out their seat numbers?

    Input
    The input contains multiple test cases. Process to end of file.
    The first line of each case is an integer n (1 ≤ n ≤ 50000), the number of geeks as well as the number of seats in CIA. Then follows a line containing n integers a1, a2, ..., an (1 ≤ ai ≤ n - i + 1), as described above. The third line is an integer m (1 ≤ m ≤ 3000), the number of queries, and the next line is m integers, q1, q2, ..., qm (1 ≤ qi ≤ n), each represents the geek's number and you should help him find his seat.

    Output
    For each test case, print m integers in a line, seperated by one space. The ith integer is the seat number of the qith geek.

    Sample Input
    3
    1 1 1
    3
    1 2 3
    5
    2 3 3 2 1
    5
    2 3 4 5 1
    Sample Output
    1 2 3
    4 5 3 1 2

    题意:每个人要做第a[i]的空位,最后查询每一个人的座位。

    思路:线段树单点更新,单点查询。

    #include<map>
    #include<stack>
    #include<queue>
    #include<math.h>
    #include<vector>
    #include<string>
    #include<stdio.h>
    #include<iostream>
    #include<string.h>
    #include<algorithm>
    #define LL long long
    #define inf 0x3f3f3f
    #define maxn 50005
    #define mem(a,b) memset(a,b,sizeof(a))
    #define root  1,n,1
    #define lson  l,mid,rt<<1
    #define rson  mid+1,r,rt<<1|1
    using namespace std;
    int n,m,x,anss;
    int sum[maxn<<2];
    int ans[maxn];
    struct node{
        int l,r,id;
        int mid(){
            return (l+r)/2;
        }
    }tree[maxn<<2];
    void pushup(int rt){
        sum[rt]=sum[rt<<1]+sum[rt<<1|1];
    }
    void build(int l,int r,int rt){
        tree[rt].l=l;
        tree[rt].r=r;
        if(l==r){
        tree[rt].id=x;
        sum[rt]=1;
        x++;
        return;}
        int mid=tree[rt].mid();
        build(lson);
        build(rson);
        pushup(rt);
    }
    void update(int l,int r,int rt,int c){
        if(l==r&&sum[rt]==c){
            sum[rt]=0;
            anss=tree[rt].id;
            return;
        }
        int mid=tree[rt].mid();
        if(sum[rt<<1]>=c) update(lson,c);
        else
             update(rson,c-sum[rt<<1]);
        pushup(rt);
    }
    int main(){
        while(~scanf("%d",&n)){
            x=1;int y;
            mem(ans,0);mem(sum,0);mem(tree,0);
            build(root);
            for(int i=0;i<n;i++){
                scanf("%d",&y);
                anss=0;
                update(root,y);
                ans[i+1]=anss;
            }
            scanf("%d",&m);int h;
            for(int i=0;i<m;i++){
                    scanf("%d",&h);
                if(i!=0)printf(" ");
                printf("%d",ans[h]);
            }printf("
    ");
        }
    }
    





  • 相关阅读:
    xmpp学习笔记(二)
    xmpp学习笔记(一)
    【转】JavaScript 简史
    【转】JavaScript 中值得注意的 for 循环
    让你少走弯路的搭建树莓派的Net与NodeJS运行环境
    【转】最流行的编程语言JavaScript能做什么?
    【转】闭包会造成内存泄漏吗?
    【转】主流浏览器内核介绍
    【转】JVM 架构解读
    【转】你所不知道的HTML <head/> 头标签
  • 原文地址:https://www.cnblogs.com/da-mei/p/9053233.html
Copyright © 2011-2022 走看看