【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 ...
随机推荐
- Django REST Framework 数码宝贝 - 3步进化 - 混合类 -->
读了我这篇博客, 你会刷新对面对对象的认知, 之前的面对对象都是LJ~~~ 表结构 class Publisher(models.Model): name = models.CharField(max ...
- 小学生都能学会的python(闭包和迭代器)
小学生都能学会的python(闭包和迭代器) 1. 函数名第一类对象 函数名其实就是变量名 1). 可以像变量一样互相赋值. 2). 可以作为函数的参数,进行传递 3). 可以作为返回值返回 4). ...
- NOIP2018提高组省一冲奖班模测训练(二)
比赛链接 NOIP2018提高组省一冲奖班模测训练(二) 今天发挥正常,昨天不在状态…… 花了很久A了第一题 第二题打了30分暴力 第三题投机取巧输出test1答案(连暴力都不知道怎么打,太弱了) 2 ...
- 【BZOJ 1211】 [HNOI2004]树的计数
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] prufer数列的应用 http://www.cnblogs.com/AWCXV/p/7626625.html 这一题没有节点的度数 ...
- Linux学习总结(16)——CentOS 下 Nginx + Tomcat 配置负载均衡
最近在读埃隆·马斯克传记,他说「我认为现在有非常多的聪明人都在致力于互联网」. 仔细一想,好像真的是这样的. 我问了自己一个问题:如果你不敲代码了,你能做什么? 答案令人极其恐怖.吓得我赶紧又去写了一 ...
- POJ——T2421 Constructing Roads
http://poj.org/problem?id=2421 Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 24132 ...
- 110_leetcode_Best Time to Buy and sell Stock II
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- 多项福利回馈会员,且看Hao123怎样玩转“霸权主义”
谈到"霸权主义",我们非常easy将其与国际政治联系在一起.只是.霸权主义可不全然用来形容政治,在7月14日,Hao123上线了一个会员福利活动,命名为"Hao1 ...
- 浅析JAVA设计模式之工厂模式(二)
1 工厂方法模式简单介绍 工厂方法 (Factroy Method)模式:又称多态性工厂模式(Polymorphic Factory),在这样的模式中,核心工厂不再是一个详细的类.而是一个抽象工厂,提 ...
- [NIO]dawn之Task具体解释
在上篇文章中,我们设置好了开发环境,接下来.我们将在了解了Task以及Buffer之后,再開始了解网络编程.我们首先来看看Task task简单介绍 package zhmt.dawn; import ...