您的位置:首页 > 其它

codeforces 555 C Case of Chocolate

2015-07-19 21:21 197 查看
一开始题目读错了,还以为可以从任意点为起点向上向左吃。

其实是只能从右边的边界为起点吃。

于是很明显,每一个横坐标最多只能出现一次,否则肯定是当前这个起点的巧克力已经被啃食了。

想到这里就更明显了,对于(xi,n+1-xi),若是向上吃,能够影响它的操作(xj,n+1-xj)肯定满足xj>xi,然后又明显一点,最小的xj肯定能影响到它。

我们来考虑操作(xj,n+1-xj),

若它是往左吃,很显然此时操作(xi,n+1-xi)能吃到被(xj,n+1-xj)吃掉的地方为止。

若它是往上吃呢?很显然无法影响到操作(xi,n+1-xi),为了简化问题,我们试试能否改变一些东西让它可以去影响。通过观察可以发现,它能吃到的终点的纵坐标很显然跟(xi,n+1-xi)操作能够吃到的终点的纵坐标一定是一样的。那么我们只需要将yj=n+1-xj,改为yj=(操作j能够吃到的终点的纵坐标)即可。于是,很高兴,我们构造出了统一的描述方法,让问题得到了很大的简化,又可以发现这种作法似乎有点像并查集。

接下来,讨论对于(xi,n+1-xi),若是向左吃的情况,这里根据上面的思路YY就一定可以知道结论。

于是全程维护一个map和两个映射数组就可以解决这个问题。
这两个数组的作用跟并查集的实现有着异曲同工之妙。

还是不懂的话,看看代码很快就能明白了。

#include<map>
#include<string>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<queue>
#include<vector>
#include<iostream>
#include<algorithm>
#include<bitset>
#include<climits>
#include<list>
#include<iomanip>
#include<stack>
#include<set>
using namespace std;
map<int,int>hash;
int x[200010],y[200010];
int main()
{
int n,q;
cin>>n>>q;
hash[0]=hash[n+1]=q;
while(q--)
{
char c;
cin>>x[q]>>y[q]>>c;
map<int,int>::iterator it=hash.lower_bound(x[q]);
if(it->first==x[q])
{
puts("0");
continue;
}
hash[x[q]]=q;
if(c=='U')
{
printf("%d\n",y[q]-y[it->second]);
y[q]=y[it->second];
}
else
{
it--;
it--;
printf("%d\n",x[q]-x[it->second]);
x[q]=x[it->second];
}
}
}


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.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: