codeforces 555c 数据结构(map 的使用)

来源:互联网 发布:童装淘宝店名大全 编辑:程序博客网 时间:2024/06/11 00:22
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 ≤ nxi + 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.

Examples
input
6 53 4 U6 1 L2 5 L1 6 U4 3 U
output
43212
input
10 62 9 U10 1 U1 10 U8 3 L10 1 L6 5 U
output
9110602
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.


题意:

一块n*n方格型的巧克力,按如图所示的方法切割剩下左上角部分,然后从边界线的任意一个方格开始选择向上吃或者向左吃,一直吃到巧克力的边界为止,输入坐标和方向,输出能够吃到的巧克力的方格数。

分析:

如果从一个点向上吃,那么只有这个点右边,并且向左吃的情况才会影响能吃到的方格数;如果向左吃,那么只有这个点左边,并且向上吃的点才会影响吃到的方格数。在整理一下就得到结论,从一个点向上能吃到的最小纵坐标,等于他右边最近被吃的点能够吃到的最小纵坐标;同理向左吃能吃到的最小横坐标,等于左边最近被吃的点能吃到的最小横坐标。

代码:

#include"iostream"#include"cstdio"#include"map"#include"algorithm"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--;   //  迭代器向左移两位指向了左边最近被吃的点(不清楚的看一下map)printf("%d\n",x[q]-x[it->second]);x[q]=x[it->second]; //  把向左能吃到的最小横坐标存在右边最近被吃的点里}}}



0 0