Codeforces Round #310 (Div. 1) C. Case of Chocolate set
10502 ワード
C. Case of Chocolate
Time Limit: 20 Sec
Memory Limit: 256 MB
タイトル接続
http://codeforces.com/contest/555/problem/C
Description
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 ≤ 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 53 4 U6 1 L2 5 L1 6 U4 3 U
Sample Output
43212
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問題より簡単だよ…直接セットしてやればいい
UのはLのを探します
LはUの...
そしてやってみればよかった~
コード#コード#
Time Limit: 20 Sec
Memory Limit: 256 MB
タイトル接続
http://codeforces.com/contest/555/problem/C
Description
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 ≤ 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 53 4 U6 1 L2 5 L1 6 U4 3 U
Sample Output
43212
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問題より簡単だよ…直接セットしてやればいい
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[20];
const int inf=0x3f3f3f3f;
inline ll read()
{
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';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(0,1));
s.insert(make_pair(n+1,0));
for(int i=0;i<q;i++)
{
int x=read(),y=read();
char c;scanf("%c",&c);
if(m.count(x))
{
puts("0");
continue;
}
if(c=='U')
{
pair<int,int> p=*s.lower_bound(pair<int,int>(x,-1));
if(p.second==1)
m[x]=p.first-x+m[p.first];
else
m[x]=p.first-x;
s.insert(pair<int,int>(x,1));
}
else
{
set<pair<int,int> >::iterator it=--s.lower_bound(pair<int,int>(x,-1));
if(it->second==0)
m[x]=x-it->first+m[it->first];
else
m[x]=x-it->first;
s.insert(pair<int,int>(x,0));
}
printf("%d
",m[x]);
}
}