Codeforces Round #310 (Div. 1) C. Case of Chocolate stl应用

C. Case of Chocolate
time limit per test3 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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.
题意就是,给个正方形,去掉反对角线下面的方块,给定一个在反对角线上的点,可以向上吃和向左吃,直到遇到吃空的方块。
用stl set 做一个一个插入,复杂度为 m * log(m),先分析下
这里写图片描述
如图,本来有了1,再加入2,也就是向左吃,这时,只和这点下面的最近点有关系了,也就是和1有关系,类似 ,当然向上吃的时候,就和上面的最近的点有关,自已画下就知道了。先出现2,只需要找到1,用x[2] - x[1]就可以了,但是再出现3的时候,只能找到2这个点,本来,应该是x[3] - x[1]的值就可以得到答案了,但2并不是1,所以,这时候,只需要把x[2]更新为x[1]的值就可以了。这样就保存前面的信息,在后面查询的时候很方便得到。
向上吃的时候,也类似,自已画画。有个小技巧,初始化的时候,加入一个0 和 n+1的虚拟点,这样就不会找到为空的点了。

#define N 200500
#define M 100005
#define maxn 205
#define MOD 1000000000000000007
set<pii> myset;
set<pii>::iterator it;
int n,m,x[N],y[N],inx;
char str[50];
int main()
{
    while ( scanf ( "%d%d" , &n , &m ) != EOF)
    {
        myset.clear();
        x[0] = y[0] = x[m + 1] = y[m+1] = 0;
        myset.insert(make_pair(0,0));
        myset.insert(make_pair(n+1,m+1));
        for(int i=1;i<=m;i++){
            S2(x[i],y[i]);
            SS(str);
            if(str[0] == 'U'){
                it = myset.lower_bound(make_pair(x[i],-1));
                inx = it->second;
                if(it->first == x[i]){
                    printf("0\n");
                }
                else {
                    myset.insert(make_pair(x[i],i));
                    printf("%d\n",y[i] - y[inx]);y[i] = y[inx];
                }
            }
            else {
                it = myset.upper_bound(make_pair(x[i],m+1));
                it--;
                inx = it->second;
                if(it->first == x[i]){
                    printf("0\n");
                }
                else {
                    myset.insert(make_pair(x[i],i));
                    printf("%d\n",x[i] - x[inx]);x[i] = x[inx];
                }
            }
        }
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值