题目链接: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. DIY Ruby CPU 分析 Part II

    [编者按]作者 Emil Soman,Rubyist,除此之外竟然同时也是艺术家,吉他手,Garden City RubyConf 组织者.本文是 DIY Ruby CPU Profiling 的第二 ...

  2. 在WIN32 DLL中使用MFC库遇到的问题

    今天写了一个DLL,DLL中用到的一个类里用到的MSXML的COM组件,所以在DLL中要包含afx.h头文件,也就不可避免的要用到MFC的类库了,但在编译时出现了错误:mfcs42d.lib(dllm ...

  3. Android:requestWindowFeature应用程序窗体显示状态操作

    注意requestWindowFeature必须在 setContentView()之前调用. 1.DEFAULT_FEATURES:系统默认状态,一般不需要指定 2.FEATURE_CONTEXT_ ...

  4. windows 下 文件属性及目录列表操作

    转:http://blog.sina.com.cn/s/blog_686d0fb001012tsg.html 我们需要一个结构体和几个函数.这些函数和结构体在<io.h>的头文件中,结构体 ...

  5. Android 获取TextView 显示的字符串宽度

    工作上有业务需要判断textview是否换行,我的做法是判断textview要显示的字符串的宽度是否超过我设定的宽度,若超过则会执行换行. 项目中的其他地方也有这样的需求,故直接使用了那一块的代码.如 ...

  6. WPF博客地址分享

    http://www.cnblogs.com/chenxizhang/archive/2010/03/25/1694604.html http://www.cnblogs.com/lzhp/archi ...

  7. 在安装ISE的情况下,充分利用ISE的安装目录,查找资料

    2013-06-22 11:03:02 在找资料时,通过官网输入关键字的方法找资料,有事会给出很多版本的链接.或者找不到,下面给出一种简便的方法,可以快速找到想要的资料. 如果要找ISE各个工具如pl ...

  8. [ffmpeg 扩展第三方库编译系列] 关于需要用到cmake 创建 mingw32编译环境问题

    我在这里给出我编译的例子 cmake -G"MinGW Makefiles" -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=& ...

  9. jqGrid如何实现单选。

    设置如下:multiselect:true // multi-select checkboxes appear multiboxonly:true // checkboxes act like rad ...

  10. Java [leetcode 27]Remove Element

    题目描述: Given an array and a value, remove all instances of that value in place and return the new len ...