At first the problem was read wrong, thought can from any point for the starting point up to the left to eat.
In fact, only from the right side of the border for the starting point to eat.
So it is clear that each horizontal axis can only appear at most once, otherwise it is certainly the current starting point of the chocolate has been eaten.
The thought of this is even more obvious, for (XI,N+1-XI), if eating up, can affect its operation (XJ,N+1-XJ) certainly satisfies the xj>xi, then obviously, the smallest XJ certainly can affect it.
Let's consider the operation (XJ,N+1-XJ),
If it is left to eat, it is obvious that at this time the operation (XI,N+1-XI) can eat the place (XJ,N+1-XJ) eaten.
What if it's going to go up and eat? Obviously can't affect the operation (XI,N+1-XI), in order to simplify the problem, we try to change something so that it can be affected. Through observation, it can be found that it can eat the end of the ordinate is obviously with (XI,N+1-XI) operation can eat the end of the ordinate must be the same. Then we just need to change the YJ=N+1-XJ to yj= (the ordinate of the end that the operation J can eat). So, happily, we have constructed a unified description method, so that the problem has been greatly simplified, but also can be found that the practice seems to be a bit like and look at the set.
Next, the discussion for (XI,N+1-XI), if the left to eat the situation, here according to the above thinking yy can certainly know the conclusion.
So the whole process of maintaining a map and two map arrays can solve this problem.
The effect of these two arrays is similar to the implementation of the check set.
Still do not understand, see the code soon to understand.
#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--;p rintf ("%d\n", X[q]-x[it->second]); X[q]=x[it->second];}}}
Time limit per test3 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard output
Andrewid The Android is a galaxy-known detective. Now he does not investigate an any case and is eating chocolate out of boredom.
A Bar of chocolate can presented as an n? x? N table, where each cell represents one piece of chocolate. The columns of the table is numbered from1ToNFrom left to right and the rows is numbered from top to bottom. Let's call the Anti-diagonal-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 followingQActions with the remaining triangular part:first, he chooses a piece on the anti-diagonal and either direction ' up ' or ' l EFT ', and then he begins-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 all action, he wants to know how many pieces he ate as a result of the this action.
Input
The first line contains integersN(1?≤? n? ≤?109 ) andQ(1?≤? q. ≤?2 105 )-the size of the chocolate bar and the number of actions.
NextQLines contain the descriptions of the Actions:theI-th of them contains numbers xi and yi (1?≤? x i,? y i? ≤? N , xi? +? y i? =? 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 4 U6 1 L2 5 L1 6 U4 3 U
Output
43212
Input
9 U10 1 U1 U8 3 L10 1 L6 5 U
Output
9110602
Note
Pictures to the sample tests:
The pieces that were eaten in the same action is 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, Starti Ng 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.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Codeforces 555 C case of chocolate