【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++ |
#include<cstdio>
#include<cstring>
#include<iostream>
#include<map>
#include<stack>
#include<queue>
#include<algorithm>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
/********************************
KinderRiven
*******************************/
#define lson (pos<<1)
#define rson (pos<<1|1)
const int maxn = 55555;
struct Node{
int l,r;
int v,col;
}node[maxn << 2];
int N,M;
void pushdown(int pos){
if(node[pos].col >= 0){
node[lson].col = node[pos].col; node[rson].col = node[pos].col;
node[lson].v = (node[lson].r - node[lson].l + 1) * node[pos].col;
node[rson].v = (node[rson].r - node[rson].l + 1) * node[pos].col;
node[pos].col = -1;
}
}
void pushup(int pos){
node[pos].v = node[lson].v + node[rson].v;
}
void BuildTree(int L,int R,int pos){
node[pos].l = L;
node[pos].r = R;
node[pos].col= -1;
if(L == R){
node[pos].v = 1;
return;
}
int m = (L + R) >> 1;
BuildTree(L,m,lson);
BuildTree(m + 1,R,rson);
pushup(pos);
return;
}
void UpDate(int L,int R,int pos,int value){
//printf("%d %d\n",node[pos].l,node[pos].r);
if(L <= node[pos].l && node[pos].r <= R){
node[pos].v = (node[pos].r - node[pos].l + 1) * value;
node[pos].col = value;
return;
}
pushdown(pos);
int m = (node[pos].l + node[pos].r) >> 1;
if(L <= m)
UpDate(L,R,lson,value);
if(R > m)
UpDate(L,R,rson,value);
pushup(pos);
}
int Query_sum(int L,int R,int pos){
if(L <= node[pos].l && node[pos].r <= R){
return node[pos].v;
}
pushdown(pos);
int m = (node[pos].l + node[pos].r) >> 1;
int ret = 0;
if(L <= m)
ret += Query_sum(L,R,lson);
if(R > m)
ret += Query_sum(L,R,rson);
pushup(pos);
return ret;
}
void Query(int value,int pos,int &p){
if(node[pos].l == node[pos].r){
p = node[pos].l;
return;
}
pushdown(pos);
int m = (node[pos].l + node[pos].r) >> 1;
if(node[lson].v >= value)
Query(value,lson,p);
else
Query(value - node[lson].v,rson,p);
pushup(pos);
return;
}
int main(){
int T;
scanf("%d",&T);
while(T--){
scanf("%d%d",&N,&M);
BuildTree(0,N - 1,1);
int op,a,b;
for(int i = 0;i < M;i ++){
scanf("%d%d%d",&op,&a,&b);
//printf("%d\n",Query_sum(0,N-1,1));
if(op == 1){ //须要改动
int sum = Query_sum(0,N - 1,1);
int k1 = Query_sum(a,N - 1,1);
int v = sum - k1;
//printf("%d\n",k1);
int p,q;
if(k1 == 0){
printf("Can not put any one.\n");
continue;
}
if(k1 < b) b = k1;
Query(v + 1,1,p);
Query(v + b,1,q);
UpDate(p,q,1,0);
printf("%d %d\n",p,q);
}
else if(op == 2){
int value = Query_sum(a,b,1);
UpDate(a,b,1,1);
printf("%d\n",b - a + 1 - value);
}
}
printf("\n");
}
return 0;
}
【HDU-4614】Vases and Flowers(线段树双查询)的更多相关文章
- HDU 4614 Vases and Flowers(线段树+二分)
题目链接 比赛的时候一直想用树状数组,但是树状数组区间更新之后,功能有局限性.线段树中的lz标记很强大,这个题的题意也挺纠结的. k = 1时,从a开始,插b个花,输出第一个插的位置,最后一个的位置, ...
- hdu 4614 Vases and Flowers 线段树
题目链接 一共n个盒子, 两种操作, 第一种是给出两个数x, y, 从第x个盒子开始放y朵花, 一个盒子只能放一朵, 如果某个盒子已经有了, 那么就跳过这个盒子放下面的盒子. 直到花放完了或者到了最后 ...
- HDU 4614 Vases and Flowers(线段树+二分)
Vases and Flowers Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others ...
- HDU 4614 Vases and Flowers (2013多校2 1004 线段树)
Vases and Flowers Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others ...
- HDU 4614 Vases and Flowers(二分+线段树区间查询修改)
描述Alice is so popular that she can receive many flowers everyday. She has N vases numbered from 0 to ...
- HDU 4614 Vases and Flowers(线段树+记录区间始末点或乱搞)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4614 题目大意:有n个空花瓶,有两种操作: 操作①:给出两个数字A,B,表示从第A个花瓶开始插花,插B ...
- hdu 4614 Vases and Flowers(线段树:成段更新)
线段树裸题.自己写复杂了,准确说是没想清楚就敲了. 先是建点为已插花之和,其实和未插花是一个道理,可是开始是小绕,后来滚雪球了,跪了. 重新建图,分解询问1为:找出真正插画的开始点和终止点,做成段更新 ...
- HDU 4614 Vases and Flowers (2013多校第二场线段树)
题意摘自:http://blog.csdn.net/kdqzzxxcc/article/details/9474169 ORZZ 题意:给你N个花瓶,编号是0 到 N - 1 ,初始状态花瓶是空的,每 ...
- HDU 4614 Vases and Flowers 【线段树】+【二分】
<题目链接> 题目大意: 有n个花瓶,每个花瓶中只能放一朵花.两种操作,一种是从A开始放F朵花,如果有的花瓶中已经有花则跳过这个花瓶,往下一个花瓶放:第二种是将区间[A,B]之间花瓶中的花 ...
- hdu 4614 Vases and Flowers
http://acm.hdu.edu.cn/showproblem.php?pid=4614 直接线段树维护 代码: #include<iostream> #include<cstd ...
随机推荐
- jQuery 文档操作
一.插入操作 1. 父元素.append(子元素) 追加某元素,在父元素中添加新的子元素, 子元素可以为: stirng / element (js对象) / jquery 元素 var oli = ...
- java的继承中构造方法
构造方法在创建对象的时候是被自动调用的,然后在继承中,是先调用父类的构造方法,然后在调用子类的构造方法, 当构造方法重写之后,在super中添加对应你想要调用构造方法的参数 例:父类 package ...
- java源码之Comparable和Comparator
1,Comparable 简介 Comparable 是排序接口. 若一个类实现了Comparable接口,就意味着“该类支持排序”. 即然实现Comparable接口的类支持排序,假设现在存在“实 ...
- Go语言Slice操作.
1.基本使用方法: a = append(a, b...) 比如:list = appened(list,[]int{1,2,3,4}...) 能够用来合并两个列表. 不用这样了 :list := m ...
- doT.js变量和数组混合读取方式
可以包裹任意大小的html 变量在其包裹的任意区域都有效 单个变量可以和数组分开展示 最好放置在最下方执行js 数据结构 var data = { "id": "1280 ...
- 简易Servlet计算器1.0
编写一个简易的Servlet计算器,暂时仅能实现 + - * / % 五种运算 jsp界面: <%@ page language="java" contentType=&qu ...
- struts2学习之基础笔记2
6.5 Struts2 的基本配置 1web.xml 作用:加载核心过滤器 格式: <filter> ``````` </filter> <filter-mapping& ...
- Android PullToRefreshListView设置各个item之间的间距
要设置第三方的上拉下载listView的item之间的间距,可以在xml布局文件中的listView节点中设置xml的属性即可: android:divider="#00000000&quo ...
- http_build_query 字符串拼接
http_build_query 字符串拼接 产生一个urlencode之后的请求字符串. 1.将数组转化成url问号(?)后的字符串 <?php $date=array( 'name'=> ...
- Servlet中文乱码原因 解决 Get 和 Post 和客户端
一.Get方式的中文乱码 1) 使用如下页面表单内容: <form action="http://127.0.0.1:8080/day07/params" method=&q ...