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
output
input
output
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.
离散化+树状数组
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
*/
相关文章推荐
- Jquery学习笔记一
- Jquery学习笔记一
- HDOJ 1094 A+B for Input-Output Practice (VII)
- 怎么写一个“完美”的单例模式
- 微软为什么直接推送Win10 RS1预览版14251版本?
- 二维码 界面定制
- 仿netcat,手写tcp通道,创建监听拥有命令行权限!
- HDOJ 1093 A+B for Input-Output Practice (V)
- hyper-v 报错 0x80070569
- SSH-Struts(一)——基本原理
- 19. Remove Nth Node From End of List
- Win10预览版14251更新后会出现哪些新bug? 已知问题汇总
- HDOJ 1091 A+B for Input-Output Practice (IV)
- Deadlock
- Win10 RS1桌面预览版14251该怎么更新升级?
- 【源码笔记】Nop定时任务
- 图像处理(十九)基于移动最小二乘的图像变形-Siggraph 2006
- 图像处理(十四)图像分割(4)grab cut的图割实现-Siggraph 2004
- 图像处理(十三)保刚性图像变形算法-Siggraph 2004
- 图像处理(十二)图像融合(1)Seamless cloning泊松克隆-Siggraph 2004