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 ...
随机推荐
- JS简单的图片左右滚动
<div id="scroll" style="overflow:hidden;width:757px;"> <table cellpaddi ...
- Hibernate查询 Query Language
1,Native SQL ->HQL->EJBQL->QBC(Query By Cretira)->QBE(Query By Example) 此排列是根据可实现功能大小排序.
- js 将一段html插入到body最前面
var wrap=document.createElement("div"); var first=document.body.firstChild;//得到页面的第一个元素 va ...
- Ubuntu Linux上安装配置Mysql
一.安装: 三种安装方式: 1. 从网上安装 sudo apt-get install mysql-server.装完已经自动配置好环境变量,可以直接使用mysql的命令. 注:建议将/etc/apt ...
- 【iCore3 双核心板】例程十:RTC实时时钟实验——显示日期和时间
实验指导书及代码包下载: http://pan.baidu.com/s/1jHuZcnc iCore3 购买链接: https://item.taobao.com/item.htm?id=524229 ...
- 动态给drawable上色
只加载一个资源,然后在运行的时候通过ColorFilter进行上色 public Drawable colorDrawable(Resources res, @DrawableRes int draw ...
- GDC2016 Epic Games【Bullet Train】 新风格的VR-FPS的制作方法
追求“舒适”和“快感”的VR游戏设计方法 http://game.watch.impress.co.jp/docs/news/20160318_749016.html [Bullet Tr ...
- 《UML大战需求分析》阅读笔记01
在刚学习软件开发的课程时,首先学习了UML设计,但只是学习了基本的语法,虽然在学期通过课堂练习进行了实践,但并没有真正理解其中作用.为了进一步的理解UML的用法,我阅读了<UML大战需求分析&g ...
- yield return的用法简介
使用yield return 语句可一次返回一个元素. 迭代器的声明必须满足以下要求: 返回类型必须为 IEnumerable.IEnumerable<T>.IEnumerator 或 I ...
- Oracle序列和索引
序列和索引 一.序列 1.序列的概念: 序列(Sequence)是用来生成连续的整数数据的对象.它常常用来作为主键的增长列,可以升序,也可以降序. 2.创建序列: 语法:创建序列 ...