codeforces 555 C Case of Chocolate

一开始题目读错了,还以为可以从任意点为起点向上向左吃。

其实是只能从右边的边界为起点吃。

于是很明显,每一个横坐标最多只能出现一次,否则肯定是当前这个起点的巧克力已经被啃食了。

想到这里就更明显了,对于(xi,n+1-xi),若是向上吃,能够影响它的操作(xj,n+1-xj)肯定满足xj>xi,然后又明显一点,最小的xj肯定能影响到它。

我们来考虑操作(xj,n+1-xj),

若它是往左吃,很显然此时操作(xi,n+1-xi)能吃到被(xj,n+1-xj)吃掉的地方为止。

若它是往上吃呢?很显然无法影响到操作(xi,n+1-xi),为了简化问题,我们试试能否改变一些东西让它可以去影响。通过观察可以发现,它能吃到的终点的纵坐标很显然跟(xi,n+1-xi)操作能够吃到的终点的纵坐标一定是一样的。那么我们只需要将yj=n+1-xj,改为yj=(操作j能够吃到的终点的纵坐标)即可。于是,很高兴,我们构造出了统一的描述方法,让问题得到了很大的简化,又可以发现这种作法似乎有点像并查集。

接下来,讨论对于(xi,n+1-xi),若是向左吃的情况,这里根据上面的思路YY就一定可以知道结论。

于是全程维护一个map和两个映射数组就可以解决这个问题。

这两个数组的作用跟并查集的实现有着异曲同工之妙。

还是不懂的话,看看代码很快就能明白了。

#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--;
			printf("%d\n",x[q]-x[it->second]);
			x[q]=x[it->second];
		}
	}
}

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.

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-08 17:46:25

codeforces 555 C Case of Chocolate的相关文章

Codeforces Round #310 (Div. 1) C. Case of Chocolate (线段树)

题目地址:传送门 这题虽然是DIV1的C..但是挺简单的..只要用线段树分别维护一下横着和竖着的值就可以了,先离散化再维护.每次查找最大的最小值<=tmp的点,可以直接在线段树里搜,也可以二分去找. 代码如下: #include <iostream> #include <string.h> #include <math.h> #include <queue> #include <algorithm> #include <stdlib.

Codeforces Round #310 (Div. 1) C Case of Chocolate

思路:对于每个点而言.只与它相邻的两个点有关系.所以可以用stl或者线段树来找到它的相邻点. 代码:187ms(开挂之后貌似是最快的- -) #include <cstdio> #include <map> #include <algorithm> using namespace std; const int N = 200000 + 1; int x[N], y[N], t[N]; //适用于正负整数 template <class T> inline b

codeforces 556 D Case of Fugitive

这个题很显然,可以转换成这个问题: 有n-1个区间,m个数, 每个数最多只能用一次,第i个数只要能被第j个区间包含,那么这个数就可以放入这个区间内. 求出,当所有区间里都恰有一个数时的情况. 我们把所有区间按照下限升序排序,所有数升序排序之后分治即可. 分治过程,维护一个元素为区间的小堆,堆顶是上限最小的区间. 考虑第i个数,把所有能够包含它的区间都丢到堆中,然后从堆中丢一个区间出来,若这个区间可以包含这个数,那么记录下来,否则无解,结束程序. 为什么此时无解呢?因为很显然后面的数都不小于这个数

codeforces 555

最近爱做codeforces.....像比赛题而且有样例,,,嘻嘻 A:因为题意卡了我两回(开始以为必须是10才消掉...后来又以为只算1的个数),,其实没啥说的 #include <iostream> #include<bits/stdc++.h> using namespace std; int main() { int m; scanf("%d",&m); char a[200001]; scanf("%s",a); int s

Codeforces 450C:Jzzhu and Chocolate(贪心)

C. Jzzhu and Chocolate time limit per test: 1 seconds memory limit per test: 256 megabytes input: standard input output: standard output Jzzhu has a big rectangular chocolate bar that consists of \(n?×?m\) unit squares. He wants to cut this bar exact

Codeforces 450 C. Jzzhu and Chocolate

//area=(n*m)/ ((x+1)*(k-x+1)) //1: x==0; //2: x=n-1 //3: x=m-1 # include <stdio.h> long long max(long long x,long long y) { return x>y?x:y; } int main() { long long n,m,k,sum,t,ans; scanf("%lld%lld%lld",&n,&m,&k); sum=n+m-2;

【CF】310 Div.1 C. Case of Chocolate

线段树的简单题目,做一个离散化,O(lgn)可以找到id.RE了一晚上,额,后来找到了原因. 1 /* 555C */ 2 #include <iostream> 3 #include <string> 4 #include <map> 5 #include <queue> 6 #include <set> 7 #include <stack> 8 #include <vector> 9 #include <dequ

codeforces Round #310(Div.1) 题解

嘴巴选手真爽,一不用打代码二不用掉Rating三还可以打杂.... 感觉这套题不难,但是被出题人出瞎了... 555A. Case of Matryoshkas 题目大意:给定n个大小从1到n的套娃,初始套成k坨,每次你可以选择两个操作: 1.选择一个不在任何其他套娃里的套娃,将里面的套娃取出来(要求原先里面有套娃) 2.选择一个不再任何其他套娃里的套娃,将一个套娃塞进去(要求原先里面没有套娃) 求将所有套娃合并的最小操作次数 如果一个套娃x初始在最里面,或者满足大小为1...x?1的套娃都在它

Oracle的PL_SQL的异常处理

--什么是异常 --异常是在PL/SQL运行过程中有可能出现的错误. --执行异常的语句 exception when [异常] when --异常输出信息. --Oracle的预定义异常 CASE_NOT_FOUND ora-06592 --case语句中,when子句没有匹配的条件,而且没有else语句,会触发该异常. NO_DATA_FOUND ORA-01403 --select..into语句没有返回记录,触发该异常 TOO_MANY_ROWS ORA-01422 --select..