题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4614

  线段树保存区间是否被覆盖以及区间的和即可,在询问的时候在线段树上二分查找就可以了。。。代码写得比较挫><

 //STATUS:C++_AC_359MS_1728KB
#include <functional>
#include <algorithm>
#include <iostream>
//#include <ext/rope>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <numeric>
#include <cstring>
#include <cassert>
#include <cstdio>
#include <string>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>
#pragma comment(linker,"/STACK:102400000,102400000")
using namespace std;
//using namespace __gnu_cxx;
//define
#define pii pair<int,int>
#define mem(a,b) memset(a,b,sizeof(a))
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define PI acos(-1.0)
//typedef
typedef __int64 LL;
typedef unsigned __int64 ULL;
//const
const int N=,M=;
const int INF=0x3f3f3f3f;
const int MOD=,STA=;
const LL LNF=1LL<<;
const double EPS=1e-;
const double OO=1e15;
const int dx[]={-,,,};
const int dy[]={,,,-};
const int day[]={,,,,,,,,,,,,};
//Daily Use ...
inline int sign(double x){return (x>EPS)-(x<-EPS);}
template<class T> T gcd(T a,T b){return b?gcd(b,a%b):a;}
template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}
template<class T> inline T lcm(T a,T b,T d){return a/d*b;}
template<class T> inline T Min(T a,T b){return a<b?a:b;}
template<class T> inline T Max(T a,T b){return a>b?a:b;}
template<class T> inline T Min(T a,T b,T c){return min(min(a, b),c);}
template<class T> inline T Max(T a,T b,T c){return max(max(a, b),c);}
template<class T> inline T Min(T a,T b,T c,T d){return min(min(a, b),min(c,d));}
template<class T> inline T Max(T a,T b,T c,T d){return max(max(a, b),max(c,d));}
//End int c[N<<],sum[N<<];
int T,n,m,a,b,tot,w; void pushup(int rt)
{
sum[rt]=sum[rt<<]+sum[rt<<|];
if(c[rt<<]==c[rt<<|] && c[rt<<]!=-)c[rt]=c[rt<<];
else c[rt]=-;
} void pushdown(int rt,int l,int r,int mid)
{
c[rt<<]=c[rt<<|]=c[rt];
sum[rt<<]=(mid-l+)*(!c[rt]);
sum[rt<<|]=(r-mid)*(!c[rt]);
} void build(int l,int r,int rt)
{
if(l==r){
sum[rt]=;
return;
}
int mid=(l+r)>>;
build(lson);
build(rson);
sum[rt]=r-l+;
} void update(int l,int r,int rt,int val)
{
if(a<=l && r<=b){
c[rt]=val;
sum[rt]=(r-l+)*(!val);
return ;
}
int mid=(l+r)>>;
if(c[rt]!=-)pushdown(rt,l,r,mid);
if(a<=mid)update(lson,val);
if(b>mid)update(rson,val);
pushup(rt);
} int query_sum(int l,int r,int rt)
{
if(a<=l && r<=b){
return sum[rt];
}
int mid=(l+r)>>,tot=;
if(c[rt]!=-)pushdown(rt,l,r,mid);
if(a<=mid)tot+=query_sum(lson);
if(b>mid)tot+=query_sum(rson);
return tot;
} void queryw(int l,int r,int rt)
{
if(l==r){
w=l;
tot-=sum[rt];
return;
}
int mid=(l+r)>>;
if(c[rt]!=-)pushdown(rt,l,r,mid);
if(sum[rt<<]>=tot)queryw(lson);
else if(mid<a || (sum[rt<<]<tot && sum[rt<<|])){
tot-=sum[rt<<];
queryw(rson);
}
else queryw(lson);
pushup(rt);
} int main()
{
// freopen("in.txt","r",stdin);
int i,j,op,x,y;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
n--;
mem(c,);
build(,n,);
while(m--){
scanf("%d%d%d",&op,&x,&y);
if(op==){
tot=y;
int t;
if(x>){
a=,b=x-;
t=query_sum(,n,);
tot+=t;
}
else t=;
a=x;
queryw(,n,);
if(tot==y)
printf("Can not put any one.\n");
else {
int en=w;
a=x;tot=t+;
queryw(,n,);
printf("%d %d\n",w,en);
a=w,b=en;
update(,n,,);
}
}
else {
a=x,b=y;
printf("%d\n",b-a+-query_sum(,n,));
update(,n,,);
}
}
putchar('\n');
}
return ;
}

HDU-4614 Vases and Flowers 线段树区间更新的更多相关文章

  1. HDU 4614 Vases and Flowers(线段树+二分)

    题目链接 比赛的时候一直想用树状数组,但是树状数组区间更新之后,功能有局限性.线段树中的lz标记很强大,这个题的题意也挺纠结的. k = 1时,从a开始,插b个花,输出第一个插的位置,最后一个的位置, ...

  2. hdu 4614 Vases and Flowers 线段树

    题目链接 一共n个盒子, 两种操作, 第一种是给出两个数x, y, 从第x个盒子开始放y朵花, 一个盒子只能放一朵, 如果某个盒子已经有了, 那么就跳过这个盒子放下面的盒子. 直到花放完了或者到了最后 ...

  3. HDU.1556 Color the ball (线段树 区间更新 单点查询)

    HDU.1556 Color the ball (线段树 区间更新 单点查询) 题意分析 注意一下pushdown 和 pushup 模板类的题还真不能自己套啊,手写一遍才行 代码总览 #includ ...

  4. HDU 1556 Color the ball(线段树区间更新)

    Color the ball 我真的该认真的复习一下以前没懂的知识了,今天看了一下线段树,以前只会用模板,现在看懂了之后,发现还有这么多巧妙的地方,好厉害啊 所以就应该尽量搞懂 弄明白每个知识点 [题 ...

  5. (简单) HDU 1698 Just a Hook , 线段树+区间更新。

    Description: In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of ...

  6. HDU 1698 Just a Hook(线段树区间更新查询)

    描述 In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of the heroes ...

  7. hdu - 1689 Just a Hook (线段树区间更新)

    http://acm.hdu.edu.cn/showproblem.php?pid=1698 n个数初始每个数的价值为1,接下来有m个更新,每次x,y,z 把x,y区间的数的价值更新为z(1<= ...

  8. 2014多校第四场1006 || HDU 4902 Nice boat (线段树 区间更新)

    题目链接 题意 : 给你n个初值,然后进行两种操作,第一种操作是将(L,R)这一区间上所有的数变成x,第二种操作是将(L,R)这一区间上所有大于x的数a[i]变成gcd(x,a[i]).输出最后n个数 ...

  9. HDU 1698 Just a Hook 线段树区间更新、

    来谈谈自己对延迟标记(lazy标记)的理解吧. lazy标记的主要作用是尽可能的降低时间复杂度. 这样说吧. 如果你不用lazy标记,那么你对于一个区间更新的话是要对其所有的子区间都更新一次,但如果用 ...

随机推荐

  1. firefly 环境配置所需工具

    原地址:http://www.9miao.com/question-15-59032.html http://down.9miao.com/attachment/forum/201405/19/120 ...

  2. 大象的崛起!Hadoop七年发展风雨录

    http://www.open-open.com/news/view/a22597 在互联网这个领域一直有这样的说法:“如果老二无法战胜老大,那么就把老大赖以生存的东西开源吧”.当年Yahoo!与Go ...

  3. C语言:将16进制字符串转化为int类型值

    将16进制字符串值转换为 int 整型值 此例中用 "1de" 作为测试字符串,实现代码如下: #include <stdio.h> #include <stdl ...

  4. WinDbg调试DMP格式文件

    前言:WinDbg是微软开发的免费源代码级的调试工具.WinDbg可以用于Kernel模式调试和用户模式调试,还可以调试Dump文件.本文的讨论是在安装了Debugging Tools for Win ...

  5. Zookeeper实战之单机集群模式

    前一篇文章介绍了Zookeeper的单机模式的安装及应用,但是Zookeeper是为了解决分布式应用场景的,所以通常都会运行在集群模式下.今天由于手头机器不足,所以今天打算在一台机器上部署三个Zook ...

  6. CSS+DIV 布局三种定位方式

    一.普通流 普通流中元素框的位置由元素在HTML中的位置决定.块级元素从上到下依次排列,框之间的垂直距离由框的垂直margin计算得到.行内元素在一行中水平布置. 二.定位 1.相对定位 被看作普通流 ...

  7. Consistent Hashing算法-搜索/负载均衡

    在做服务器负载均衡时候可供选择的负载均衡的算法有很多,包括:  轮循算法(Round Robin).哈希算法(HASH).最少连接算法(Least Connection).响应速度算法(Respons ...

  8. 【转】android资源目录---assets与res/raw区别

    blog.csdn.net/hshm20517/article/details/6461890 assets:用于存放需要打包到应用程序的静态文件,以便部署到设备中.与res/raw不同点在于,ASS ...

  9. Android开发之实用小知识点汇总-1

    1.去掉android屏幕中的actionbar: this.requestWindowFeature(Window.FEATURE_NO_TITLE);// 去掉标题栏 //这个是全屏幕显示的代码 ...

  10. C++类的复制构造函数和赋值运算符

    前言: C++面向对象的编程过程中,凡是在类中运用到动态内存分配的时候总是会写一个显示的复制构造函数和赋值重载运算符,本文将结合C++ Primer Plus一书的内容分析下原因: 一.在C++编程中 ...