题目大意:一个数组,四种操作:

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 (线段树区间更新)的更多相关文章

  1. Uva 12436 Rip Van Winkle's Code

    Rip Van Winkle was fed up with everything except programming. One day he found a problem whichrequir ...

  2. UVA 12436 - Rip Van Winkle&#39;s Code(线段树)

    UVA 12436 - Rip Van Winkle's Code option=com_onlinejudge&Itemid=8&page=show_problem&cate ...

  3. Uva 12436 Rip Van Winkle&#39;s Code

    Rip Van Winkle was fed up with everything except programming. One day he found a problem whichrequir ...

  4. POJ 2528 Mayor's posters 【区间离散化+线段树区间更新&&查询变形】

    任意门:http://poj.org/problem?id=2528 Mayor's posters Time Limit: 1000MS   Memory Limit: 65536K Total S ...

  5. codevs 1690 开关灯 线段树区间更新 区间查询Lazy

    题目描述 Description YYX家门前的街上有N(2<=N<=100000)盏路灯,在晚上六点之前,这些路灯全是关着的,六点之后,会有M(2<=m<=100000)个人 ...

  6. HDU 1556 Color the ball(线段树区间更新)

    Color the ball 我真的该认真的复习一下以前没懂的知识了,今天看了一下线段树,以前只会用模板,现在看懂了之后,发现还有这么多巧妙的地方,好厉害啊 所以就应该尽量搞懂 弄明白每个知识点 [题 ...

  7. hihoCoder 1080 : 更为复杂的买卖房屋姿势 线段树区间更新

    #1080 : 更为复杂的买卖房屋姿势 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho都是游戏迷,“模拟都市”是他们非常喜欢的一个游戏,在这个游戏里面他们 ...

  8. HDU 5023 A Corrupt Mayor's Performance Art(线段树区间更新)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5023 解题报告:一面墙长度为n,有N个单元,每个单元编号从1到n,墙的初始的颜色是2,一共有30种颜色 ...

  9. HDU 4902 Nice boat 2014杭电多校训练赛第四场F题(线段树区间更新)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4902 解题报告:输入一个序列,然后有q次操作,操作有两种,第一种是把区间 (l,r) 变成x,第二种是 ...

  10. HDU 1698 线段树 区间更新求和

    一开始这条链子全都是1 #include<stdio.h> #include<string.h> #include<algorithm> #include<m ...

随机推荐

  1. 基于K2 BPM平台,中原地产实现了从2个人到5万多人的跨越

    演讲人:吴付文 中原地产CIO 点击这里查看中原地产怎么使用BPM实现业绩的飞跃式发展.

  2. S5PV210之添加缺少的-内核提供的'.h'文件 linux3.0.8驱动

    怎样解决编译时出现内核提供的函数或变量没有定义,使用source insight搜索功能找到声明的头文件,然后包含该头件就行了: 比如: error: implicit declaration of ...

  3. [网络技术]VPN设置

    1.解决VPN服务器默认路由困扰 现在移动办公已经变得家常便饭,每次外出出差办公需要访问单位的内网服务器时,该怎么办呢?相信很多人都想到了VPN连接!的确,使用VPN连接, 我们可以利用现成的Inte ...

  4. Java custom annotations

    Custom annotation definition is similar as Interface, just with @ in front. Annotation interface its ...

  5. iOS字体设置

    label.font = [UIFont fontWithName:@"Arial-BoldItalicMT" size:24]; 字体名如下: Font Family: Amer ...

  6. NSArray,NSSet,NSDictionary的遍历,基本使用集锦

    NSArray *array = [NSArray arrayWithObjects:@"zhangsan",@"lisi",@"wangwu&quo ...

  7. Unity3D ShaderLab 模拟精灵动画

    Unity3D ShaderLab 模拟精灵动画 在上一篇,介绍了通过Shader 模拟纹理运动,那么更深一步讲,我们也可以把帧动画的精灵纹理运动通过shader实现. 虽然大家都是在游戏脚本中做更高 ...

  8. WCF之多个终结点

    1.服务端配置如下(一个Service节点下可有多个endpoint,): <system.serviceModel> <services> <service name= ...

  9. 【转】 golang slice array

    1. array   同一类型数据的集合     var arr [n]type    //声明type类型一维数组     var arr [m][n]type //声明type类型二维数组     ...

  10. 一步一步理解GB、GBDT、xgboost

    GBDT和xgboost在竞赛和工业界使用都非常频繁,能有效的应用到分类.回归.排序问题,虽然使用起来不难,但是要能完整的理解还是有一点麻烦的.本文尝试一步一步梳理GB.GBDT.xgboost,它们 ...