HDU-4614-Vases and Flowers
来源:互联网 发布:cnc数控编程入门教程 编辑:程序博客网 时间:2024/05/20 06:29
这个题属于线段树,题意也就是说有2个操作,一个是从某个位置开始向花瓶里插花,一个是清除现在花瓶中的花。
思路:
用线段树进行维护2种操作,我保存的是当前区间的花瓶还有多少个是空的,也就是说可以插花
调试了好久~唉~
代码:
#include<cstdio>#include<cstring>#include<iostream>using namespace std;const int maxn=50010;const int inf=1<<29;struct node{ int l; int r; int res;}t[maxn*10];int n,m,ansl,ansr;void Build(int l,int r,int index){ t[index].l=l; t[index].r=r; t[index].res=t[index].r-t[index].l+1; if(l==r)return; int mid=(l+r)>>1; Build(l,mid,index<<1); Build(mid+1,r,index<<1|1);}void PushDown(int index){ if(!t[index].res&&t[index].l<t[index].r) {t[index<<1].res=0;t[index<<1|1].res=0; } if(t[index].res==t[index].r-t[index].l+1&&t[index].l<t[index].r) {t[index<<1].res=t[index<<1].r-t[index<<1].l+1;t[index<<1|1].res=t[index<<1|1].r-t[index<<1|1].l+1; }}int Put(int l,int num,int index){ PushDown(index); if(!t[index].res||!num)return 0; if(t[index].l>=l&&t[index].res<=num&&t[index].res==t[index].r-t[index].l+1) {ansl=min(ansl,t[index].l);ansr=max(ansr,t[index].r);int res=t[index].res;t[index].res=0;return res; } int ans; int mid=(t[index].l+t[index].r)>>1; if(l>mid)ans=Put(l,num,index<<1|1); else {ans=Put(l,num,index<<1);ans+=Put(l,num-ans,index<<1|1); } t[index].res=t[index<<1].res+t[index<<1|1].res; return ans;}int Update(int l,int r,int index){ PushDown(index); if(t[index].l==l&&t[index].r==r) {int res=t[index].r-t[index].l+1-t[index].res;t[index].res=t[index].r-t[index].l+1;return res; } int mid=(t[index].l+t[index].r)>>1,ans=0; if(r<=mid)ans=Update(l,r,index<<1); else if(l>mid)ans=Update(l,r,index<<1|1); elseans=Update(l,mid,index<<1)+Update(mid+1,r,index<<1|1); t[index].res=t[index<<1].res+t[index<<1|1].res; return ans;}int main(){ int T; scanf("%d",&T); while(T--) {scanf("%d%d",&n,&m);Build(1,n,1);while(m--){ int op,a,b; scanf("%d%d%d",&op,&a,&b); a++; if(op==1) { ansl=inf,ansr=-inf;int x; x=Put(a,b,1);if(x==0){ printf("Can not put any one.\n"); continue;} printf("%d %d\n",ansl-1,ansr-1); } else {b++;printf("%d\n",Update(a,b,1)); }}printf("\n"); } return 0;}
- hdu 4614 Vases and Flowers
- hdu 4614 Vases and Flowers
- HDU 4614 Vases and Flowers
- hdu 4614 Vases and Flowers
- hdu 4614 Vases and Flowers
- HDU-4614-Vases and Flowers
- HDU 4614 Vases and Flowers
- HDU 4614 Vases and Flowers
- HDU 4614 Vases and Flowers
- HDU 4614 Vases and Flowers
- Vases and Flowers HDU
- Vases and Flowers HDU
- Vases and Flowers HDU
- HDU 4614 Vases and Flowers 解题报告
- hdu 4614 Vases and Flowers 线段树
- HDU 4614(Vases and Flowers)
- hdu 4614——Vases and Flowers
- hdu 4614 Vases and Flowers 线段树
- Code Review Tips
- 矩阵乘法(Matrix Multiply)
- AS3.0 HTML标签
- webservice 总结的学习贴
- 最小生成树问题
- HDU-4614-Vases and Flowers
- js,php实现用ua判断手机,ipad,pc浏览器访问网页不同尺寸页
- vim 使用进阶
- 恢复 git reset -hard 的误操作
- HDU 1054Strategic Game 贪心
- Java递归删除树结构数据
- VC多线程详解
- iOS如何判断当前网络的运营商
- nyoj-132-最长回文子串