CodeForces 555C Case of Chocolate(STL:map的使用)

http://codeforces.com/problemset/problem/555/C

C. Case of Chocolate
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom.

A bar of chocolate can be presented as an n × n table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to n from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following q actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge.

After each action, he wants to know how many pieces he ate as a result of this action.

Input

The first line contains integers n (1 ≤ n ≤ 109) and q (1 ≤ q ≤ 2·105) — the size of the chocolate bar and the number of actions.

Next q lines contain the descriptions of the actions: the i-th of them contains numbers xi and yi (1 ≤ xi, yi ≤ n, xi + yi = n + 1) — the numbers of the column and row of the chosen cell and the character that represents the direction (L — left, U — up).

Output

Print q lines, the i-th of them should contain the number of eaten pieces as a result of the i-th action.

Sample test(s)
Input
6 5
3 4 U
6 1 L
2 5 L
1 6 U
4 3 U
Output
4
3
2
1
2
Input
10 6
2 9 U
10 1 U
1 10 U
8 3 L
10 1 L
6 5 U
Output
9
1
10
6
0
2
Note

Pictures to the sample tests:

The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten.

In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything.


对于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)


这个可以画一下图方便理解。


map知识:

map的元素是pair,用迭代器找到后使用first,second来调用元素的key和value

使用count来判断key存不存在

如果没有大于等于某元素的,lower_bound返回end()


#include<iostream>
#include<cstdio>
#include<map>
#include<cstring>
#include<set>
using namespace std;
int n,q;
map<int,int> u;
map<int,int> l;
int main()
{
    int x,y;
    char op[5];
    cin>>n>>q;
        while(q--)
        {
            scanf("%d%d%s",&x,&y,op);
            map<int,int>::iterator p;
            if(op[0]=='U')
            {
                int 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
            {
                int 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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值