picks loves segment tree I
picks loves segment tree I
题目背景
来源:
\(\text {2018 WC Segment Tree Beats}\)
原作者:
\(\text {C_SUNSHINE}\)
\(\text {jiry_2}\)
题目描述:
- 给定一个长度为\(n\)的数列\(A\),接下来有\(m\)次操作:
- 区间\([l,r]\)中的所有数变成\(min(A_i,x)\)
- 询问区间\([l,r]\)中所有数的和
- \(n,m \le 50000\)
- 我会分块!
- \(n,m \le 500000\)
- 线段树?
输入输出格式
输入格式
第一行两个正整数表示\(N,M\)
第二行\(N\)个正整数,表示数列\(A_i\)
接下来\(M\)行每行包含\(3\)或\(4\)个整数,表示一个操作,具体如下:
操作1: 1 x y x 含义:将区间\([l,r]\)内每个数变成\(min(A_i,x)\)
操作2: 2 x y 含义:输出区间\([l,r]\)内每个数的和
输出格式
输出包含若干行整数,即为所有操作\(2\)的结果。
说明:
对于所有的数据,有\(N \le 500000,M \le 500000,a_i \le 2 \times 10^9\)
保证所有出现的数据在\(int64/long \ long\)范围内
本菜鸡说不清楚,直接当板子了,看网上的dalao们都写的Ⅴ,Ⅵ,Ⅶ,Ⅸ什么的,我太菜,从基础开始来吧。
Code:
#include <cstdio>
#define ll long long
#define ls id<<1
#define rs id<<1|1
const int N=5e5+10;
ll mx[N<<2],se[N<<2],sum[N<<2],tag[N<<2],a[N];
ll max(ll x,ll y){return x>y?x:y;}
int cnt[N<<2],n,m;
void updata(int id)
{
if(mx[ls]>mx[rs])
{
mx[id]=mx[ls];
cnt[id]=cnt[ls];
se[id]=max(se[ls],mx[rs]);
}
else if(mx[ls]<mx[rs])
{
mx[id]=mx[rs];
cnt[id]=cnt[rs];
se[id]=max(mx[ls],se[rs]);
}
else
{
mx[id]=mx[ls];
cnt[id]=cnt[ls]+cnt[rs];
se[id]=max(se[ls],se[rs]);
}
sum[id]=sum[ls]+sum[rs];
}
void build(int id,int l,int r)
{
if(l==r)
{
sum[id]=mx[id]=a[l];
cnt[id]=1;
se[id]=0;
return;
}
int mid=l+r>>1;
build(ls,l,mid),build(rs,mid+1,r);
updata(id);
}
void pushdown(int id)
{
if(tag[id])
{
if(mx[ls]>tag[id])
{
sum[ls]-=(mx[ls]-tag[id])*cnt[ls];
tag[ls]=mx[ls]=tag[id];
}
if(mx[rs]>tag[id])
{
sum[rs]-=(mx[rs]-tag[id])*cnt[rs];
tag[rs]=mx[rs]=tag[id];
}
tag[id]=0;
}
}
void change(int id,int L,int R,int l,int r,ll x)
{
if(mx[id]<=x) return;
if(L==l&&R==r&&se[id]<x)
{
sum[id]-=(mx[id]-x)*cnt[id];
tag[id]=mx[id]=x;
return;
}
pushdown(id);
int Mid=L+R>>1;
if(r<=Mid) change(ls,L,Mid,l,r,x);
else if(l>Mid) change(rs,Mid+1,R,l,r,x);
else change(ls,L,Mid,l,Mid,x),change(rs,Mid+1,R,Mid+1,r,x);
updata(id);
}
ll query(int id,int L,int R,int l,int r)
{
if(l==L&&r==R) return sum[id];
pushdown(id);
int Mid=L+R>>1;
if(r<=Mid) return query(ls,L,Mid,l,r);
else if(l>Mid) return query(rs,Mid+1,R,l,r);
else return query(ls,L,Mid,l,Mid)+query(rs,Mid+1,R,Mid+1,r);
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++) scanf("%lld",a+i);
build(1,1,n);
ll x;
for(int op,l,r,i=1;i<=m;i++)
{
scanf("%d%d%d",&op,&l,&r);
if(op==1)
{
scanf("%lld",&x);
change(1,1,n,l,r,x);
}
else
printf("%lld\n",query(1,1,n,l,r));
}
return 0;
}
2018.10.13
picks loves segment tree I的更多相关文章
- 2018.07.29~30 uoj#170. Picks loves segment tree VIII(线段树)
传送门 线段树好题. 维护区间取两种最值,区间加,求区间两种历史最值,区间最小值. 自己的写法调了一个晚上+一个上午+一个下午+一个晚上并没有调出来,90" role="prese ...
- 题解-hzy loves segment tree I
Problem 题目概要:给定一棵 \(n\) 个节点的树,点有点权,进行 \(m\) 次路径取\(\max\)的操作,最后统一输出点权 \(n\leq 10^5,m\leq 5\times 10^6 ...
- BestCoder#16 A-Revenge of Segment Tree
Revenge of Segment Tree Problem Description In computer science, a segment tree is a tree data struc ...
- [LintCode] Segment Tree Build II 建立线段树之二
The structure of Segment Tree is a binary tree which each node has two attributes startand end denot ...
- [LintCode] Segment Tree Build 建立线段树
The structure of Segment Tree is a binary tree which each node has two attributes start and end deno ...
- Segment Tree Modify
For a Maximum Segment Tree, which each node has an extra value max to store the maximum value in thi ...
- Segment Tree Query I & II
Segment Tree Query I For an integer array (index from 0 to n-1, where n is the size of this array), ...
- Segment Tree Build I & II
Segment Tree Build I The structure of Segment Tree is a binary tree which each node has two attribut ...
- Lintcode: Segment Tree Query II
For an array, we can build a SegmentTree for it, each node stores an extra attribute count to denote ...
随机推荐
- 方别《QQ群霸屏技术》,又见《QQ群建群细则》
规则,时刻变动;QQ群系列,咱们再来一轮. QQ群霸屏技术,你说建群貌似很菜,大家仿佛都知道,其实只知其一不知其二. QQ群类别 群分类,常规的就以下几种. 普通群. 建群随意,偏个性化,一言不合就拉 ...
- 自定义控件,继承自 ListView
public class MyListView extends ListView { /** * 如果在xml中创建并设置了style,就会调用三个参数的. * * @param context * ...
- ThinkPHP中的pathinfo模式和URL重写
语文一直不太好,要我怎么解释这个pathinfo模式还真不知道怎么说,那就先来一段代码说下pathinfo模式吧 http://serverName/appName/module/action/id/ ...
- logger模块的使用
logging模块 下面是logger模块的配置文件,在写程序需要记录日志可以直接拿过来用,但是要经过相应配置的一些修改. 对于如何使用,在我上一篇随笔<ATM程序规范练习>中的记录日志的 ...
- node获取URL数据
req.method -->GET req.hostname -->127.0.0.1 req.originalUrl -->/test/test/test?name=wang ...
- rhel6.4 根目录扩容
状况:根目录容量不足 解决:扩容根目录 ====================================================== 解决步骤: 1. 将新的磁盘加入服务器 2. 使用 ...
- 【Leetcode】804. Unique Morse Code Words
Unique Morse Code Words Description International Morse Code defines a standard encoding where each ...
- 牛客暑假多校第六场I-Team Rocket
一.题意 我们是穿越银河的火箭队....... 给出若干个区间,之后给出若干个点,要求对每个点求出,第一个覆盖点的区间的数量,之后用当前所有点覆盖的区间的序号的乘积结合输入的Y来生成下一位点.最后输出 ...
- Hue 工具使用
Hue 是一个 Web 接口的 Hadoop 分析数据工具,由 Cloudra 公司开源 官方网址 Github 地址 -> 安装方法 文档地址 一.Build 1.ubuntu安装所需环境(以 ...
- jquery validation remote进行唯一性验证时只使用自定义参数,不使用默认参数
在使用validation进行唯一性验证时,想各个模块写一个统一的方法,相统一参数名称,但是remote方法会默认把对应的参数传过去 如: 会把role.roleName默认作为变量提交过去 所以想自 ...