cf #310 E. Case of Chocolate (upper_bound())

题目:http://codeforces.com/contest/556/problem/E

题意:一块只有左上角的巧克力,每次从对角线位置向左或者向上吃,并且只能连续地吃。有q吃查询,问每次能吃的长度。

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

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.


分析:由于x+y=n+1,对于一次向上吃,它能吃的长度能由上方吃过的离它最近的那一次得到。向坐吃同理。分情况讨论就行了。

代码:

#include <bits/stdc++.h>
using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
const LL INF = 1E9+9;

struct node
{
	int s,t,dir; //1代表上,0代表左 
	node(int a=0,int b=0,int c=0):
		s(a),t(b),dir(c){}
	bool operator < (const node &t)const
	{
		return s<t.s;
	}
};

set <node> st;
set <int> visit;
int main()
{
	typedef set <node>::iterator its;
	int n,q,x,y;
	char op[2];
	scanf("%d%d",&n,&q);
	st.insert(node(0,1,1));
	st.insert(node(n+1,1,0)); 
	while(q--)
	{
		scanf("%d%d%s",&x,&y,op);
		if(visit.find(x)!=visit.end())
		{
			puts("0");
			continue ;
		}
		visit.insert(x);
		if(op[0]=='U')
		{
			its it=st.upper_bound(node(x));
			if(it->dir==1)
			{
				printf("%d\n",it->t-x+1);
				st.insert(node(x,it->t,1));
			}
			else
			{
				printf("%d\n",it->s-x);
				st.insert(node(x,it->s-1,1));
			}
		//	printf("info : %d %d %d\n",it->s,it->t,it->dir); 
		}
		else
		{
			its it=--st.upper_bound(node(x));
			if(it->dir==1)
			{
				printf("%d\n",x-it->s);
				st.insert(node(x,it->s+1,0)); 
			}
			else
			{
				printf("%d\n",x-it->t+1);
				st.insert(node(x,it->t,0));
			} 
		//	printf("info : %d %d %d\n",it->s,it->t,it->dir); 
		}
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值