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 ...
随机推荐
- WPF 程序启动显示为通知区域的图标方法
首先需要引用 System.Windows; System.Drawing; public partial class MainWindow : Window { public MainWindo ...
- Android课程---Android Studio的一些小技巧
APK瘦身 在Android Studio中我们可以开启混淆,和自动删除没有Resources文件,来达到给APP瘦身的目的,这对于一些维护很久的老项目比较有用,里面有很多无效的Resource, 删 ...
- where过滤字句
无条件为真 为假 别名不能直接用来当做查询来用 只能如此 下面这些语句是可以使用的,使用的是排序规则
- String与StringBuilder之间区别(转)
当我们在初学使用C#时,常常会不知道该用StringBuilder合适还是用String高效,下面是我在学习当中对StringBuilder和String的区别总结,分享给大家. String类有不可 ...
- Centos 6.5升级到Git2.1.2
安装需求 # yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel# yum install gcc pe ...
- JS中的混合模式
function Animation(list) { this.box = document.getElementById(list.id); this.size = list.size; this. ...
- IIS应用程序池最大进程数设置
1.当工作进程数>1时,如果有多个连接请求就会启动多个工作进程实例来处理,所启动的最多工作进程数就是你设置的最大进程数,后续更多的连接请求会循环的发送至不同的工作进程来处理.每个工作进程都能承担 ...
- C#编程利器之二:结构与枚举(Structure and enumeration)【转】
C#编程利器之二:结构与枚举(Structure and enumeration) 在上一篇文章中,介绍了类如何封装程序中的对象.而实际中,出了类可以封装对象外,结构和枚举也可以封装一些对象,本文将着 ...
- centos 修改网卡名为eth0
centos7和centos6的命名规则不同,centos6网卡名由udev控制 网卡名以eth开头然后第一块网卡叫eth0第二块网卡叫eth1,然后centos7的网卡命名规则截然不同 centos ...
- Nhiberate (二) 搭项目
使用: visual studio 2015 ;SQL SERVER 2012. 参考.测试可用 其中有点不太一样的地儿, ISession 的泛型方法: 用了 QueryOver<>,转 ...