C. Case of Chocolate
Time Limit: 20 Sec
Memory Limit: 256 MB
题目连接
http://codeforces.com/contest/555/problem/C
Description
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 Input
6 5
3 4 U
6 1 L
2 5 L
1 6 U
4 3 U
Sample Output
4
3
2
1
2
HINT
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.
题意
见hint,图很清楚,注意x+y==n+1
题解:
比B题简单呀……直接set搞一搞就好了
U的就找L的
L就找U的……
然后搞一搞就好了~
代码
//qscqesze
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef unsigned long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define maxn 2000001
#define mod 1000000007
#define eps 1e-9
int Num;
char CH[];
const int inf=0x3f3f3f3f;
inline ll read()
{
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
} //************************************************************************************** set<pair<int,int> > s;
map<int,int>m; int main()
{
int n=read(),q=read();
s.insert(make_pair(,));
s.insert(make_pair(n+,));
for(int i=;i<q;i++)
{
int x=read(),y=read();
char c;scanf("%c",&c);
if(m.count(x))
{
puts("");
continue;
}
if(c=='U')
{
pair<int,int> p=*s.lower_bound(pair<int,int>(x,-));
if(p.second==)
m[x]=p.first-x+m[p.first];
else
m[x]=p.first-x;
s.insert(pair<int,int>(x,));
}
else
{
set<pair<int,int> >::iterator it=--s.lower_bound(pair<int,int>(x,-));
if(it->second==)
m[x]=x-it->first+m[it->first];
else
m[x]=x-it->first;
s.insert(pair<int,int>(x,));
}
printf("%d\n",m[x]);
}
}