HDU4614【线段树。】【花瓶与插花】

2023-12-17 20:10
文章标签 线段 插花 hdu4614 花瓶

本文主要是介绍HDU4614【线段树。】【花瓶与插花】,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=4614

 

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

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

二分写的也很恶心哪

还有题目稍复杂一点的注定得推敲各种公式,不光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【线段树。】【花瓶与插花】的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/505688

相关文章

poj3468(线段树成段更新模板题)

题意:包括两个操作:1、将[a.b]上的数字加上v;2、查询区间[a,b]上的和 下面的介绍是下解题思路: 首先介绍  lazy-tag思想:用一个变量记录每一个线段树节点的变化值,当这部分线段的一致性被破坏我们就将这个变化值传递给子区间,大大增加了线段树的效率。 比如现在需要对[a,b]区间值进行加c操作,那么就从根节点[1,n]开始调用update函数进行操作,如果刚好执行到一个子节点,

hdu1394(线段树点更新的应用)

题意:求一个序列经过一定的操作得到的序列的最小逆序数 这题会用到逆序数的一个性质,在0到n-1这些数字组成的乱序排列,将第一个数字A移到最后一位,得到的逆序数为res-a+(n-a-1) 知道上面的知识点后,可以用暴力来解 代码如下: #include<iostream>#include<algorithm>#include<cstring>#include<stack>#in

hdu1689(线段树成段更新)

两种操作:1、set区间[a,b]上数字为v;2、查询[ 1 , n ]上的sum 代码如下: #include<iostream>#include<algorithm>#include<cstring>#include<stack>#include<queue>#include<set>#include<map>#include<stdio.h>#include<stdl

hdu 1754 I Hate It(线段树,单点更新,区间最值)

题意是求一个线段中的最大数。 线段树的模板题,试用了一下交大的模板。效率有点略低。 代码: #include <stdio.h>#include <string.h>#define TREE_SIZE (1 << (20))//const int TREE_SIZE = 200000 + 10;int max(int a, int b){return a > b ? a :

hdu 1166 敌兵布阵(树状数组 or 线段树)

题意是求一个线段的和,在线段上可以进行加减的修改。 树状数组的模板题。 代码: #include <stdio.h>#include <string.h>const int maxn = 50000 + 1;int c[maxn];int n;int lowbit(int x){return x & -x;}void add(int x, int num){while

poj 1127 线段相交的判定

题意: 有n根木棍,每根的端点坐标分别是 px, py, qx, qy。 判断每对木棍是否相连,当他们之间有公共点时,就认为他们相连。 并且通过相连的木棍相连的木棍也是相连的。 解析: 线段相交的判定。 首先,模板中的线段相交是不判端点的,所以要加一个端点在直线上的判定; 然后,端点在直线上的判定这个函数是不判定两个端点是同一个端点的情况的,所以要加是否端点相等的判断。 最后

HDU4737线段树

题目大意:给定一系列数,F(i,j)表示对从ai到aj连续求或运算,(i<=j)求F(i,j)<=m的总数。 const int Max_N = 100008 ;int sum[Max_N<<2] , x[Max_N] ;int n , m ;void push_up(int t){sum[t] = sum[t<<1] | sum[t<<1|1] ;}void upd

zoj 1721 判断2条线段(完全)相交

给出起点,终点,与一些障碍线段。 求起点到终点的最短路。 枚举2点的距离,然后最短路。 2点可达条件:没有线段与这2点所构成的线段(完全)相交。 const double eps = 1e-8 ;double add(double x , double y){if(fabs(x+y) < eps*(fabs(x) + fabs(y))) return 0 ;return x + y ;

圆与线段的交点

poj 3819  给出一条线段的两个端点,再给出n个圆,求出这条线段被所有圆覆盖的部分占了整条线段的百分比。 圆与线段的交点 : 向量AB 的参数方程  P = A + t * (B - A)      0<=t<=1 ; 将点带入圆的方程即可。  注意: 有交点 0 <= t <= 1 ; 此题求覆盖的部分。 则 若求得 t  满足 ; double ask(d

Codeforces 482B 线段树

求是否存在这样的n个数; m次操作,每次操作就是三个数 l ,r,val          a[l] & a[l+1] &......&a[r] = val 就是区间l---r上的与的值为val 。 也就是意味着区间[L , R] 每个数要执行 | val 操作  最后判断  a[l] & a[l+1] &......&a[r] 是否= val import ja