Vases and Flowers (二分 + 线段树)

题目链接:https://vjudge.net/contest/332656#problem/H

题意:

n个花瓶,m个操作,花瓶里面有的有花,有的是空的。

1 x y 表示从第x个位置开始查y多花,若一朵花也插不上输出"Can not put any one.",反之输出插花的左位置和右位置。

2 操作是清除区间[a,b]的花。并输出清除了多少花。

思路:
线段树维护区间,然后每次二分查找满足要求的第一个位置。

  1 #include <math.h>
  2 #include <stdio.h>
  3 #include <stdlib.h>
  4 #include <iostream>
  5 #include <algorithm>
  6 #include <string>
  7 #include <string.h>
  8 #include <vector>
  9 #include <map>
 10 #include <stack>
 11 #include <set>
 12 #include <random>
 13
 14 #define ll long long
 15 const int maxn = 1e5 + 10;
 16
 17 int n,m;
 18
 19 struct Node {
 20     int l,r;
 21     int val;
 22     int lazy;
 23 }tree[maxn<<2];
 24
 25 void build(int l,int r,int nod) {
 26     tree[nod].l = l;
 27     tree[nod].r = r;
 28     if (l == r ) {
 29         tree[nod].lazy = -1;
 30         return ;
 31     }
 32     int mid = (l + r) >> 1;
 33     build(l,mid,nod<<1);
 34     build(mid+1,r,(nod<<1)+1);
 35 }
 36
 37 void pushdown(int nod) {
 38     tree[nod<<1].val = tree[nod].lazy*(tree[nod<<1].r-tree[nod<<1].l+1);
 39     tree[(nod<<1)+1].val = tree[nod].lazy*(tree[(nod<<1)+1].r-tree[(nod<<1)+1].l+1);
 40     tree[nod<<1].lazy = tree[(nod<<1)+1].lazy = tree[nod].lazy;
 41     tree[nod].lazy = -1;
 42 }
 43
 44 void modify(int x,int y,int z,int nod=1) {
 45     int l = tree[nod].l,r = tree[nod].r;
 46     if (x <= l && y >= r) {
 47         tree[nod].lazy = z;
 48         tree[nod].val = (r-l+1)*z;
 49         return ;
 50     }
 51     if (tree[nod].lazy != -1 ) {
 52         pushdown(nod);
 53     }
 54     int mid = (l + r) >> 1;
 55     if (x <= mid) {
 56         modify(x,y,z,nod<<1);
 57     }
 58     if (y > mid) {
 59         modify(x,y,z,(nod<<1)+1);
 60     }
 61     tree[nod].val = tree[nod<<1].val + tree[(nod<<1)+1].val;
 62 }
 63
 64 int query(int x,int y,int nod=1) {
 65     int l = tree[nod].l,r = tree[nod].r;
 66     if (x <= l && y >= r) {
 67         return tree[nod].val;
 68     }
 69     if (tree[nod].lazy != -1 ) {
 70         pushdown(nod);
 71     }
 72     int mid = (l + r ) >> 1;
 73     int sum = 0;
 74     if (x <= mid) {
 75         sum += query(x,y,nod<<1);
 76     }
 77     if (y > mid) {
 78         sum += query(x,y,(nod<<1)+1);
 79     }
 80     tree[nod].val = tree[nod<<1].val + tree[(nod<<1)+1].val;
 81     return sum;
 82 }
 83
 84 int find(int s,int num ) {  // 查找满足从s开始插入num个的右边第一个位置
 85     int temp = query(s,n,1);
 86     if (temp == n-s+1) {
 87         return -1;
 88     }
 89     if (n-s+1-temp < num) {
 90         num = n-s+1-temp;
 91     }
 92     int l = s,r = n;
 93     int mid,d;
 94     int f = -1;
 95     while (l <= r) {
 96         mid = (l + r) >> 1;
 97         d = mid-s+1-query(s,mid);
 98         if (d > num) {
 99             r = mid - 1;
100         }
101         else if (d < num) {
102             l = mid + 1;
103         }
104         else {
105             f = mid;
106             r = mid - 1;
107         }
108     }
109     return f;
110 }
111
112 int main() {
113     int T;
114     scanf("%d",&T);
115     while (T--) {
116         memset(tree,0, sizeof(tree));
117         scanf("%d%d",&n,&m);
118         n--;
119         build(0,n,1);
120         while (m--) {
121             int d,x,y;
122             scanf("%d%d%d",&d,&x,&y);
123             if (d == 1) {
124                 int temp = find(x,1);
125                 if (temp == -1) {
126                     printf("Can not put any one.\n");
127                 }
128                 else {
129                     int end = find(x,y);
130                     printf("%d %d\n",temp,end);
131                     modify(temp,end,1);
132                 }
133             }
134             else {
135                 printf("%d\n",query(x,y));
136                 modify(x,y,0);
137             }
138         }
139         printf("\n");
140     }
141     return 0;
142 }

原文地址:https://www.cnblogs.com/-Ackerman/p/11729075.html

时间: 2024-10-05 00:03:02

Vases and Flowers (二分 + 线段树)的相关文章

HDU 4614 Vases and Flowers(线段树区间更新+二分)

Problem Description Alice is so popular that she can receive many flowers everyday. She has N vases numbered from 0 to N-1. When she receive some flowers, she will try to put them in the vases, one flower in one vase. She randomly choose the vase A a

【HDU 4614】Vases and Flowers(线段树区间更新懒惰标记)

题目0到n-1的花瓶,操作1在下标a开始插b朵花,输出始末下标.操作2清空[a,b]的花瓶,求清除的花的数量.线段树懒惰标记来更新区间.操作1,先查询0到a-1有num个空瓶子,然后用线段树的性质,或者二分找出第num+1个空瓶子的下标,和第num+b个空瓶子的下标.再区间更新为满.操作2,也相当于区间更新为空. #include<cstdio> #include<cstring> #include<algorithm> #define N 50001 using na

HDU4614Vases and Flowers 二分+线段树;

参考:https://blog.csdn.net/ophunter_lcm/article/details/9879495 题意: 有n个花瓶,有两种操作,1.从a开始放b朵花,有花的花瓶跳过,2.把a到b间的花全部拿下来. 思路: 线段树+二分 利用二分确定区间,这样就可以是线段树实现更简单的问题: 1)对区间进行全部设置为1的操作 2)对区间进行全部清零的操作 #include <iostream> #include <cstdio> #include <algorith

Vases and Flowers-HDU4614 二分+线段树

题意: 给你N个花瓶,编号是0  到 N - 1 ,一开始每个花瓶都是空的,你有两个操作: 第一个操作: 从第x个花瓶起开始插花,总共插y束,如果遇到花瓶中有花就跳过这个花瓶,直到花插完或者 插到第N-1个花瓶为止,输出插第一朵花的位置和最后一朵花的位置 第二个操作 将第x个花瓶到第y个花瓶之间的花扔掉,输出扔掉的花的数目 链接:http://acm.hdu.edu.cn/showproblem.php?pid=4614 思路 我们通过线段树来记录每个区间中花的数目,区间长度减去该区间中花的数目

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

题目大意:有n个花瓶,每个花瓶中只能放一朵花.两种操作,一种是从A开始放F朵花,如果有的花瓶中已经有花则跳过这个花瓶,往下一个花瓶放:第二种是将区间[A,B]之间花瓶中的花清空.如果是第一种操作,输出这次放的花的左右端点:如果是第二种操作,输出这次总共清理出了多少支花. 题目分析:建立线段树,节点维护在相应的区间中,没有放入花的花瓶数目.有三种操作:一.查询某个区间中第k个没有插入花的花瓶位置:二.更新区间,使区间全部插入花:三.更新区间,使区间中的花瓶全部清空: 代码如下: # include

【HDU-4614】Vases and Flowers(线段树双查询)

11946317 2014-10-23 09:08:28 Accepted 4614 437MS 2348K rid=11946317" target="_blank" style="color:rgb(26,92,200); text-decoration:none">3340 B G++ KinderRiven #include<cstdio> #include<cstring> #include<iostream&

【BZOJ-3110】K大数查询 整体二分 + 线段树

3110: [Zjoi2013]K大数查询 Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 6265  Solved: 2060[Submit][Status][Discuss] Description 有N个位置,M个操作.操作有两种,每次操作如果是1 a b c的形式表示在第a个位置到第b个位置,每个位置加入一个数c如果是2 a b c形式,表示询问从第a个位置到第b个位置,第C大的数是多少. Input 第一行N,M接下来M行,每行形如1 a

HDU5008 Boring String Problem(后缀数组 + 二分 + 线段树)

题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5008 Description In this problem, you are given a string s and q queries. For each query, you should answer that when all distinct substrings of string s were sorted lexicographically, which one is

(困难) CF 484E Sign on Fence,整体二分+线段树

Bizon the Champion has recently finished painting his wood fence. The fence consists of a sequence of n panels of 1 meter width and of arbitrary height. The i-th panel's height is hi meters. The adjacent planks follow without a gap between them. Afte