您的位置:首页 > 其它

hdu-4614 Vases and Flowers 成段更新

2014-07-19 17:12 211 查看
题目链接

#include "stdio.h"
#include "string.h"
#include "queue"
#include "vector"
#include "algorithm"
using namespace std;
const int maxn = 50005;
const int mod = 10007;
const int inf = 1<<30;
int n,m;
int op,x,y,fir,las,f;
struct node
{
int c;
}tree[maxn<<2];
int Pow( int a,int x )
{
int ans = 1;
for( int i = 1; i <= x; i ++ )
{
ans = ( ans*a )%mod;
}
return ans;
}
void PushUp( int rt )
{
if( tree[rt<<1].c == tree[rt<<1|1].c && tree[rt<<1].c != -1 )
tree[rt].c = tree[rt<<1].c;
}
void PushDown( int rt )
{
if( tree[rt].c != -1 )
{
tree[rt<<1].c = tree[rt<<1|1].c = tree[rt].c;
tree[rt].c = -1;
}
}
void BuildTree( int rt,int ld,int rd )
{
tree[rt].c = 0;
if( ld < rd )
{
int mid = ( ld+rd )>>1;
BuildTree( rt<<1,ld,mid );
BuildTree( rt<<1|1,mid+1,rd );
}
}
void updata( int rt,int ld,int rd )
{
if( f <= 0 )	return;
if( x <= ld && rd <= y && tree[rt].c != -1 && f >= ( rd - ld + 1 ) )
{
if( tree[rt].c == 0 ){
tree[rt].c = 1;
if( fir == -1 )	fir = ld;
las = rd;
f -= rd - ld + 1;
}
return;
}
PushDown( rt );
int mid = ( ld+rd )>>1;
if( x <= mid )   updata( rt<<1,ld,mid );
if( y > mid )    updata( rt<<1|1,mid+1,rd );
PushUp( rt );
}
int query( int rt,int ld,int rd )
{
if( x <= ld && rd <= y && tree[rt].c != -1 )
{
if( tree[rt].c == 1 ){
tree[rt].c = 0;
return rd - ld + 1;
}
else
return 0;
}
if( y < ld || x > rd )	return 0;
PushDown( rt );
int mid = ( ld+rd )>>1;
int l = query( rt<<1,ld,mid );
int r = query( rt<<1|1,mid+1,rd );
PushUp( rt );
return l + r;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("data.txt","r",stdin);
#endif
int cas;
scanf("%d",&cas);
while( cas -- )
{
scanf("%d%d",&n,&m);
BuildTree( 1,0,n-1 );
for( int i = 0; i < m; i ++ )
{
scanf("%d",&op);
if( op == 1 )
{
scanf("%d%d",&x,&f);
fir = las = -1;
y = n-1;
updata( 1,0,n-1 );
if( fir == -1 )
puts("Can not put any one.");
else
printf("%d %d\n",fir,las);
}
else{
scanf("%d%d",&x,&y);
printf("%d\n",query( 1,0,n-1 ));
}
}
puts("");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: