codeforces 555C Case of Chocolate(map)

/*
题目大意:给一个巧克力,把副对角线(参照线代)下方的先都吃掉,然后每次给出一个询问,
从对角线出发,要么向上,要么向左,吃到已经吃过的地方或者边缘为止,输出每次能吃多少小格巧克力。
*/

/*
对于U的操作,能到达的y的上界和x值大于它最接近的操作的y的上界是一样的:
1.如果是L的操作,上界就是那个L操作的y值。
2.如果是U的操作,上界就是这次U操作所能到达的上界。

对于L的操作,则是找y值大于它最近的操作。
所以建立两个map U,L,一个key值是x,一个是y,因为map的lower_bound是按照key值查找的。

对于U操作,结束后U要插入pair(x,能到达的y值),在L中插入(y,x)
对于L操作 ,结束后L要插入pair(y,能到达的x值),在U中插入(x,y)
*/
# include<stdio.h>
# include<algorithm>
# include<string.h>
# include<map>
using namespace std;
map<int,int>l;
map<int,int>u;
int main()
{
    int n,m;
    int ans,x,y;
    while(~scanf("%d%d",&n,&m))
    {
        l.clear();
        u.clear();
        while(m--)
        {
            char op[5];
            map<int,int>::iterator p;
            scanf("%d%d%s",&x,&y,&op);
            if(op[0]=='U')
            {
                ans=0;
                p=u.lower_bound(x);
                if(u.count(x))
                {
                    printf("0\n");
                    continue;
                }
                if(p==u.end())
                    ans=y;
                else
                    ans=y-(p->second);
                printf("%d\n",ans);
                l[y]=x;
                u[x]=y-ans;
            }
            else
            {
                ans=0;
                p=l.lower_bound(y);
                if(l.count(y))
                {
                    printf("0\n");
                    continue;
                }
                if(p==l.end())
                    ans=x;
                else
                    ans=x-(p->second);
                printf("%d\n",ans);
                l[y]=x-ans;
                u[x]=y;
            }
        }
    }
    return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值