hdu 4614 线段树
思路:当k为1的时候,用二分法查询包含有f个空瓶的上界r,然后更新会方便很多,直接更新区间(a,r)了。
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> #define lson(x) (x<<1) #define rson(x) ((x<<1)+1) #define mid ((tree[po].l+tree[po].r)>>1) #define inf 1<<30 #define Maxm 1000000 #define Maxn 60000 using namespace std; struct Tree{int l,r,left,up; }tree[Maxm]; int flag=0,ans,fir; void buildTree(int l,int r,int po) {tree[po].l=l,tree[po].r=r;tree[po].left=r-l+1;tree[po].up=0;if(l==r)return ;buildTree(l,mid,lson(po));buildTree(mid+1,r,rson(po)); } void down(int po) {if(!tree[po].left){tree[po].up=0;tree[lson(po)].left=0;tree[rson(po)].left=0;tree[rson(po)].up=0;tree[lson(po)].up=0;return ;}if(!tree[po].up) return ;tree[lson(po)].left=mid-tree[po].l+1;tree[rson(po)].left=tree[po].r-mid;tree[rson(po)].up=1;tree[lson(po)].up=1;tree[po].up=0; } void update(int l,int r,int po) {if(l<=tree[po].l&&r>=tree[po].r){ans+=tree[po].r-tree[po].l+1-tree[po].left;tree[po].left=tree[po].r-tree[po].l+1;tree[po].up=1;return ;}down(po);if(r<=mid)update(l,r,lson(po));elseif(l>mid)update(l,r,rson(po));else{update(l,r,lson(po));update(l,r,rson(po));}tree[po].left=tree[lson(po)].left+tree[rson(po)].left; } int findTree(int l,int r,int po) {if(l<=tree[po].l&&r>=tree[po].r){return tree[po].left;}down(po);int temp=0;if(r<=mid)temp=findTree(l,r,lson(po));elseif(l>mid)temp=findTree(l,r,rson(po));else{temp=findTree(l,r,lson(po));temp+=findTree(l,r,rson(po));}tree[po].left=tree[lson(po)].left+tree[rson(po)].left;return temp; } void Insert(int l,int r,int po) {if(l<=tree[po].l&&r>=tree[po].r){if(tree[po].left==tree[po].r-tree[po].l+1&&!flag)flag=1,fir=tree[po].l;if(flag){tree[po].left=0;return ;}}if(!tree[po].left)return ;down(po);if(r<=mid)Insert(l,r,lson(po));elseif(l>mid)Insert(l,r,rson(po));else{Insert(l,r,lson(po));Insert(l,r,rson(po));}tree[po].left=tree[lson(po)].left+tree[rson(po)].left; } int main() {int n,m,k,a,b,i,j,t;scanf("%d",&t);while(t--){scanf("%d%d",&n,&m);buildTree(0,n-1,1);for(i=1;i<=m;i++){scanf("%d%d%d",&k,&a,&b);if(k==1){flag=0;int l,r,Mid,temp;temp=findTree(a,n-1,1);if(temp<b)b=temp;l=a,r=n-1;while(r>l){Mid=(l+r)>>1;temp=findTree(a,Mid,1);if(temp>=b)r=Mid;elsel=Mid+1;}Insert(a,r,1);if(!flag)printf("Can not put any one.\n");elseprintf("%d %d\n",fir,r);}else{ans=0;update(a,b,1);printf("%d\n",ans);}}printf("\n");}return 0; }
转载于:https://www.cnblogs.com/wangfang20/p/3217019.html
hdu 4614 线段树相关推荐
- POJ 2777 ZOJ 1610 HDU 1698 --线段树--区间更新
直接将这3题 放一起了 今天在做线段树的东西 这3个都是区间更新的 查询方式互相不同 反正都可以放到一起吧 直接先上链接了 touch me touch me touch me 关于涉及到区间的修改 ...
- hdu 5367(线段树+区间合并)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5367 官方题解: 对于求"高山脉"长度,可以利用线段树.树节点中保存左高度连续长度 ...
- hdu 5266(线段树+LCA)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5266 解题思路: 考虑dfs序,通过在简单的证明可知L~R的LCA为L ~R 中dfs序较小的那个位置 ...
- hdu 5124(线段树区间更新+lazy思想)
http://acm.hdu.edu.cn/showproblem.php?pid=5124 题意:区间覆盖次数问题. 解题思路:线段树水之. #include<iostream> #in ...
- HDU - 4578Transformation——线段树+区间加法修改+区间乘法修改+区间置数+区间和查询+区间平方和查询+区间立方和查询
[题目描述] HDU - 4578Transformation Problem Description Yuanfang is puzzled with the question below: The ...
- poj 2777 AND hdu 5316 线段树
区间染色问题,用线段树可以解决.颜色总类不多,故考虑用二进制数的每一位表示一种颜色,然后父节点的颜色就是孩子节点颜色"或"起来,加上lazy标记,轻松AC. poj 2777: 1 ...
- HDU 5238 线段树+数论
原题:http://acm.hdu.edu.cn/showproblem.php?pid=5238. 题解:给你长度为n的操作序列,和m组操作求每组操作的模29393的值.这道题直接显然是没有前途的, ...
- poj 2528 离散化+线段树 hdu 1698 线段树 线段树题目类型一:染色计数 外加离散化
第一次听到离散化是今年省赛的时候,一道矩形并的题,很水,就两个矩形... 今天再去做线段树已经发现离散化忘得差不多了...水逼的悲哀啊... 先看简单点的hdu 1698 http://acm.hdu ...
- HDU 1166(线段树)
线段树 1 #include <cstdio> 2 #include <iostream> 3 using namespace std; 4 #define N 200010 ...
最新文章
- 如果让你拥有100万粉丝,你会做什么
- 动手写 framebuffer 画点、划线程序 (七)
- 【Python】垃圾分类,调用阿里云API
- C# 使用PictureBox控件--点击切换图片
- php post提交函数,PHP curl get post 提交函数
- 转换成CSV文件、Word、Excel、PDF等的方法--读取CSV文件的方法
- python用pandas读取txt_python Pandas 读取txt表格的实例
- UTC和GMT时间区别
- 【rqnoj 341】星门跳跃
- 关于lombok和mapstruct整合报无参构造函数错误
- Kaggle数据集之电信客户流失数据分析(三)之决策树分类
- Spring boot @Value 注入 boolean 设置默认值问题
- 5个优质开玩笑代码脚本
- mysql查看数据库的容量及表容量
- 铜陵C语言培训,铜陵学院c语言程序设计报告答案
- 《JSP程序设计》手机销售网后台设计
- HTML期末大作业~节日礼品购物商城网站html模板(HTML+CSS+JavaScript)
- mat 释放_c++ - OpenCV无法释放Mat *内存 - 堆栈内存溢出
- 数学建模学习(83):模拟退火算法(SA)求解多元函数
- NFC读写MifareClassic协议的NFC卡