HDU 4614 Vases and Flowers
来源:互联网 发布:crossover mac性能如何 编辑:程序博客网 时间:2024/05/20 09:07
选段树区间更新
题目描述:插花,每个花瓶只允许插入一枝花。插花操作是从第A个花瓶开始插花,如果第A个花瓶空着,就插入一枝花,否则判断第A+1个花瓶,知道插完全部的F只花,或者是没有花瓶可插,把剩余的花弃掉,操作终止。输出插花的第一个花瓶和最后一个花瓶,如果一枝花都无法插入,那输出Can not put any one.。清理操作:清理区间[A,B]内的话,输出清理的花的数目。
解题分析:线段区间内维护三个量,区间内插的花的数目,区间内第一个空花瓶,最后一个空花瓶。模拟即可。
代码如下:
#include <iostream>#include <cstdio>#include <algorithm>#include <cstring>using namespace std;const int INF = 0x3f3f3f3f;const int maxn = 50000 + 10;struct Node{ int l,r; int add,sum,first,last;}P[maxn << 2];int n,m;int tol;void build(int l,int r,int pos){ P[pos].l = P[pos].first = l; P[pos].r = P[pos].last = r; P[pos].add = -1; P[pos].sum = 0; if(l == r) return ; int mid = (l + r) / 2; build(l,mid,pos << 1); build(mid + 1,r,pos << 1 | 1);}void pushup(int pos){ P[pos].sum = P[pos << 1].sum + P[pos << 1 | 1].sum; if( P[pos].sum == (P[pos].r - P[pos].l + 1) ) { P[pos].first = INF; P[pos].last = -INF; } else { P[pos].first = min( P[pos << 1].first,P[pos << 1 | 1].first ); P[pos].last = max(P[pos << 1].last , P[pos << 1 | 1].last ); }}void pushdown(int pos){ if(P[pos].add != -1) { P[pos << 1].add = P[pos << 1 | 1].add = P[pos].add; if(P[pos].add == 1) { P[pos << 1].sum = P[pos << 1].r - P[pos << 1].l + 1; P[pos << 1].first = INF; P[pos << 1].last = - INF; P[pos << 1 | 1].sum = P[pos << 1 | 1].r - P[pos << 1 | 1].l + 1; P[pos << 1 | 1].first = INF; P[pos << 1 | 1].last = -INF; } else if(P[pos].add == 0) { P[pos << 1].sum = P[pos << 1 | 1].sum = 0; P[pos << 1].first = P[pos << 1].l; P[pos << 1].last = P[pos << 1].r; P[pos << 1 | 1].first = P[pos << 1 | 1].l; P[pos << 1 | 1].last = P[pos << 1 | 1].r; } P[pos].add = -1; }}void update1(int L,int R,int pos,int &first,int &last){ if(tol <= 0) return; if(P[pos].sum == (P[pos].r - P[pos].l + 1)) return; int rest = (P[pos].r - P[pos].l + 1 ) - P[pos].sum; if(L <= P[pos].l && P[pos].r <= R && rest <= tol) { first = min(first,P[pos].first); last = max(last,P[pos].last); tol -= rest; P[pos].sum = P[pos].r - P[pos].l + 1; P[pos].first = INF; P[pos].last = -INF; P[pos].add = 1; return ; } pushdown(pos); int mid = (P[pos].l + P[pos].r) / 2; if(L <= mid && tol > 0) update1(L,R,pos << 1,first,last); if(mid < R && tol > 0) update1(L,R,pos << 1 | 1,first,last); pushup(pos);}int update2(int L,int R,int pos){ if(L <= P[pos].l && P[pos].r <= R) { int t = P[pos].sum; P[pos].sum = 0; P[pos].first = P[pos].l; P[pos].last = P[pos].r; P[pos].add = 0; return t; } pushdown(pos); int mid = ( P[pos]. l + P[pos].r ) / 2; int ans = 0; if(L <= mid) ans += update2(L,R,pos << 1); if(mid < R) ans += update2(L,R,pos << 1 | 1); pushup(pos); return ans;}int main(){ int kase; scanf("%d",&kase); while(kase--) { scanf("%d %d",&n,&m); build(1,n,1); while(m--) { int k,x,y; scanf("%d %d %d",&k,&x,&y); if(k == 1) { int first = INF,last = -INF; tol = y; x++; update1(x,n,1,first,last); if(first == INF) printf("Can not put any one.\n"); else printf("%d %d\n",first - 1,last - 1); } else { x++; y++; cout << update2(x,y,1) << endl; } } puts(""); } return 0;}
阅读全文
0 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 线段树
- vue中computed和watch区别
- 27-未来畅想
- android:kotlin语言中使用单例模式
- java springMVC框架面试题
- Remap
- HDU 4614 Vases and Flowers
- iOS开发中常用的方法
- Oracle---数据库网络配置
- SSH--进入登录页面或者管理页面(jsp)
- Jzoj3934【NOIP2014day2官方数据】寻找道路
- 20171005写题心得
- Django中URL的相关配置以及后台函数常见设置(Django系列6)
- 编译Python-(Opencv+Contrib)-Ubuntu16.04
- Java SSM框架实现分页