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)的更多相关文章

  1. 分块+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 ...

  2. POJ 2777——线段树Lazy的重要性

    POJ 2777 Count Color --线段树Lazy的重要性 原题 链接:http://poj.org/problem?id=2777 Count Color Time Limit: 1000 ...

  3. poj3468 线段树+lazy标记

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 92921   ...

  4. poj 2777(线段树+lazy思想) 小小粉刷匠

    http://poj.org/problem?id=2777 题目大意 涂颜色,输入长度,颜色总数,涂颜色次数,初始颜色都为1,然后当输入为C的时候将x到y涂为颜色z,输入为Q的时候输出x到y的颜色总 ...

  5. hdu 1698 Just a Hook 【线段树+lazy】

    题目 写了一天的线段树,这道题主要说明一下sum是赋值的,不是累加的,并且在push_down的时候lazy也是赋值的.因可能对懒标记的理解还不是很透彻吧. #include <iostream ...

  6. HDU3577Fast Arrangement(线段树+lazy)

    Problem Description Chinese always have the railway tickets problem because of its' huge amount of p ...

  7. HDU 3954 Level up(多颗线段树+lazy操作)

    又是一开始觉得的水题,结果GG了好久的东西... 题意是给你n个英雄,每个英雄开始为1级经验为0,最多可以升到k级并且经验一直叠加,每一级都有一个经验值上限,达到就升级.接着给你两种操作:W li r ...

  8. 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 ...

  9. poj 3237 树链剖分模板(用到线段树lazy操作)

    /* 本体在spoj375的基础上加了一些操作,用到线段树的lazy操作模板类型 */ #include<stdio.h> #include<string.h> #includ ...

随机推荐

  1. c#面向对象基础 类、方法、方法重载

    C#是纯粹的面向对象编程语言,它真正体现了“一切皆为对象”的精神.在C#中,即使是最基本的数据类型,如int,double,bool类型,都属于System.Object(Object为所有类型的基类 ...

  2. vmware安装centos时遇到无法创建新虚拟机: 不具备执行此操作的权限。

    我的问题是选择文件位置造成的,我选择在了VMware安装的位置,重新选择一个文件夹即可.

  3. 网页mp3播放代码

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  4. IOS第七天(2:UiTableView 加上数据分离)

    ****加上数据分离 #import "HMViewController.h" #import "HMStudent.h" @interface HMViewC ...

  5. Range

    欢迎转载,转载请注明出处,徽沪一郎. 概要 Scala中Range可以看成是List的特例,Range的包含的元素类型是Int, 本文介绍如何创建Range Range创建 方法一: val r1 = ...

  6. TestNG的一个不足之处

    PS:本博客selenium分类不会记载selenium打开浏览器,定位元素,操作页面元素,切换到iframe,处理alter.confirm和prompt对话框这些在网上随处可见的信息:本博客此分类 ...

  7. Android颜色资源文件

    <?xml version="1.0" encoding="utf-8"?><resources> <color name=&qu ...

  8. 安卓和ios的lineheight的不一样如何解决?

    lineheight在pc端上显示很正常,但是在手机就很不同,在iphone6上,设置了lineheight,但是文本上面多了几像素,如果你设置lineheight在35px一下的按钮(用span做的 ...

  9. 能套用的tab栏切换

    效果: 在style.js文件里封装了一个Show函数,有4个参数,di(当前点击的按钮),num(按钮个数,也可以说是下面具体内容个数),divn(一个id前缀,这个例子里是ta,),active( ...

  10. tesseract ocr文字识别Android实例程序和训练工具全部源代码

    tesseract ocr是一个开源的文字识别引擎,Android系统中也可以使用.可以识别50多种语言,通过自己训练识别库的方式,可以大大提高识别的准确率. 为了节省大家的学习时间,现将自己近期的学 ...