HDU-4614 Vases and Flowers 线段树区间更新
题目链接: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 线段树区间更新的更多相关文章
- HDU 4614 Vases and Flowers(线段树+二分)
题目链接 比赛的时候一直想用树状数组,但是树状数组区间更新之后,功能有局限性.线段树中的lz标记很强大,这个题的题意也挺纠结的. k = 1时,从a开始,插b个花,输出第一个插的位置,最后一个的位置, ...
- hdu 4614 Vases and Flowers 线段树
题目链接 一共n个盒子, 两种操作, 第一种是给出两个数x, y, 从第x个盒子开始放y朵花, 一个盒子只能放一朵, 如果某个盒子已经有了, 那么就跳过这个盒子放下面的盒子. 直到花放完了或者到了最后 ...
- HDU.1556 Color the ball (线段树 区间更新 单点查询)
HDU.1556 Color the ball (线段树 区间更新 单点查询) 题意分析 注意一下pushdown 和 pushup 模板类的题还真不能自己套啊,手写一遍才行 代码总览 #includ ...
- HDU 1556 Color the ball(线段树区间更新)
Color the ball 我真的该认真的复习一下以前没懂的知识了,今天看了一下线段树,以前只会用模板,现在看懂了之后,发现还有这么多巧妙的地方,好厉害啊 所以就应该尽量搞懂 弄明白每个知识点 [题 ...
- (简单) HDU 1698 Just a Hook , 线段树+区间更新。
Description: In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of ...
- 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 ...
- hdu - 1689 Just a Hook (线段树区间更新)
http://acm.hdu.edu.cn/showproblem.php?pid=1698 n个数初始每个数的价值为1,接下来有m个更新,每次x,y,z 把x,y区间的数的价值更新为z(1<= ...
- 2014多校第四场1006 || HDU 4902 Nice boat (线段树 区间更新)
题目链接 题意 : 给你n个初值,然后进行两种操作,第一种操作是将(L,R)这一区间上所有的数变成x,第二种操作是将(L,R)这一区间上所有大于x的数a[i]变成gcd(x,a[i]).输出最后n个数 ...
- HDU 1698 Just a Hook 线段树区间更新、
来谈谈自己对延迟标记(lazy标记)的理解吧. lazy标记的主要作用是尽可能的降低时间复杂度. 这样说吧. 如果你不用lazy标记,那么你对于一个区间更新的话是要对其所有的子区间都更新一次,但如果用 ...
随机推荐
- linux下执行 ls,cat等一些命令报出 -bash: /bin/cat: Cannot allocate memory 有没解决的方法
环境变量配置出错了cd -- 进入用户目录vim .bash_profile删除以前PATH这一行,把下面的粘帖进去PATH=$PATH:$HOME/bin:/root:/root/snapshot/ ...
- fiddler插件开发step by step 1
Fiddler 是优秀的抓包工具,有着众多的优秀插件.Fiddler 软件是由C#语言开发的,运行在.net Framework 框架之上,所以我们也可以使用vs来开发自己的Fiddler插件,下面就 ...
- python:UnicodeEncodeError
problem: (<type 'exceptions.UnicodeEncodeError'>, UnicodeEncodeError('ascii', u'[taobao_cocobe ...
- 进阶:使用 EntityManager
JPA中要对数据库进行操作前,必须先取得EntityManager实例,这有点类似JDBC在对数据库操作之前,必须先取得Connection实例,EntityManager是JPA操作的基础,它不是设 ...
- Android进程守护
http://blog.csdn.net/t12x3456/article/details/8982198 http://blog.csdn.net/ljx19900116/article/detai ...
- socket编程中write、read和send、recv之间的区别
http://blog.csdn.net/petershina/article/details/7946615 一旦,我们建立好了tcp连接之后,我们就可以把得到的fd当作文件描述符来使用. 由此网络 ...
- 使用SAE部署Flask,使用非SAE flask版本和第三方依赖包的方法
目前SAE的Flask的版本为0.7,但是我从学习开始的flask版本就已经是0.10了,而且一些扩展都是使用的0.10以后的from flask.ext.特性进行引入的.所以需要修改SAE的环境. ...
- 各种html5 的 polyfill
https://github.com/Modernizr/Modernizr/wiki/HTML5-Cross-browser-Polyfills 配合 Modernizr
- bzoj1061: [Noi2008]志愿者招募
线性规划与费用流.http://www.cnblogs.com/iiyiyi/p/5616080.html.数组范围开错了!!!然后2.31-1=0x7fffffff!=0x7f7f7f7f. 开始以 ...
- Android handler Thread 修改UI Demo
/********************************************************************** * Android handler Thread 修改U ...