UVA-12436 Rip Van Winkle's Code (线段树区间更新)
题目大意:一个数组,四种操作:
long long data[250001];
void A( int st, int nd ) {
for( int i = st; i <= nd; i++ ) data[i] = data[i] + (i - st + 1);
}
void B( int st, int nd ) {
for( int i = st; i <= nd; i++ ) data[i] = data[i] + (nd - i + 1);
}
void C( int st, int nd, int x ) {
for( int i = st; i <= nd; i++ ) data[i] = x;
}
long long S( int st, int nd ) {
long long res = 0;
for( int i = st; i <= nd; i++ ) res += data[i];
return res;
}
模拟这四种操作。
题目分析:三种更新操作,一种询问操作。三种更新实际上是两种,add更新(等差数列做加减运算仍是等差数列)和set更新,add更新的懒标记记录首项、尾项和公差。
代码如下:
# include<bits/stdc++.h>
using namespace std;
# define LL long long
# define mid (l+(r-l)/2) const int N=250000; struct Node
{
LL sum;
LL x,d,st,ed;
bool lazy_set;
bool lazy_add;
};
Node tr[(N+5)*4+100];
char op[2]; void clear_lazy(int rt)
{
tr[rt].lazy_add=false;
tr[rt].d=tr[rt].st=tr[rt].ed=0;
} void change1(int rt,int l,int r,LL st,LL ed,LL d)
{
tr[rt].lazy_add=true;
tr[rt].sum+=(LL)(r-l+1)*(st+ed)/2;
tr[rt].st+=st;
tr[rt].ed+=ed;
tr[rt].d+=d;
} void change2(int rt,int l,int r,LL x)
{
tr[rt].lazy_set=true;
tr[rt].sum=(LL)(r-l+1)*x;
tr[rt].x=x; clear_lazy(rt);
} void pushUp(int rt)
{
tr[rt].sum=tr[rt<<1].sum+tr[rt<<1|1].sum;
} void pushDown(int rt,int l,int r)
{
if(tr[rt].lazy_set){ change2(rt<<1,l,mid,tr[rt].x);
change2(rt<<1|1,mid+1,r,tr[rt].x);
tr[rt].lazy_set=false;
}
if(tr[rt].lazy_add){ LL st=tr[rt].st;
LL ed=tr[rt].ed;
int d=tr[rt].d; change1(rt<<1,l,mid,st,st+d*(mid-l),d);
change1(rt<<1|1,mid+1,r,st+d*(mid-l+1),ed,d); clear_lazy(rt);
}
} void build(int rt,int l,int r)
{
tr[rt].lazy_set=false;
tr[rt].lazy_add=false;
tr[rt].sum=tr[rt].d=0;
tr[rt].st=tr[rt].ed=0;
if(l==r) return ;
build(rt<<1,l,mid);
build(rt<<1|1,mid+1,r);
} void update1(int rt,int l,int r,int L,int R,LL d)
{
if(L<=l&&r<=R){
if(d>0){
change1(rt,l,r,l-L+1,r-L+1,d);
}else{
change1(rt,l,r,R-l+1,R-r+1,d);
}
}else{
pushDown(rt,l,r);
if(L<=mid) update1(rt<<1,l,mid,L,R,d);
if(R>mid) update1(rt<<1|1,mid+1,r,L,R,d);
pushUp(rt);
}
} void update2(int rt,int l,int r,LL L,LL R,LL x)
{
if(L<=l&&r<=R){
change2(rt,l,r,x);
}else{
pushDown(rt,l,r);
if(L<=mid) update2(rt<<1,l,mid,L,R,x);
if(R>mid) update2(rt<<1|1,mid+1,r,L,R,x);
pushUp(rt);
}
} LL query(int rt,int l,int r,LL L,LL R)
{
if(L<=l&&r<=R) return tr[rt].sum;
pushDown(rt,l,r);
LL res=0;
if(L<=mid) res+=query(rt<<1,l,mid,L,R);
if(R>mid) res+=query(rt<<1|1,mid+1,r,L,R);
return res;
} int main()
{
//freopen("in.txt","r",stdin);
int n;
while(scanf("%d",&n)!=EOF)
{
build(1,1,N);
LL a,b,c;
while(n--)
{
scanf("%s",op);
if(op[0]=='A'){
scanf("%lld%lld",&a,&b);
update1(1,1,N,a,b,1ll);
}else if(op[0]=='B'){
scanf("%lld%lld",&a,&b);
update1(1,1,N,a,b,-1ll);
}else if(op[0]=='C'){
scanf("%lld%lld%lld",&a,&b,&c);
update2(1,1,N,a,b,c);
}else{
scanf("%lld%lld",&a,&b);
printf("%lld\n",query(1,1,N,a,b));
}
}
}
return 0;
}
UVA-12436 Rip Van Winkle's Code (线段树区间更新)的更多相关文章
- Uva 12436 Rip Van Winkle's Code
Rip Van Winkle was fed up with everything except programming. One day he found a problem whichrequir ...
- UVA 12436 - Rip Van Winkle's Code(线段树)
UVA 12436 - Rip Van Winkle's Code option=com_onlinejudge&Itemid=8&page=show_problem&cate ...
- Uva 12436 Rip Van Winkle's Code
Rip Van Winkle was fed up with everything except programming. One day he found a problem whichrequir ...
- POJ 2528 Mayor's posters 【区间离散化+线段树区间更新&&查询变形】
任意门:http://poj.org/problem?id=2528 Mayor's posters Time Limit: 1000MS Memory Limit: 65536K Total S ...
- codevs 1690 开关灯 线段树区间更新 区间查询Lazy
题目描述 Description YYX家门前的街上有N(2<=N<=100000)盏路灯,在晚上六点之前,这些路灯全是关着的,六点之后,会有M(2<=m<=100000)个人 ...
- HDU 1556 Color the ball(线段树区间更新)
Color the ball 我真的该认真的复习一下以前没懂的知识了,今天看了一下线段树,以前只会用模板,现在看懂了之后,发现还有这么多巧妙的地方,好厉害啊 所以就应该尽量搞懂 弄明白每个知识点 [题 ...
- hihoCoder 1080 : 更为复杂的买卖房屋姿势 线段树区间更新
#1080 : 更为复杂的买卖房屋姿势 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho都是游戏迷,“模拟都市”是他们非常喜欢的一个游戏,在这个游戏里面他们 ...
- HDU 5023 A Corrupt Mayor's Performance Art(线段树区间更新)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5023 解题报告:一面墙长度为n,有N个单元,每个单元编号从1到n,墙的初始的颜色是2,一共有30种颜色 ...
- HDU 4902 Nice boat 2014杭电多校训练赛第四场F题(线段树区间更新)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4902 解题报告:输入一个序列,然后有q次操作,操作有两种,第一种是把区间 (l,r) 变成x,第二种是 ...
- HDU 1698 线段树 区间更新求和
一开始这条链子全都是1 #include<stdio.h> #include<string.h> #include<algorithm> #include<m ...
随机推荐
- hdu 1031 (partial sort problem, nth_element, stable_partition, lambda expression) 分类: hdoj 2015-06-15 17:47 26人阅读 评论(0) 收藏
partial sort. first use std::nth_element to find pivot, then use std::stable_partition with the pivo ...
- 到目前为止,Linux下最完整的Samba服务器配置攻略 (转)
http://blog.chinaunix.net/uid-23069658-id-3142052.html 安装平台为UBUNTU 14.04,直接软件中心安装samba, service smb ...
- (kate)win8-64位系统下opencv-2.4.3的安装以及在visual_studio2010中配置
环境: 操作系统:window8.1 64bit Opencv版本:OPencv-2.4.3 VS版本:vs 2010 一.安装Opencv 1.Opencv官网http://opencv.org/ ...
- 单例模式简单解析--Singleton 单例模式(懒汉方式和饿汉方式)
单例模式的概念: 单例模式的意思就是只有一个实例.单例模式确保某一个类只有一个实例,而且自行实例化并向整个系统提供这个实例.这个类称为单例类. 关键点: 1)一个类只有一个实例 这是最基本 ...
- 加载不同的nib文件
只需要实现nibName方法即可 另外还需在vc控制器初始化的时候不指定对应的nib文件名 -(NSString *)nibName { if(UI_USER_INTERFACE_IDIOM() == ...
- mycat启动后,用Navicat Premium 连接报 "2013"
最近在学习mycat,启动后,用Navicat Premium 连接报 "2013" Lost Connection During Query ,经过一顿百度也没发现是怎么回事, ...
- Interview----First single charactor
题目:在一个字符串中找到第一个只出现一次的字符.如输入 abaccdeff,则输出 b. 分析:这道题是 2006 年 google 的一道笔试题. 分析: 用 Hash, 时间和空间复杂度是 O(N ...
- eclipse GIT使用
新建工程(要和GIT上同名,同类型)->右键->team->add to index->commit->更新config文件->remote: fetch from ...
- 【LeetCode OJ】Palindrome Partitioning
Problem Link: http://oj.leetcode.com/problems/palindrome-partitioning/ We solve this problem using D ...
- 5、SQL基础整理(字符串函数)
字符串函数 ASCII 返回字符串首字母的ascii编码 select ASCII('name') select ASCII(name) from xuesheng select *from xues ...