题目链接: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. Samza在YARN上的启动过程 =》 之二 submitApplication

    首先,来看怎么构造一个org.apache.hadoop.yarn.client.api.YarnClient class ClientHelper(conf: Configuration) exte ...

  2. Java 8 vs. Scala(一): Lambda表达式

    [编者按]虽然 Java 深得大量开发者喜爱,但是对比其他现代编程语言,其语法确实略显冗长.但是通过 Java8,直接利用 lambda 表达式就能编写出既可读又简洁的代码.作者 Hussachai ...

  3. http://www.cnblogs.com/doit8791/p/4093808.html

    http://www.cnblogs.com/doit8791/p/4093808.html

  4. codeforces #305 div1 done

    总算搞定了这一场比赛的题目,感觉收获蛮大 其中A,B,C都能通过自己的思考解决掉 D题思路好神,E题仔细想想也能想出来 以后坚持每两天或者一天做一场CF的div1的全套题目 除非有实在无法做出来的题目 ...

  5. linux xxd 命令

    http://www.cnblogs.com/openix/archive/2012/04/23/2466320.html xxd -i dht.jpg dht.h

  6. java内存模型 年轻代/年老代 持久区

    jvm中的年轻代 老年代 持久代 gc   虚拟机中的共划分为三个代:年轻代(Young Generation).老年代(Old Generation)和持久代(Permanent Generatio ...

  7. nginx的负载均衡和反响代理配置

    4.        负载均衡配置 nginx 的 upstream默认是以轮询的方式实现负载均衡,这种方式中,每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除. 另外 ...

  8. spring的组成

    ① Spring Core:核心容器,BeanFactory提供了组件生命周期的管理,组件的创建,装配,销毁等功能 SpringContext:ApplicationContext,扩展核心容器,提供 ...

  9. 201. Bitwise AND of Numbers Range

    题目: Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all num ...

  10. MySQL 普通索引、唯一索引和主索引

    1.普通索引 普通索引(由关键字KEY或INDEX定义的索引)的唯一任务是加快对数据的访问速度.因此,应该只为那些最经常出现在查询条件(WHEREcolumn=)或排序条件(ORDERBYcolumn ...