JuQueen(线段树 lazy)
JuQueen
Time Limit: 5 Sec  Memory Limit:
512 MB
Description

Input

Output

Sample Input
10 10 5
state 0
groupchange 2 9 7
state 9
groupchange 0 2 10
change 0 -5
Sample Output
0
7
7
3
-3 线段树lazy的巧用#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#define LL long long using namespace std; const int Num = 5000000; typedef struct Tree
{
int Min;//所表示区间的最大值 int Max;//所表示区间的最小值 int lazy;//区间所要变化的值
} Tree; Tree Tr[Num*5]; char str[15]; int c,n,o; void Pushup(int st)
{
Tr[st].Max = max(Tr[st<<1].Max,Tr[st<<1|1].Max); Tr[st].Min = min(Tr[st<<1].Min,Tr[st<<1|1].Min); Tr[st].Min += Tr[st].lazy; Tr[st].Max += Tr[st].lazy;
} void Build(int L,int R,int st)
{
Tr[st].Max=0; Tr[st].Min=0; Tr[st].lazy=0; if(L==R)
{
return ;
}
int mid =(L+R)>>1; Build(L,mid,st<<1); Build(mid+1,R,st<<1|1); Pushup(st);
} void Update(int L,int R,int st,int l,int r,int s)
{
if(L>r||R<l)
{
return ;
} if(L>=l&&R<=r)//区间更新
{
Tr[st].lazy+=s; Tr[st].Max+=s; Tr[st].Min+=s; return ;
}
int mid = (L+R)>>1; Update(L,mid,st<<1,l,r,s); Update(mid+1,R,st<<1|1,l,r,s); Pushup(st);
} Tree Query(int L,int R,int st,int l,int r,int s)
{
Tree p,q; p.Max=0; p.Min=c; if(L>r||R<l)
{
return p;
} if(L>=l&&R<=r)
{
p.Max=Tr[st].Max+s; p.Min=Tr[st].Min+s; return p;
}
int mid = (L+R)>>1; s+=Tr[st].lazy;
//将lazy所表示的逐层向下更新
if(l<=mid)
{
q=Query(L,mid,st<<1,l,r,s); p.Max=max(q.Max,p.Max); p.Min=min(q.Min,p.Min);
}
if(r>mid)
{
q=Query(mid+1,R,st<<1|1,l,r,s); p.Max=max(q.Max,p.Max); p.Min=min(q.Min,p.Min);
}
return p;
} int main()
{
int l,r,s; Tree p; while(~scanf("%d %d %d",&n,&c,&o))
{
Build(1,n,1); for(int i=0; i<o; i++)
{
scanf("%s",str); if(!strcmp(str,"change"))
{
scanf("%d %d",&l,&s); r=++l; p=Query(1,n,1,l,r,0); if(s<0)
{
if(p.Min+s>=0)
{
printf("%d\n",s); Update(1,n,1,l,r,s);
}
else
{
printf("%d\n",-p.Min); Update(1,n,1,l,r,-p.Min);
}
}
else
{
if(p.Max+s<=c)
{
printf("%d\n",s); Update(1,n,1,l,r,s);
}
else
{
printf("%d\n",c-p.Max); Update(1,n,1,l,r,c-p.Max);
}
} }
else if(!strcmp(str,"groupchange"))
{
scanf("%d %d %d",&l,&r,&s); l++,r++; p=Query(1,n,1,l,r,0); if(s<0)
{
if(p.Min+s>=0)
{
printf("%d\n",s); Update(1,n,1,l,r,s);
}
else
{
printf("%d\n",-p.Min); Update(1,n,1,l,r,-p.Min);
}
}
else
{
if(p.Max+s<=c)
{
printf("%d\n",s); Update(1,n,1,l,r,s);
}
else
{
printf("%d\n",c-p.Max); Update(1,n,1,l,r,c-p.Max);
}
}
}
else if(!strcmp(str,"state"))
{
scanf("%d",&l); r=++l; p=Query(1,n,1,l,r,0); printf("%d\n",p.Max);
}
}
}
return 0;
}
JuQueen(线段树 lazy)的更多相关文章
- 分块+lazy  或者  线段树+lazy   Codeforces Round #254 (Div. 2) E
		
E. DZY Loves Colors time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
 - POJ 2777——线段树Lazy的重要性
		
POJ 2777 Count Color --线段树Lazy的重要性 原题 链接:http://poj.org/problem?id=2777 Count Color Time Limit: 1000 ...
 - poj3468 线段树+lazy标记
		
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 92921 ...
 - poj 2777(线段树+lazy思想)  小小粉刷匠
		
http://poj.org/problem?id=2777 题目大意 涂颜色,输入长度,颜色总数,涂颜色次数,初始颜色都为1,然后当输入为C的时候将x到y涂为颜色z,输入为Q的时候输出x到y的颜色总 ...
 - hdu 1698 Just a Hook 【线段树+lazy】
		
题目 写了一天的线段树,这道题主要说明一下sum是赋值的,不是累加的,并且在push_down的时候lazy也是赋值的.因可能对懒标记的理解还不是很透彻吧. #include <iostream ...
 - HDU3577Fast Arrangement(线段树+lazy)
		
Problem Description Chinese always have the railway tickets problem because of its' huge amount of p ...
 - HDU 3954 Level up(多颗线段树+lazy操作)
		
又是一开始觉得的水题,结果GG了好久的东西... 题意是给你n个英雄,每个英雄开始为1级经验为0,最多可以升到k级并且经验一直叠加,每一级都有一个经验值上限,达到就升级.接着给你两种操作:W li r ...
 - POJ3237 Tree(树剖+线段树+lazy标记)
		
You are given a tree with N nodes. The tree’s nodes are numbered 1 through N and its edges are numbe ...
 - poj  3237  树链剖分模板(用到线段树lazy操作)
		
/* 本体在spoj375的基础上加了一些操作,用到线段树的lazy操作模板类型 */ #include<stdio.h> #include<string.h> #includ ...
 
随机推荐
- oracle initialization or shutdown in progress问题解决步骤
			
今天像往常一样打开电脑,启动plsql工具连接数据库,但是尽然连接不了,报了“oracle initialization or shutdown in progress”的提示信息,从操作系统 ...
 - Latex 页面样式
			
LATEX支持三种预定义的页眉/页脚(header/footer)样式,称为页面样式(pagestyle).如下命令: \pagestyle{style} 中的style参数确定了使用哪一种页面样式. ...
 - geometric median
			
The geometric median of a discrete set of sample points in a Euclidean space is the point minimizing ...
 - 【iCore3 双核心板】例程十:RTC实时时钟实验——显示日期和时间
			
实验指导书及代码包下载: http://pan.baidu.com/s/1jHuZcnc iCore3 购买链接: https://item.taobao.com/item.htm?id=524229 ...
 - hover事件优化(延时操作)
			
JQ的hover事件拓展 编写原因:当鼠标滑过某个带有hover事件的元素,但是仅仅是路过,并不是希望查看此部分内容的时候,效果不理想 $.fn.extend({ delayed : function ...
 - the  major  advances  since  the  birth  of  the  computer
			
COMPUTER ORGANIZATION AND ARCHITECTURE DESIGNING FOR PERFORMANCE NINTH EDITION • The family concept: ...
 - C输入输出函数与缓冲区
			
#转 对C语言输入输出流和缓冲区的深入理解C语言缓冲区(缓存)详解缓冲区又称为缓存,它是内存空间的一部分.也就是说,在内存空间中预留了一定的存储空间,这些存储空间用来缓冲输入或输出的数据,这部分预留的 ...
 - ios数据库常用sql语句
			
SQlite常用语句 由于sql语句在程序代码中以字符串的形式存在,没有代码提示,不细心很容易出错,而且不容易被查出来.sql语句字符串是单引号. 写sql语句的时候一定要细心呀.如果写不好可以找公司 ...
 - entity.Student@150f3932, entity.Student@1a740c6b   没有实体中的数据
			
public class AppServerAction extends BaseAction { /** * */ /** * 初始化 “我的产品”列表 JSP页面 ...
 - Inside Flask - globals 全局变量(对象代理)
			
Inside Flask - globals 全局变量(对象代理) 框架是一个容器,在框架内编程,一般是要遵守框架的约定和使用模式.通常这样的模式是 IoC,即由框架调用用户的代码,而不是用户调用框架 ...