HDU 4614 Vases and Flowers (2013多校2 1004 线段树)
Vases and Flowers
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 38 Accepted Submission(s): 10
For each test case, the first line contains two integers N(1 < N < 50001) and M(1 < M < 50001). N is the number of vases, and M is the operations of Alice. Each of the next M lines contains three integers. The first integer of one line is K(1 or 2). If K is 1, then two integers A and F follow. It means Alice receive F flowers and try to put a flower in the vase A first. If K is 2, then two integers A and B follow. It means the owner would like to clean the vases numbered from A to B(A <= B).
Output one blank line after each test case.
10 5
1 3 5
2 4 5
1 1 8
2 3 6
1 8 8
10 6
1 2 5
2 3 4
1 0 8
2 2 5
1 4 4
1 2 3
2
1 9
4
Can not put any one.
2 6
2
0 9
4
4 5
2 3
[/pre]
很裸的线段树。
但是写起来很麻烦。
1~N 的区间,用1表示空的,0表示放了花的。
维护一个sum,就是和
first : 区间最左边的1
last: 区间最右边的1
然后一个更新操作,一个求区间和操作。
查询区间第一个1,和最后一个1.
二分确定区间。
#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <string.h>
#include <set>
#include <map>
#include <vector>
#include <queue>
#include <string>
#include <math.h>
using namespace std;
const int MAXN = ;
struct Node
{
int l,r;
int sum;
int lazy;
int first;
int last;
}segTree[MAXN*];
void push_up(int i)
{
if(segTree[i].l==segTree[i].r)return;
segTree[i].sum = segTree[i<<].sum+segTree[(i<<)|].sum;
if(segTree[i<<].first != -)segTree[i].first = segTree[i<<].first;
else segTree[i].first = segTree[(i<<)|].first;
if(segTree[(i<<)|].last != -)segTree[i].last = segTree[(i<<)|].last;
else segTree[i].last = segTree[(i<<)].last;
}
void push_down(int i)
{
if(segTree[i].r == segTree[i].l)return;
if(segTree[i].lazy==)
{
segTree[i<<].first = segTree[i<<].l;
segTree[i<<].last = segTree[i<<].r;
segTree[i<<].sum = segTree[i<<].r-segTree[i<<].l+;
segTree[i<<].lazy=;
segTree[(i<<)|].first = segTree[(i<<)|].l;
segTree[(i<<)|].last = segTree[(i<<)|].r;
segTree[(i<<)|].sum = segTree[(i<<)|].r-segTree[(i<<)|].l+;
segTree[(i<<)|].lazy=;
}
if(segTree[i].lazy == -)
{
segTree[i<<].first = -;
segTree[i<<].last = -;
segTree[i<<].sum = ;
segTree[i<<].lazy=-;
segTree[(i<<)|].first = -;
segTree[(i<<)|].last = -;
segTree[(i<<)|].sum = ;
segTree[(i<<)|].lazy=-;
}
segTree[i].lazy = ;
}
void build(int i,int l,int r)
{
segTree[i].l = l;
segTree[i].r = r;
segTree[i].sum = r-l+;
segTree[i].lazy = ;
segTree[i].first = l;
segTree[i].last = r;
if(l==r)return ;
int mid = (l+r)/;
build(i<<,l,mid);
build((i<<)|,mid+,r);
}
void update(int i,int l,int r,int type)
{
if(segTree[i].l == l && segTree[i].r==r)
{
if(type == )
{
if(segTree[i].sum == )return;
segTree[i].sum = ;
segTree[i].lazy = -;
segTree[i].first = -;
segTree[i].last = -;
return;
}
else if(type == )
{
if(segTree[i].sum == segTree[i].r-segTree[i].l+)return;
segTree[i].sum = segTree[i].r-segTree[i].l+;
segTree[i].lazy = ;
segTree[i].first = segTree[i].l;
segTree[i].last = segTree[i].r;
return;
} }
push_down(i);
int mid = (segTree[i].l + segTree[i].r)/;
if(r <= mid)update(i<<,l,r,type);
else if(l > mid)update((i<<)|,l,r,type);
else
{
update(i<<,l,mid,type);
update((i<<)|,mid+,r,type);
}
push_up(i);
}
int sum(int i,int l,int r)
{
if(segTree[i].l == l && segTree[i].r == r)
{
return segTree[i].sum;
}
push_down(i);
int mid = (segTree[i].l + segTree[i].r)/;
if(r <= mid)return sum(i<<,l,r);
else if(l > mid)return sum((i<<)|,l,r);
else return sum((i<<)|,mid+,r)+sum(i<<,l,mid);
}
int n,m;
int query1(int i,int l,int r)
{
if(segTree[i].l == l && segTree[i].r == r)
{
return segTree[i].first;
}
push_down(i);
int mid = (segTree[i].l + segTree[i].r)/;
int ans1,ans2;
if(r <= mid)return query1(i<<,l,r);
else if(l > mid)return query1((i<<)|,l,r);
else
{
ans1 = query1(i<<,l,mid);
if(ans1 != -)return ans1;
return query1((i<<)|,mid+,r);
}
}
int query2(int i,int l,int r)
{
if(segTree[i].l == l && segTree[i].r == r)
{
return segTree[i].last;
}
push_down(i);
int mid = (segTree[i].l + segTree[i].r)/;
int ans1,ans2;
if(r <= mid)return query2(i<<,l,r);
else if(l > mid)return query2((i<<)|,l,r);
else
{
ans1 = query2((i<<)|,mid+,r);
if(ans1 != -)return ans1;
return query2(i<<,l,mid);
}
}
int bisearch(int A,int F)
{
if(sum(,A,n)==)return -;
if(sum(,A,n)<F)return n;
int l= A,r = n;
int ans=A;
while(l<=r)
{
int mid = (l+r)/;
if(sum(,A,mid)>=F)
{
ans = mid;
r = mid-;
}
else l = mid+;
}
return ans;
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
build(,,n);
int op,u,v;
while(m--)
{
scanf("%d%d%d",&op,&u,&v);
if(op == )
{
u++;
int t = bisearch(u,v);
//printf("t:%d\n",t);
if(t==-)
{
printf("Can not put any one.\n");
continue;
}
printf("%d %d\n",query1(,u,t)-,query2(,u,t)-);
update(,u,t,);
}
else
{
u++;v++;
//printf("sum:%d\n",sum(1,u,v));
printf("%d\n",v-u+-sum(,u,v));
update(,u,v,);
}
}
printf("\n");
}
return ;
}
HDU 4614 Vases and Flowers (2013多校2 1004 线段树)的更多相关文章
- HDU 4614 Vases and Flowers (2013多校第二场线段树)
题意摘自:http://blog.csdn.net/kdqzzxxcc/article/details/9474169 ORZZ 题意:给你N个花瓶,编号是0 到 N - 1 ,初始状态花瓶是空的,每 ...
- HDU 4679 Terrorist’s destroy (2013多校8 1004题 树形DP)
Terrorist’s destroy Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Othe ...
- HDU 4614 Vases and Flowers(线段树+二分)
Vases and Flowers Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others ...
- hdu 4614 Vases and Flowers
http://acm.hdu.edu.cn/showproblem.php?pid=4614 直接线段树维护 代码: #include<iostream> #include<cstd ...
- HDU 4614 Vases and Flowers(二分+线段树区间查询修改)
描述Alice is so popular that she can receive many flowers everyday. She has N vases numbered from 0 to ...
- HDU 4614 Vases and Flowers(线段树+记录区间始末点或乱搞)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4614 题目大意:有n个空花瓶,有两种操作: 操作①:给出两个数字A,B,表示从第A个花瓶开始插花,插B ...
- hdu 4614 Vases and Flowers(线段树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4614 题意: 给你N个花瓶,编号是0 到 N - 1 ,初始状态花瓶是空的,每个花瓶最多插一朵花. ...
- HDU 4614 Vases and Flowers(线段树+二分)
题目链接 比赛的时候一直想用树状数组,但是树状数组区间更新之后,功能有局限性.线段树中的lz标记很强大,这个题的题意也挺纠结的. k = 1时,从a开始,插b个花,输出第一个插的位置,最后一个的位置, ...
- hdu 4614 Vases and Flowers(线段树:成段更新)
线段树裸题.自己写复杂了,准确说是没想清楚就敲了. 先是建点为已插花之和,其实和未插花是一个道理,可是开始是小绕,后来滚雪球了,跪了. 重新建图,分解询问1为:找出真正插画的开始点和终止点,做成段更新 ...
随机推荐
- quartz的触发器CronTriggerBean 配置
一个Quartz的CronTrigger表达式分为七项子表达式,其中每一项以空格隔开,从左到右分别是:秒,分,时,月的某天,月,星期的某天,年:其中年不是必须的,也就是说任何一个表达式最少需要六项! ...
- (五)用正则化(Regularization)来解决过拟合
1 过拟合 过拟合就是训练模型的过程中,模型过度拟合训练数据,而不能很好的泛化到测试数据集上.出现over-fitting的原因是多方面的: 1) 训练数据过少,数据量与数据噪声是成反比的,少量数据导 ...
- ORA-10456:cannot open standby database;media recovery session may be in process
http://neeraj-dba.blogspot.com/2011/10/ora-10456-cannot-open-standby-database.html Once while star ...
- grep -A -B选项详解和mysqlbinlog
grep的-A-B-选项详解(转)[@more@] grep能找出带有关键字的行,但是工作中有时需要找出该行前后的行,下面是解释 1. grep -A1 keyword filename 找出file ...
- 一:AndEngine的小例子
首先导入架包,下载:http://download.csdn.net/detail/duancanmeng/4060082 lib文件夹中 像我们写android程序entends Activity一 ...
- Arduino 使用舵机库时 其它引脚输出怪异 解决方案
使用Servo.h时,不管你在初始化时用的是9还是10脚,都不要把这两个脚作为舵机以外的用途! 例: servo.attach(9); digitalWrite(10,1);//错,不能把第10脚用作 ...
- ubuntu 切换工作区域
在Ubuntu 13.04中,默认是不激活多桌面工作空间的,所以在之前的版本可以在启动器看到的那个像“田”字的“工作区切换器”图标没有了,始终只有一个桌面了.要激活工作空间,在system setti ...
- linux log4j 使用
1.首先到Apache官网下载log4j.jar文件http://logging.apache.org/log4j/1.2/download.html 引用到eclipse项目里面 2.在src目录下 ...
- CURL 错误码 中文翻译
这几天用CURL做下载系统,经常会遇到一些问题,很多的错误还是和CURL的option有关.现在把这些错误码贴过来,方便查看一下. 错误代码列表 CURLE_UNSUPPORTED_PROTOCOL ...
- Oracle自定义数据类型 2 (调用对象方法)
调用对象方法 调用对象方法基于类型创建表后,就可以在查询中调用对象方法 A. 创建基于对象的表语法: create table <表名> of <对象类型>意义 ...