4927 线段树练习5

 时间限制: 1 s
 空间限制: 128000 KB
 题目等级 : 黄金 Gold
 
 
 
题目描述 Description

有n个数和5种操作

add a b c:把区间[a,b]内的所有数都增加c

set a b c:把区间[a,b]内的所有数都设为c

sum a b:查询区间[a,b]的区间和

max a b:查询区间[a,b]的最大值

min a b:查询区间[a,b]的最小值

输入描述 Input Description

第一行两个整数n,m,第二行n个整数表示这n个数的初始值

接下来m行操作,同题目描述

输出描述 Output Description

对于所有的sum、max、min询问,一行输出一个答案

样例输入 Sample Input

10 6

3 9 2 8 1 7 5 0 4 6

add 4 9 4

set 2 6 2

add 3 8 2

sum 2 10

max 1 7

min 3 6

样例输出 Sample Output

49

11

4

数据范围及提示 Data Size & Hint

10%:1<n,m<=10

30%:1<n,m<=10000

100%:1<n,m<=100000

保证中间结果在long long(C/C++)、int64(pascal)范围内

PS:由于数据6出错导致某些人只有90分,已于2016.5.13修正。

出题人在此对两位90分的用户表示诚挚的歉意

思路:

  裸线段树;

  轻松ac;

来,上代码:

#include <cstdio>
#include <iostream>
#include <algorithm> #define maxn 100001
#define LL long long using namespace std; struct TreeNodeType {
LL l,r,mid,dis,max_,min_,flag,flag_; bool if_;
};
struct TreeNodeType tree[maxn<<]; LL if_z,n,m; char Cget; inline void read_int(LL &now)
{
now=,if_z=,Cget=getchar();
while(Cget<''||Cget>'')
{
if(Cget=='-') if_z=-;
Cget=getchar();
}
while(Cget>=''&&Cget<='')
{
now=now*+Cget-'';
Cget=getchar();
}
now*=if_z;
} inline void tree_up(LL now)
{
tree[now].dis=tree[now<<].dis+tree[now<<|].dis;
tree[now].max_=max(tree[now<<].max_,tree[now<<|].max_);
tree[now].min_=min(tree[now<<].min_,tree[now<<|].min_);
} inline void tree_down(LL now)
{
if(tree[now].l==tree[now].r) return ;
if(tree[now].if_)
{
tree[now<<].flag=,tree[now<<].flag_=tree[now].flag_;
tree[now<<].if_=true,tree[now<<].max_=tree[now].flag_;
tree[now<<].min_=tree[now].flag_;
tree[now<<].dis=(tree[now<<].r-tree[now<<].l+)*tree[now].flag_;
tree[now<<|].flag=,tree[now<<|].flag_=tree[now].flag_;
tree[now<<|].if_=true,tree[now<<|].max_=tree[now].flag_;
tree[now<<|].min_=tree[now].flag_;
tree[now<<|].dis=(tree[now<<|].r-tree[now<<|].l+)*tree[now].flag_;
}
else
{
if(tree[now<<].if_)
{
tree[now<<].max_+=tree[now].flag;
tree[now<<].min_+=tree[now].flag;
tree[now<<].flag_+=tree[now].flag;
tree[now<<].dis+=(tree[now<<].r-tree[now<<].l+)*tree[now].flag;
}
else
{
tree[now<<].max_+=tree[now].flag;
tree[now<<].min_+=tree[now].flag;
tree[now<<].flag+=tree[now].flag;
tree[now<<].dis+=(tree[now<<].r-tree[now<<].l+)*tree[now].flag;
}
if(tree[now<<|].if_)
{
tree[now<<|].max_+=tree[now].flag;
tree[now<<|].min_+=tree[now].flag;
tree[now<<|].flag_+=tree[now].flag;
tree[now<<|].dis+=(tree[now<<|].r-tree[now<<|].l+)*tree[now].flag;
}
else
{
tree[now<<|].max_+=tree[now].flag;
tree[now<<|].min_+=tree[now].flag;
tree[now<<|].flag+=tree[now].flag;
tree[now<<|].dis+=(tree[now<<|].r-tree[now<<|].l+)*tree[now].flag;
}
}
tree[now].flag=,tree[now].flag_=,tree[now].if_=false;
} void tree_build(LL now,LL l,LL r)
{
tree[now].l=l,tree[now].r=r;
if(l==r)
{
read_int(tree[now].dis);
tree[now].max_=tree[now].dis;
tree[now].min_=tree[now].dis;
return ;
}
tree[now].mid=(l+r)>>;
tree_build(now<<,l,tree[now].mid);
tree_build(now<<|,tree[now].mid+,r);
tree_up(now);
} void tree_change(LL now,LL l,LL r,LL x)
{
if(tree[now].l==l&&tree[now].r==r)
{
if(tree[now].if_) tree[now].flag_+=x;
else tree[now].flag+=x;
tree[now].dis+=(r-l+)*x;
tree[now].max_+=x;
tree[now].min_+=x;
return ;
}
if(tree[now].flag||tree[now].if_) tree_down(now);
if(l>tree[now].mid) tree_change(now<<|,l,r,x);
else if(r<=tree[now].mid) tree_change(now<<,l,r,x);
else
{
tree_change(now<<,l,tree[now].mid,x);
tree_change(now<<|,tree[now].mid+,r,x);
}
tree_up(now);
} void tree_change_(LL now,LL l,LL r,LL x)
{
if(tree[now].l==l&&tree[now].r==r)
{
tree[now].if_=true;
tree[now].flag_=x;
tree[now].dis=(r-l+)*x;
tree[now].max_=x;
tree[now].min_=x;
return ;
}
if(tree[now].flag||tree[now].if_) tree_down(now);
if(l>tree[now].mid) tree_change_(now<<|,l,r,x);
else if(r<=tree[now].mid) tree_change_(now<<,l,r,x);
else
{
tree_change_(now<<,l,tree[now].mid,x);
tree_change_(now<<|,tree[now].mid+,r,x);
}
tree_up(now);
} LL tree_query(LL now,LL l,LL r,LL type)
{
if(tree[now].l==l&&tree[now].r==r)
{
if(type==) return tree[now].dis;
if(type==) return tree[now].max_;
if(type==) return tree[now].min_;
}
if(tree[now].flag||tree[now].if_) tree_down(now);
tree_up(now);
if(l>tree[now].mid) return tree_query(now<<|,l,r,type);
else if(r<=tree[now].mid) return tree_query(now<<,l,r,type);
else
{
if(type==) return tree_query(now<<,l,tree[now].mid,type)+tree_query(now<<|,tree[now].mid+,r,type);
if(type==) return max(tree_query(now<<,l,tree[now].mid,type),tree_query(now<<|,tree[now].mid+,r,type));
if(type==) return min(tree_query(now<<,l,tree[now].mid,type),tree_query(now<<|,tree[now].mid+,r,type));
}
} int main()
{
read_int(n),read_int(m);
tree_build(,,n);
char ch[];
for(LL i=;i<=m;i++)
{
cin>>ch;
LL x,y,z;
if(ch[]=='a')
{
read_int(x),read_int(y),read_int(z);
tree_change(,x,y,z);
}
else if(ch[]=='s')
{
if(ch[]=='e')
{
read_int(x),read_int(y),read_int(z);
tree_change_(,x,y,z);
}
else
{
read_int(x),read_int(y);
cout<<tree_query(,x,y,);
putchar('\n');
}
}
else if(ch[]=='m')
{
if(ch[]=='a')
{
read_int(x),read_int(y);
cout<<tree_query(,x,y,);
putchar('\n');
}
else
{
read_int(x),read_int(y);
cout<<tree_query(,x,y,);
putchar('\n');
}
}
}
return ;
}

AC日记——线段树练习5 codevs 4927的更多相关文章

  1. AC日记——线段树练习三 codevs 1082 (分块尝试)

    线段树练习 3 思路: 分块: 来,上代码: #include <cmath> #include <cstdio> #include <cstring> #incl ...

  2. AC日记——线段树练习4 codevs 4919

    4919 线段树练习4  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题解  查看运行结果     题目描述 Description 给你N个数,有两种操作 ...

  3. hdu 4117 -- GRE Words (AC自动机+线段树)

    题目链接 problem Recently George is preparing for the Graduate Record Examinations (GRE for short). Obvi ...

  4. hdu 4117 GRE Words (ac自动机 线段树 dp)

    参考:http://blog.csdn.net/no__stop/article/details/12287843 此题利用了ac自动机fail树的性质,fail指针建立为树,表示父节点是孩子节点的后 ...

  5. HDU 5069 Harry And Biological Teacher(AC自动机+线段树)

    题意 给定 \(n\) 个字符串,\(m\) 个询问,每次询问 \(a\) 字符串的后缀和 \(b\) 字符串的前缀最多能匹配多长. \(1\leq n,m \leq 10^5\) 思路 多串匹配,考 ...

  6. BZOJ2434:[NOI2011]阿狸的打字机(AC自动机,线段树)

    Description 阿狸喜欢收藏各种稀奇古怪的东西,最近他淘到一台老式的打字机.打字机上只有28个按键,分别印有26个小写英文字母和'B'.'P'两个字母. 经阿狸研究发现,这个打字机是这样工作的 ...

  7. T1081 线段树练习 2 codevs

    http://codevs.cn/problem/1081/  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 大师 Master 题目描述 Description 给你N个数, ...

  8. 背单词(AC自动机+线段树+dp+dfs序)

    G. 背单词 内存限制:256 MiB 时间限制:1000 ms 标准输入输出 题目类型:传统 评测方式:文本比较   题目描述 给定一张包含N个单词的表,每个单词有个价值W.要求从中选出一个子序列使 ...

  9. AC日记——[ZJOI2008]树的统计Count bzoj 1036

    1036: [ZJOI2008]树的统计Count Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 15007  Solved: 6092[Submit ...

随机推荐

  1. Laravel常用命令

    php artisan make:controller BlogController php artisan make:model Blog

  2. SpringMVC 项目中引用其他 Module 中的方法

    1. 将要引用的Module 引入项目中 2. 在主Module中添加依赖, 3. 被引用的类必须放在 Module 中/src/下的某个package中,否则引用不到(重要)

  3. PHPCompatibility检测php版本语法兼容

    直接上步骤: cd /datas/htdocs/ mkdir PHPCompatibility cd PHPCompatibility/ curl -s http://getcomposer.org/ ...

  4. PHP redis使用命令

    很有用;以下是redis官方提供的命令使用技巧: 下载地址如下: https://github.com/owlient/phpredis(支持redis 2.0.4) Redis::__constru ...

  5. Mac远程访问Ubuntu

    MacOS和Ubuntu连接到同一个网络使用ping命令可以通信即可.SSH使用SSH可以很方便的在MacOS上访问Ubuntu,不过只能用命令行操作,相当于连接了Ubuntu的终端. 1. Ubun ...

  6. 我的Python分析成长之路6

    模块:本质就是.py结尾的文件.从逻辑上组织python代码. 包: 本质就是一个目录,带有__init__.py文件,从逻辑上组织模块. 模块的分类: 1.标准库(内置的模块) 2.开源库(第三方库 ...

  7. poj-2488 a knight's journey(搜索题)

    Time limit1000 ms Memory limit65536 kB Background The knight is getting bored of seeing the same bla ...

  8. Python之code对象与pyc文件(二)

    上一节:Python之code对象与pyc文件(一) 创建pyc文件的具体过程 前面我们提到,Python在通过import或from xxx import xxx时会对module进行动态加载,如果 ...

  9. dubbo doc入门文档

    dubbo document http://dubbo.apache.org/zh-cn/docs/user/references/xml/dubbo-protocol.html

  10. 光学字符识别OCR-4

    经过第一部分,我们已经较好地提取了图像的文本特征,下面进行文字定位. 主要过程分两步:         1.邻近搜索,目的是圈出单行文字:         2.文本切割,目的是将单行文本切割为单字. ...