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 ...
随机推荐
- Solr简单总结
Solr 运行Solr服务 方式一:Jetty服务器启动Solr 进入solr-4.10.2/example目录 打开命令行,执行java –jar start.jar命令,即可启动Solr服务 打开 ...
- python三大神器之生成器
生成器Generator: 本质:迭代器(所以自带了__iter__方法和__next__方法,不需要我们去实现) 特点:惰性运算,开发者自定义 在python中有三种方法来获取生成器: 1.通过生成 ...
- tarnado源码解析系列一
目录 tarnado tarnado源码安装 tarnado测试程序 application类的解析 一. tarnado简介 最近在学习Python,无意间接触到的tarnado,感觉tarnado ...
- spoj1026 favorite dice
#include <bits/stdc++.h> using namespace std; int n,t; ; double dp[N]; /* 甩一个n面的骰子,问每一面都被甩到的需要 ...
- Shoot the Bullet(ZOJ3229)(有源汇上下界最大流)
描述 ensokyo is a world which exists quietly beside ours, separated by a mystical border. It is a utop ...
- 集成activiti到现有项目中
1.在lib中添加相关的jar包 2.找到一个activiti.cfg.xml,若是想用现有的数据库需要配置 <?xml version="1.0" encoding=&qu ...
- WPF中的ControlTemplate(控件模板)
原文:WPF中的ControlTemplate(控件模板) WPF中的ControlTemplate(控件模板) ...
- 长沙Uber优步司机奖励政策(3月28日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- C++11中std::forward的使用
std::forward argument: Returns an rvalue reference to arg if arg is not an lvalue reference; If arg ...
- 通过transpose和flip实现图像旋转90/180/270度
在fbc_cv库中,提供了对图像进行任意角度旋转的函数rotate,其实内部也是调用了仿射变换函数warpAffine.如果图像仅是进行90度倍数的旋转,是没有必要用warpAffine函数的.这里通 ...