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 ...
随机推荐
- spring security为不同用户显示各自的登录成功页面
一个常见的需求是,普通用户登录之后显示普通用户的工作台,管理员登陆之后显示后台管理页面.这个功能可以使用taglib解决. 其实只要在登录成功后的jsp页面中使用taglib判断当前用户拥有的权限进行 ...
- Lua数据结构
lua中的table不是一种简单的数据结构,它可以作为其他数据结构的基础,如:数组,记录,链表,队列等都可以用它来表示. 1.数组 在lua中,table的索引可以有很多种表示方式.如果用整数来表示t ...
- Centos上Docker 使用dockerfile构建容器实现ssh
这几日在学习docker.遇到的问题数一年都数不完,网上大多数都是ubuntu的,百度或者谷歌的时候心好累.写写文档来帮助使用centos的docker爱好者们. docker基本操作这里就不介绍了 ...
- 屏蔽Enter触发的事件
无论是 <button type="button" onclick="console.log('123');">123</button> ...
- PHP第三方登录 -- 微博登录
进化史 博客园 首页 新随笔 联系 管理 订阅 随笔- 9 文章- 0 评论- 0 php 实现qq第三方登录 学习之前,请大家先看一下oAuth协议. 首先呢,我们进入QQ互联的官方网站 ht ...
- P4 前端编译器p4c-bm、后端编译器bmv2命令安装 make error问题
参考:Github 安装p4c-bm: sudo pip install -r requirements.txt sudo pip install -r requirements_v1_1.txt / ...
- Android RecyclerView的基本使用
Android RecyclerView 在去年的Google I/O大会上就推出来了,以前经常使用的ListView 继承的是AbsListView,而RecyclerView则直接继承 ViewG ...
- JavaMail接、收邮件
我总算把这个研究出来啦.... 不要觉得 代码有点多哈. 我们先来说发送邮箱吧,首先建立一个属性文件*.properties sys.properties server=smtp.163.com ## ...
- find 找出大文件
找到大文件 find . -type f -size +100M -exec du -smh {} \;
- pptp记录日志
/etc/ppp/ip-up #!/bin/bash # This file should not be modified -- make local changes to # /etc/ppp/ip ...