您的位置:首页 > 其它

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

2016-01-28 08:28 369 查看
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 ≤ 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 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.

离散化+树状数组

#include <iostream>
#include<stdio.h>
#include<algorithm>
using namespace std;
struct node{
int x,y,z,index;
};
node c[300000];
int n,m,a[500000],b[500000][2],flg[500000],f[300000][3],r[300000],q[300000],p[300000];
bool cmp(node a,node b)
{
if (a.x<b.x) return true;
if (a.x==b.x&&a.index<b.index) return true;
return false;
}
int lowbit(int x)
{
return (x&(-x));
}
void add(int index,int val)
{
while(index<=m)
{
a[index]+=val;
index+=lowbit(index);
}
}
int sum(int index)
{
int sum=0;
while(index)
{
sum+=a[index];
index-=lowbit(index);
}
return sum;
}
int dog(int val)
{
int left=0;
int right=m+1;
int ans=r[m]+1,mid;
while(left<=right)
{
mid=(left+right)/2;
int tmp=sum(mid);
if (tmp==val)
{
ans=mid;
right=mid-1;
}
else
if (tmp>val) right=mid-1;
else
left=mid+1;
}
return ans;
}
int main()
{
int x,y,val,index1,index2;
char ch;
while(scanf("%d%d",&n,&m)!=EOF)
{
for(int i=1;i<=m;i++)
{
scanf("%d%d %c",&x,&y,&ch);
f[i][0]=x;
f[i][1]=y;
f[i][2]=(ch=='U'?0:1);
c[i].x=x;
c[i].y=y;
c[i].z=f[i][2];
c[i].index=i;
}
sort(c+1,c+1+m,cmp);
r[1]=1;
for(int i=2;i<=m;i++)
r[i]=(c[i].x==c[i-1].x?r[i-1]:r[i-1]+1);
for(int i=1;i<=m;i++)
{
q[c[i].index]=r[i];
p[r[i]]=c[i].x;
}
for(int i=0;i<=m+1;i++)
flg[i]=a[i]=0;
p[0]=0;
p[r[m]+1]=n+1;
b[0][0]=0;
b[0][1]=0;
b[r[m]+1][0]=r[m]+1;
b[r[m]+1][1]=r[m]+1;
for(int i=1;i<=m;i++)
{
x=q[i];
y=r[m]+1-x;
if (flg[x]==1)
{
printf("0\n");
continue;
}
flg[x]=1;
val=sum(x);
index1=dog(val);
index2=dog(val+1);
add(x,1);

if (f[i][2]==1)
{
printf("%d\n",p[x]-p[b[index1][1]]);
b[x][0]=x;
b[x][1]=b[index1][1];
}
else
{
printf("%d\n",-p[x]+p[b[index2][0]]);
b[x][0]=b[index2][0];
b[x][1]=x;
}
}
}
return 0;
}
/*

6 5 3 4 U 6 1 L 2 5 L 1 6 U 4 3 U

*/


  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: