2013 Multi-University Training Contest 1 Vases and Flowers HDU 4614

来源:互联网 发布:java可以从事什么职业 编辑:程序博客网 时间:2024/06/05 05:52

题意:给你N个花瓶,M次操作,每次输入K,如果K为1,那么再输入A,F,表示从A+1编号的花瓶开始,只要有空瓶,就插入一支花,如果F只花插完或者没有空瓶可以插了,就停止,输出最左边插入的和最右边插入的花瓶编号,当然,如果一支没插,或者F=0时,输出"Can not put any one.";如果K为2,再输入A,B,这时,将编号从A到B的所有花瓶中的花清空,输出清空的花的数量。

思路:明显的线段树。每个节点保存当前节点的左端点和右端点,以及flag标记,还有当前区间剩余的空瓶个数。当K为1时,先查询A+1~N-1之间的空瓶个数为tmp,如果tmp==0直接输出"Can not put any one.",否则在线段树中查找tmp空瓶的最左边和最右边的标号,分别为L,R,找到后更新区间[L,R]都为有花的情况;如果K为2,先查询当前A~B之间有多少花(等于区间花瓶总个数减去区间空瓶的个数),然后更新[A,B]为空瓶的情况。

#include <iostream>#include <cstdio>#include <cstring>using namespace std;#define MAX 50005#define lson rt<<1#define rson rt<<1|1struct node{    int l,r,flag,sum,rest;} tree[MAX<<2];void pushUp(int rt){    tree[rt].rest=tree[lson].rest+tree[rson].rest;    tree[rt].sum=tree[lson].sum+tree[rson].sum;}void pushDown(int rt){    int ls=lson,rs=rson;    if(tree[rt].flag==-1)    {        tree[ls].flag=tree[rs].flag=-1;        tree[ls].rest=tree[ls].r-tree[ls].l+1;        tree[rs].rest=tree[rs].r-tree[rs].l+1;        tree[rt].flag=0;    }    else if(tree[rt].flag==1)    {        tree[ls].flag=tree[rs].flag=1;        tree[ls].rest=tree[rs].rest=0;        tree[rt].flag=0;    }}void build(int rt,int l,int r){    tree[rt].l=l,tree[rt].r=r;    tree[rt].flag=0;    tree[rt].rest=r-l+1;    if(l==r) return ;    int mid=(l+r)>>1;    build(lson,l,mid);    build(rson,mid+1,r);}int query(int rt,int l,int r){    if(r<l) return 0;    if(tree[rt].l==l&&tree[rt].r==r)        return tree[rt].rest;    pushDown(rt);    int mid=(tree[rt].l+tree[rt].r)>>1;    if(r<=mid) return query(lson,l,r);    else if(l>mid) return query(rson,l,r);    else        return query(lson,l,mid)+query(rson,mid+1,r);}int queryPos(int rt,int tag){    if(tree[rt].l==tree[rt].r)        return tree[rt].l;    pushDown(rt);    int res=-1;    if(tag<=tree[lson].rest) res=queryPos(lson,tag);    else res=queryPos(rson,tag-tree[lson].rest);    return res;}void update(int rt,int l,int r,int val){    if(tree[rt].flag==val) return ;    if(tree[rt].l==l&&tree[rt].r==r)    {        if(val==1)        {            tree[rt].flag=1;            tree[rt].rest=0;        }        else        {            tree[rt].flag=-1;            tree[rt].rest=r-l+1;        }        return ;    }    pushDown(rt);    int mid=(tree[rt].l+tree[rt].r)>>1;    if(r<=mid) update(lson,l,r,val);    else if(l>mid) update(rson,l,r,val);    else    {        update(lson,l,mid,val);        update(rson,mid+1,r,val);    }    pushUp(rt);}int main(){    int n,m,k,l,r;    int T;    scanf("%d",&T);    while(T--)    {        scanf("%d%d",&n,&m);        build(1,1,n);        while(m--)        {            scanf("%d%d%d",&k,&l,&r);            if(k==1)            {                if(r==0)                {                    puts("Can not put any one.");                    continue;                }                int tmp=query(1,1,l);                if(tree[1].rest-tmp==0)                {                    puts("Can not put any one.");                    continue;                }                if(tree[1].rest-tmp<r) r=tree[1].rest-tmp;                int R=queryPos(1,tmp+r);                int L=queryPos(1,tmp+1);                update(1,L,R,1);                printf("%d %d\n",L-1,R-1);            }            else            {                printf("%d\n",r-l+1-query(1,l+1,r+1));                update(1,l+1,r+1,-1);            }        }        puts("");    }    return 0;}


原创粉丝点击