Codeforces 555C Case of Chocolate

来源:互联网 发布:杀破狼js为什么下架了 编辑:程序博客网 时间:2024/05/18 13:27

C. 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.


题目大意:你有一块由对角线分开的巧克力,你每次选择对角线上一点,选择向左或向上吃,每次吃到边缘或者空缺就停止,现在给出每次的起点,问每次能吃到多少巧克力。

解法1:我们考虑这个题目的要求,每次都要查询最近一次与他垂直的线段的最大横/纵坐标,这个就可以使用数据结构。我们采用线段树维护1——n区间内最大的被吃过的坐标。行列分别建树。

代码

#include<cstdio>#include<cstring>#include<string>#include<algorithm>#include<iostream>#include<cmath>#include<cstdlib>#include<vector>using namespace std;const int MAXN=200010;struct segmenttree{int f[MAXN*4];int size;void init(int n){size=1;while (size<n){size*=2;}for (int i=0;i<size*2;i++){f[i]=0;}}void update(int a,int b,int k,int l,int r,int v){if (b<=l || r<=a){return ;}if (a<=l && r<=b){f[k]=max(f[k],v);return ;}int mid=(l+r)>>1;update(a,b,k*2+1,l,mid,v);update(a,b,k*2+2,mid,r,v);}void update(int a,int b,int k){update(a,b,0,0,size,k);}int get(int k){k+=size-1;int ret=f[k];while (k>0){k=(k-1)/2;ret=max(ret,f[k]);}return ret;}};segmenttree Left,up;int x[MAXN],y[MAXN];bool d[MAXN];vector <int> vx,vy;int main(){int n,q;scanf("%d%d",&n,&q);for (int i=0;i<q;i++){char c;scanf("%d %d %c",&x[i],&y[i],&c);if (c=='L'){d[i]=1;}vx.push_back(x[i]);vy.push_back(y[i]);}vx.push_back(0);vy.push_back(0);sort(vx.begin(),vx.end());sort(vy.begin(),vy.end());vx.erase(unique(vx.begin(),vx.end()),vx.end());up.init(vx.size()+2);Left.init(vy.size()+2);for (int i=0;i<q;i++){x[i]=lower_bound(vx.begin(),vx.end(),x[i])-vx.begin();y[i]=lower_bound(vy.begin(),vy.end(),y[i])-vy.begin();if (d[i]==0){int gt=up.get(x[i]);printf("%d\n",vy[y[i]]-vy[gt]);Left.update(gt,y[i]+1,x[i]);up.update(x[i],x[i]+1,y[i]);}else{int gt=Left.get(y[i]);printf("%d\n",vx[x[i]]-vx[gt]);
}
return 0;}


解法2:我们考虑每次吃过的地方,一定与上次平行方向吃的大小有关,若没有多余的操作,上次吃的地方被与他垂直吃的地方挡住了,也一定会挡住我们这次吃的。若挡不住,我们这次也一定能吃下去。我们只需要计算这一次与上一次同方向吃的长度的差,再判断中间吃的地方有没有挡住即可。

一点STL的奇技淫巧。

#include<cstdio>#include<cstring>#include<string>#include<algorithm>#include<iostream>#include<cmath>#include<cstdlib>#include<map>using namespace std;map <int,pair<char,int> > m;int query(){char c;int x,y;scanf("%d%d%c%c",&x,&y,&c,&c);map<int,pair<char,int> >::iterator b=m.lower_bound(x);if (b->first==x){return 0;}if (c=='L'){b--;}int ans=abs(b->first-x);if (b->second.first==c){ans+=b->second.second;}m[x]=make_pair(c,ans);return ans;} int main(){int n,q;m[0]=make_pair('U',0);scanf("%d%d",&n,&q); m[n+1]=make_pair('L',0);for (int i=1;i<=q;i++){printf("%d\n",query());}    return 0;}


原创粉丝点击