果然看了理解了一下大牛的代码然后自己敲结果果然有不少错误

回复说,线段树做为一种数据结构,最好以一种风格过一题裸的然后作为自己的模板。。

二分写的也很恶心哪

还有题目稍复杂一点的注定得推敲各种公式,不光DP注意边界那样令自己恶心,就是这些公式+1,-1结果都是差之千里,或者自己根本调试不出来。

思路很重要,模板也比较重要,线段树裸敲的话容易出错,比树状数组出错几率大的多。

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cstring>
#include <cmath>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <string>
#include <queue>
#include <set>
#include <map>
#include <vector>
#include <assert.h> using namespace std; #define lowbit(i) (i&-i)
#define sqr(x) ((x)*(x))
#define enter printf("\n")
#define is_sqr(x) (x&(x-1))
#define pi acos(-1.0)
#define clr(x) memset(x,0,sizeof(x))
#define fp1 freopen("in.txt","r",stdin)
#define fp2 freopen("out.txt","w",stdout)
#define pb push_back typedef long long LL; const double eps = 1e-7;
const double DINF = 1e100;
const int INF = 1000000006;
const LL LINF = 1000000000000000005ll;
const int MOD = (int) 1e9 + 7;
const int maxn=300005; template<class T> inline T Min(T a,T b){return a<b?a:b;}
template<class T> inline T Max(T a,T b){return a>b?a:b;}
template<class T> inline T Min(T a,T b,T c){return min(min(a, b),c);}
template<class T> inline T Max(T a,T b,T c){return max(max(a, b),c);} struct Node
{
int l,r,sum,add;
}node[maxn];//这地方也有技巧 线段树要存多大的数据 比如树高为H
void build(int root,int l,int r)
{
node[root].l=l;
node[root].r=r;
node[root].add=-1;
node[root].sum=0;
if(node[root].l==node[root].r) return ;
else
{
int mid=(l+r)/2;
build(root*2,l,mid);
build(root*2+1,mid+1,r);
}
}
int query(int root,int l,int r)
{
if(node[root].l==l&&node[root].r==r) return node[root].sum;//又有错误
if(node[root].add>=0)//更新子结点?
{
int ls=root*2,rs=root*2+1;
//node[root].sum=(node[root].r-node[root].l+1)*node[root].add;
//这一步不需要,前面已修改
node[ls].sum=(node[ls].r-node[ls].l+1)*node[root].add;//两处错误
node[rs].sum=(node[rs].r-node[rs].l+1)*node[root].add;// ls rs用不用更新?
node[ls].add=node[rs].add=node[root].add;
node[root].add=-1;
}
int ans=0;
int mid=(node[root].l+node[root].r)/2;
if(r<=mid)
{
return query(root*2,l,r);
}
else if(l>mid)
{
return query(root*2+1,l,r);
}
else
{
ans=query(root*2,l,mid);
ans+=query(root*2+1,mid+1,r);
}
return ans;
}
void update(int root,int l,int r,int add)
{
if(node[root].l==l&&node[root].r==r)//不够掌握的就是他这块的更新和上面的什么关系
{
node[root].add=add;
node[root].sum=(node[root].r-node[root].l+1)*node[root].add;
return;
}
int mid=(node[root].l+node[root].r)/2;
if(r<=mid)
{
update(root*2,l,r,add);
}
else if(l>mid)
{
update(root*2+1,l,r,add);
}
else
{
update(root*2,l,mid,add);
update(root*2+1,mid+1,r,add);
}
node[root].sum=node[root*2].sum+node[root*2+1].sum;
}
int bin_sea(int l,int r,int a)
{
int ll=l;
while(l<r)
{
int mid=(l+r)/2;
int temp_num=query(1,ll,mid);
if(mid-ll+1-temp_num>=a)//最开始没写出二分查找来 后来又因为mid-l+1错了 //第N处错误 //第N+1处错误
r=mid;
else l=mid+1;
}
return l;
}
int main()
{
int ncase;
scanf("%d",&ncase);
while(ncase--)
{
int n,m,i,j;
scanf("%d%d",&n,&m);
n--;
build(1,0,n);
for(i=1;i<=m;i++)
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
if(a==1)
{
int right_num=query(1,b,n);
// printf("~%d\n",right_num);
if(right_num==n-b+1)
printf("Can not put any one.\n");
else
{
int left_num=b==0?0:query(1,0,b-1);//这地方要是b==0的话 left_num直接等于0 (注意点1)//第N+3处错误
//printf("~%d\n",left_num);
int left_pos=bin_sea(0,n,b-left_num+1);//为吗是b-left_num+1?而不是+2
int right_pos=bin_sea(b,n,min(n-b+1-right_num,c));//(注意点2,min)
printf("%d %d\n",left_pos,right_pos);
update(1,left_pos,right_pos,1);
}
}
else if(a==2)
{
printf("%d\n",query(1,b,c));
update(1,b,c,0);
}
}
printf("\n");
}
return 0;
}

HDU4614【线段树。】的更多相关文章

  1. hdu4614 线段树+二分 插花

    Alice is so popular that she can receive many flowers everyday. She has N vases numbered from 0 to N ...

  2. HDU-4614 Vases and Flowers (线段树区间更新)

    题目大意:有n个花瓶,每个花瓶中只能放一朵花.两种操作,一种是从A开始放F朵花,如果有的花瓶中已经有花则跳过这个花瓶,往下一个花瓶放:第二种是将区间[A,B]之间花瓶中的花清空.如果是第一种操作,输出 ...

  3. HDU4614 Vases and Flowers 二分+线段树

    分析:感觉一看就是二分+线段树,没啥好想的,唯一注意,当开始摆花时,注意和最多能放的比大小 #include<iostream> #include<cmath> #includ ...

  4. HDU-4614 Vases and Flowers 线段树区间更新

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4614 线段树保存区间是否被覆盖以及区间的和即可,在询问的时候在线段树上二分查找就可以了...代码写得比 ...

  5. hdu4614(线段树+二分)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=4614 题意:给定一个区间[0,N-1],初始时每个位置上的数字都是0,可以对其进行以下两种操作: 1. ...

  6. hdu4614 Vases and Flowers 线段树+二分

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4614 题意: 给你N个花瓶,编号是0  到 N - 1 ,初始状态花瓶是空的,每个花瓶最多插一朵花. ...

  7. hdu4614 Vases and Flowers 线段树

    Alice is so popular that she can receive many flowers everyday. She has N vases numbered from 0 to N ...

  8. hdu4614 Vases and Flowers【线段树】【二分】

    Alice is so popular that she can receive many flowers everyday. She has N vases numbered from 0 to N ...

  9. HDU-4614 Vases and Flowers(线段树区间更新+二分查找)

    http://acm.hdu.edu.cn/showproblem.php?pid=4614 Time Limit: 4000/2000 MS (Java/Others)    Memory Limi ...

随机推荐

  1. android.intent.action.MAIN 与 android.intent.category.LAUNCHER 的验证理解

    第一种情况:有MAIN,无LAUNCHER,程序列表中无图标 原因:android.intent.category.LAUNCHER决定应用程序是否显示在程序列表里  第二种情况:无MAIN,有LAU ...

  2. PHP取二进制文件头快速判断文件类型

    <?php /*文件扩展名说明 *7173 gif *255216 jpg *13780 png *6677 bmp *239187 txt,aspx,asp,sql *208207 xls.d ...

  3. java----线程篇

    一个线程是进程内的一个单一的顺序控制流程图,多线程是指一个进程可以同时运行几个任务,每个任务由一个线程来完成.即多个线程可以同时运行,并且在一个进程内执行不同的任务. 1.创建线程两种方法方法一, 继 ...

  4. 在百万数据中找出重复的数据sql

    select * from (select count(name) as isone, name from tbl_org_departments group by name) t where t.i ...

  5. eclipse html插件的下载和安装

    需求:需要在eclipse里面编辑html和jsp,语法高亮和语法提示,自动补全等. 1.下载GEF(依赖包): http://www.eclipse.org/downloads/download.p ...

  6. javascript eval 执行过程

    当执行eval时,会执行如下过程 eval(x): 1.如果 x的类型不是string,那么会return x; 2.把x转换成 ecmascript 代码.如果转换失败,责抛出SyntaxError ...

  7. shell复习笔记----用户管理

    $ who    可以知道系统上有多少登陆 $who |wc -l 计算用户个数 注意:|是管道符号,可以在两个程序之间建立管道(pipeline):who 的输出,成了 wc 的输入, wc 所列出 ...

  8. 通过注册表查找oracle_home的位置

    运行regedit进入注册表 ctrl+f查找oracle_home

  9. CQRS学习——Storage实现(EF+Code First+DynamicReponsitory)[其四]

    [这里是的实现,指的是针对各个数据访问框架的一个基础实现] 目标 定义仓储/QueryEntry的基本功能 实现仓储的基本功能,以利于复用 实现一些常用的功能 提供一些便利的功能 目标框架 博主使用的 ...

  10. Nagios Apache报Internal Server Error错误的解决方法

    今天配置Nagios的时候遇到了一些麻烦,前面的步骤都一切顺利,nagios运行后,可以看到nagios的主页,但点击左边的菜单时总是提示Internal Server Error错误.错误如下: v ...